zoukankan      html  css  js  c++  java
  • springboot整合mybatis

    1 创建entity实体类

    public class Student {
        private Integer stuid;
        private String stuname;
    
        public Integer getStuid() {
            return stuid;
        }
    
        public void setStuid(Integer stuid) {
            this.stuid = stuid;
        }
    
        public String getStuname() {
            return stuname;
        }
    
        public void setStuname(String stuname) {
            this.stuname = stuname;
        }
    
        public Student(Integer stuid, String stuname) {
            this.stuid = stuid;
            this.stuname = stuname;
        }
        public Student(){}
    
        public Student(String stuname) {
            this.stuname = stuname;
        }
    }

    2 创建application.yml文件

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql:///springbootjpa
        username: root
        password: 123
    #myabtis配置
    mybatis:
      type-aliases-package: com.boot.entity
      mapper-locations: classpath:/mapper/*.xml

    3 创建Dao层接口

    @Repository
    public interface StudentDao {
        //查询所有学生
        //@Select("select * from student")
        public List<Student>getAllStudent();
    
        //新增学生
        //@Insert("insert into student(stuname) values(#{stuname})")
        @Options(useGeneratedKeys=true,keyProperty = "stuid",keyColumn = "stuid")
        public void addStudent(Student student);
    
        //修改学生信息
        //@Update("update student set stuname=#{stuname} where stuid=#{stuid}")
        public int updaStudent(Student student);
    
    
        //删除学生信息
        //@Delete("delete from student where stuid=#{stuid}")
        public int delStudent(Integer stuid);
    }

    4 在resources目录下创建mapper文件下创建dao层xml文件

    <mapper namespace="com.boot.dao.StudentDao">
        <select id="getAllStudent" resultType="Student">
            select * from student
        </select>
    
        <insert id="addStudent">
            insert into student(stuname) values(#{stuname})
        </insert>
        <update id="updaStudent">
            update student set stuname=#{stuname} where stuid=#{stuid}
        </update>
        <delete id="delStudent">
            delete from student where stuid=#{stuid}
        </delete>
    </mapper>

    5  创建service层

    @Service("studentService")
    public class StudentService {
    
        @Resource
        private StudentDao studentDao;
        //查询所有学生信息
        public List<Student> getAllStudent(){
            return studentDao.getAllStudent();
        }
        //添加学生信息
        public void addStudent(Student student){
            studentDao.addStudent(student);
        }
        //修改学生信息
        public int updaStudent(Student student){
            return studentDao.updaStudent(student);
        }
        //删除学生信息
        public int delStudent(Integer stuid){
            return studentDao.delStudent(stuid);
        }
    }

    6 创建Controller层

    @RestController
    public class StudentController {
    
        @Autowired
        private StudentService studentService;
    
        //查询学生信息
        @RequestMapping("/student")
        public List<Student> getAllStudent(){
            PageHelper.startPage(1,1);
            return studentService.getAllStudent();
        }
        //添加学生信息
        @RequestMapping("/addStudent")
        public Student addStudent(){
            Student student=new Student();
            student.setStuname("99");
              studentService.addStudent(student);
            return student;
        }
        //修改学生信息
        @RequestMapping("/updaStudent")
        public int updaStudent(Student student){
            return studentService.updaStudent(new Student(10,"33"));
        }
        //删除学生信息
        @RequestMapping("/delStudent")
        public int delStudent(Integer stuid){
            return studentService.delStudent(11);
        }
    }
    @Repository
    public interface StudentDao {
    //查询所有学生
    //@Select("select * from student")
    public List<Student>getAllStudent();

    //新增学生
    //@Insert("insert into student(stuname) values(#{stuname})")
    @Options(useGeneratedKeys=true,keyProperty = "stuid",keyColumn = "stuid")
    public void addStudent(Student student);

    //修改学生信息
    //@Update("update student set stuname=#{stuname} where stuid=#{stuid}")
    public int updaStudent(Student student);


    //删除学生信息
    //@Delete("delete from student where stuid=#{stuid}")
    public int delStudent(Integer stuid);
    }
  • 相关阅读:
    Nginx 代理gRPC—为 Skywalking 提供负载均衡
    分布式链路追踪 SkyWalking 源码分析 —— Agent 发送 Trace 数据
    skywalking中后端collect采样率的设置
    洛谷 P3387 【模板】缩点(Tarjan,DAG上的dp)
    洛谷 P1613 跑路(Floyd,倍增)&& 【模板】 Floyd
    HEOI 2014 南园满地堆轻絮
    洛谷 P7108 移花接木
    CF594A Warrior and Archer
    CF187B AlgoRace
    浅谈vector容器的奇技淫巧
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12038442.html
Copyright © 2011-2022 走看看