1、注解方式
(1)注解方式用接口+注解(@Mapper)方式实现Mybatis操作数据库
@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);
@Insert("insert into department(id,departmentName) values(#{id},#{departmentName})")
public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
注:如果Mapper接口非常多且都位于相同包下,可以再SpringBoot主配置类或者Mybatis配置类上添加@MapperScan(value = "com.chen.springboot.mapper")
等同于对包下所有接口添加@Mapper
注解
(2)controller类可以直接通过@Autowired容器接口来调用其中方法来执行相应的sql语句
@RestController
public class DepartmentController {
@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;
}
}
2、xml配置文件方式
(1)创建操作数据库的接口及其方法
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
public void insertEmp(Employee employee);
}
(2)编写Mybatis配置文件(Mybatis配置文件全面托管于Github上,可以去Github的Mybatis的使用文档来查看配置文件规范)
进入Getting Started有详细的Mybatis配置过程
言归正传,继续我们的Mybatis配置文件(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></configuration>
Mapper映射文件(EmployeeMapper.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.chen.springboot.mappper.EmployeeMapper">
<select id="getEmpById" resultType="com.chen.springboot.bean.Employee">
select * from employee where id=#{id}
</select>
<insert id="insertEmp">
insert into employee(id,lastName,email,gender,d_id) values(#{id},#{lastName},#{email},#{gender},#{dId})
</insert>
</mapper>
最后在SpringBoot配置文件(application.yml)中将Mapper配置文件和Mybatis配置文件整合并在SpringBoot中生效:
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
(3)controller类可以直接通过@Autowired容器接口来调用其中方法来执行相应的sql语句
@RestController
public class EmployeeController {
@Autowired
EmployeeMapper employeeMapper;
@GetMapping("/emp/{id}")
public Employee getEmpById(@PathVariable("id")Integer id){
return employeeMapper.getEmpById(id);
}
@GetMapping("/emp")
public Employee insertEmp(Employee employee){
employeeMapper.insertEmp(employee);
return employee;
}
}
3、总结springboot开启mybatis驼峰命名自动映射的三种方式
方式一:通过springboot的配置文件(application.yml):
mybatis:
configuration:
map-underscore-to-camel-case: true
此方式是最简单的,但是要注意,通过springboot的配置文件配置mybatis的设置,则不能够再使用mybatis的配置文件,例如:下边代码中①②的两个设置不能同时存在,要么使用config-location指定mybatis的配置文件,在通过mybatis的配置文件配置相关设置,要么通过springboot配置文件的mybatis.configuration进行相关设置,二者只能选其一,否则会报错。
mybatis:
config-location: classpath:mybatis/mybatis-config.xml ①
mapper-locations: classpath:mybatis/mapper/*.xml
configuration:
map-underscore-to-camel-case: true ②
方式二:通过mybatis的配置文件
首先需要在springboot的配置文件application.yml中指定mybatis配置文件的位置。
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
然后在mybatis配置文件中进行设置
<?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>
方式三:通过@Comfiguration注解和@Bean注解,向容器中添加ConfigurationCustomizer类型的组件,在ConfigurationCustomizer中进行设置
@Configuration
public class MybatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}