springboot整合mybatis:
1、添加mybatis的pom文件:
org.mybatis.spring.boot
mybatis‐spring‐boot‐starter
1.3.1
2、配置数据库相关属性
3、使用MapperScan批量扫描所有的Mapper接口;
@MapperScan(value = “com.xxx.springboot.mapper”)
4、编写mybatis配置类,代码如下:
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
5、配置yml文件的mybatis信息
6、编写控制器类,代码如下:
@RestController
public class DeptController {
@Autowired
DepartmentMapper departmentMapper;
@Autowired
EmployeeMapper employeeMapper;
@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;
}
@GetMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id){
return employeeMapper.getEmpById(id);
}
}
7、这个是getEmp表返回数据
{
“id”: 1,
“lastName”: “你很好”,
“gender”: 1,
“email”: “zmw1805@163.com”,
“dId”: 1
}