Mybatis使用总结 混用
1.Mybatis注解版
1.已经具备 bean:
Department
Employee
数据表
数据库连接成功
2.步骤:
2.1 编写操作每一个表对应的 mapper 接口 (最好写一个mapper包)
例如:
public interface DepartmentMapper {
}
2.2 接口所需要的注解为:@Mapper 指定这是一个操作数据库的Mapper
2.3 定义方法 操作数据库表
2.4 直接通过注解标明方法完成语句
例如:
@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);
@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);
}
2.5 编写Controller进行测试 其中注入mapper文件
例如:
@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;
}
}
2.6 不需要做配置 MybatisAutoConfiguration
2.7 可以在语句中 定义主键
例如:
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(department_name) values(#{departmentName})")
public int insertDept(Department department);
2.8 驼峰命名法 配置
"insert into department(department_name) values(#{departmentName})"
"insert into department(departmentName) values(#{departmentName})"
bean中 private String departmentName;
如何对应
解决方案:自定义Mybatis配置规则
MybatisConfig
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
@Override
public void customize(
Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
2.9 Mapper包中 mapper文件太多 每一个添加@Mapper太费事
解决:
使用MapperScan批量扫描所有的Mapper接口;
@MapperScan(value = "com.terry.mybatisdemo.mapper")
可以写在启动类上
2.Mybatis文件版
1.编写操作每一个表对应的 mapper 接口 (最好写一个mapper包)
例如:
public interface EmployeeMapper {
}
2.定义方法
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
public void insertEmp (Employee employee);
}
3.在resources文件下建立 Mybatis文件夹 Mybatis文件夹下创建 mapper文件夹
参考文档:https://mybatis.org/mybatis-3/getting-started.html
3.1 在resources文件下建立全局配置文件 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>
3.2 在mapper文件下 创建sql映射文件 对应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.terry.mybatisdemo.mapper.EmployeeMapper">
<select id="getEmpById" resultType="com.terry.mybatisdemo.bean.Employee">
select * from employee where id = #{id}
</select>
<select id="insertEmp">
insert into employee(lastName,email,gender,d_id) values (#{lastName},#{email},#{gender},#{d_id})
</select>
</mapper>
在sql映射文件內 绑定接口 绑定返回值
3.3 在 application.yml 配置 全局配置文件 sql映射文件
例如:
mybatis:
# 指定全局配置文件位置
config-location: classpath:mybatis/mybatis-config.xml
# 指定sql映射文件位置
mapper-locations: classpath:mybatis/mapper