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);
    }
  • 相关阅读:
    原生JS实现几个常用DOM操作API
    js字符串操作总结
    复选框 全选 反选 不选
    纯js实现分页
    JS 数据类型转换
    原生JS 购物车及购物页面的cookie使用
    js实现两种实用的排序算法——冒泡、快速排序
    linux下rename命令使用(可以实现文件批量重名)
    screen命令常用参数使用
    iptables端口转发规则(内网端口转外网端口)
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12038442.html
Copyright © 2011-2022 走看看