【Springboot】springboot中使用mybatis操作数据库
新建springboot项目的时候,选择好web,mybatis,JDBC
在application.properties或者application.yml中配置
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/studentManagerBoot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC username: root password:
Student的实体类:
package com.hj.studentmanagerboot.user.entity; public class Student { private int stuId; private int stuNum; private String stuName; private String stuBirthday; private int stuEnterYear; private int stiState; @Override public String toString() { return "Student{" + "stuId=" + stuId + ", stuNum=" + stuNum + ", stuName=" + stuName + ", stuBirthday='" + stuBirthday + '\'' + ", stuEnterYear=" + stuEnterYear + ", stiState=" + stiState + '}'; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public int getStuNum() { return stuNum; } public void setStuNum(int stuNum) { this.stuNum = stuNum; } public String getStuBirthday() { return stuBirthday; } public void setStuBirthday(String stuBirthday) { this.stuBirthday = stuBirthday; } public int getStuEnterYear() { return stuEnterYear; } public void setStuEnterYear(int stuEnterYear) { this.stuEnterYear = stuEnterYear; } public int getStiState() { return stiState; } public void setStiState(int stiState) { this.stiState = stiState; } }
写好在dao包中新建StudentMapper接口,添加getStudent(int stuId)方法
package com.hj.studentmanagerboot.user.dao; import com.hj.studentmanagerboot.user.entity.Student; import org.springframework.stereotype.Repository; @Repository public interface StudentMapper { Student getStudent(int stuId); }
在service包中新建getStudentService,并自动装配StudentMapper,添加getStudentService的方法
package com.hj.studentmanagerboot.user.service; import com.hj.studentmanagerboot.user.dao.StudentMapper; import com.hj.studentmanagerboot.user.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class StudentServiceTest { @Autowired StudentMapper studentMapper; public Student getStudent(int stuId) { return studentMapper.getStudent(stuId); } }
在controller中自动装配StudentService
package com.hj.studentmanagerboot.user.handler; import com.hj.studentmanagerboot.user.service.StudentServiceTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("testStudent") public class StudentHandlerTest { @Autowired private StudentServiceTest studentServiceTest; @RequestMapping("getUser/{id}") public String getUser(@PathVariable("id") int stuId) { return studentServiceTest.getStudent(stuId).toString(); } }
在SpringBoot的入口程序中增加mybatis的注解,扫描dao包(如果不使用扫描包的方式,可以在dao包中的每个Mapper接口前,加上@Mapper注解)
@MapperScan("com.hj.studentmanagerboot.user.dao") public class ...{ }
在Resource文件下新建mapper文件夹存放Mapper.xml,并在application.yml或application.properties中增加存放mybatis的Mapper配置文件的信息:
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.hj.studentmanagerboot.user.entity
#如果不使用type-aliases-package,则在每个实体类前加上@Component即可
在StudentMapper.xml中写入配置信息
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hj.studentmanagerboot.user.dao.StudentMapper"> <select id="getStudent" resultType="com.hj.studentmanagerboot.user.entity.Student"> select * from stuInfo where stuId = #{stuId} </select> </mapper>
最后还碰到一个这样的问题,数据中命名一般是user_name,而实体类一般是userName,如何能让查找时候,自动转化命名呢。
只需要在application.yml中加入下面的配置即可:
mybatis:
configuration:
map-underscore-to-camel-case: true