Spring Boot 整合 MyBatis:
1,导入 mybatis-spring-boot-starter 依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2,配置数据源相关属性:Spring Boot 整合Druid配置数据源监控
3,给数据库建表:
spring:
datasource:
schema:
- classpath:sql/department.sql
- classpath:sql/employee.sql
4,运行项目,确定表已经创建成功!然后将schema属性注释掉(避免项目运行重复创建覆盖数据):
5,创建JavaBean:
public class Employee {
private Integer id;
private String lastName;
private Integer gender;
private String email;
private Integer dId;
public void setId(Integer id) {
this.id = id;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public void setEmail(String email) {
this.email = email;
}
public void setdId(Integer dId) {
this.dId = dId;
}
public Integer getId() {
return id;
}
public String getLastName() {
return lastName;
}
public Integer getGender() {
return gender;
}
public String getEmail() {
return email;
}
public Integer getdId() {
return dId;
}
}
public class Department {
private Integer id;
private String departmentName;
public void setId(Integer id) {
this.id = id;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public Integer getId() {
return id;
}
public String getDepartmentName() {
return departmentName;
}
}
6,创建一个操作数据库的mapper接口加上:@Mapper(或者在项目启动类上加上:@MapperScan(value = "com.atguigu.springboot.mapper"),可以使用MapperScan批量扫描所有的Mapper接口)
//指定这是一个操作数据库的mapper
@Mapper
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);
//是否使用自动生成的主键,哪个属性是封装主键的;刚插入的数据id会封装到department对象返回
@Options(useGeneratedKeys = true,keyProperty = "id")//否则刚插入的数据id显示为null
@Insert("insert into department(departmentName) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
or
7,编写一个测试用的Controller
@RestController
public class DeptController {
@Autowired
DepartmentMapper departmentMapper;
@GetMapping("/dept/{id}")
public Department getDepartment(@PathVariable("id") Integer id){
return departmentMapper.getDeptById(id);
}
@GetMapping("/dept")
public Department insertDept(Department department){
departmentMapper.insertDept(department);
return department;
}
}
8,在地址栏插入数据:localhost:8080/dept?departmentName=AA
OK,整合完成了!
将数据库属性大写 N 改成 _n 后,该属性就封装不上了,因为JavaBean里还是大写的N:
9,自定义MyBatis的配置规则;给容器中添加一个ConfigurationCustomizer:
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
@Override
public void customize(Configuration configuration) {
//开启驼峰命名法规则
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
10,然后在访问:localhost:8080/dept/3,开启驼峰命名法规则后,又可以封装对应的数据了!