本文章主要记录Spring Boot 数据整合MyBatis的关键步骤
一、整合注解版的Mybatis
1、pom文件引入对应的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
2、指定一个数据库的mapper,@Options(useGeneratedKeys = true,keyProperty = "id")代表的是表明主键,主键自增
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(department_name) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set department_name=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
3、编写配置类
@org.springframework.context.annotation.Configuration
public class MybatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
4、编写对应的Controller来做测试,这里需要注意当方法传入的参数为对象时候,浏览器上的URL传入的参数会自动对应到对象的属性
例如:http://localhost:8087/dept?departmentName=AA
departmentName为Department 的属性
@RestController
public class DeptController {
@Autowired
DepartmentMapper departmentMapper;
@GetMapping("/dept/{id}")
public Department getDepartment(@PathVariable("id") Integer id){
Department department = departmentMapper.getDeptById(id);
return department;
}
@GetMapping("/dept")
public Department insertDepartment(Department department){
departmentMapper.insertDept(department);
return department;
}
}
二、整合配置版MyBatis
1、添加配置文件
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
mybatis-config.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
这里需要注意mybatis的配置文件mybatis-config.xml不能和mapper文件放在一起
2、建立对应的对象类
public class Employee {
private Integer id;
private String lastName;
private Integer gender;
private String email;
private Integer dID;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getdID() {
return dID;
}
public void setdID(Integer dID) {
this.dID = dID;
}
}
3、新建mapper类
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
public void insertEmp(Employee employee);
}
4、编写mapper.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.example.springboot.mapper.EmployeeMapper">
<!--public Employee getEmpById(Integer id);
public void insertEmp(Employee employee);
-->
<select id="getEmpById" resultType="com.example.springboot.bean.Employee">
SELECT * FROM employee WHERE id=#{id}
</select>
<insert id="insertEmp">
INSERT INTO employee(lastName,email,gender,d_id) VALUES (#{lasrName},#{email},#{gender},#{did})
</insert>
</mapper>
5、编写Controller测试
@RestController
public class DeptController {
@Autowired
EmployeeMapper employeeMapper;
@GetMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id){
return employeeMapper.getEmpById(id);
}
}