mybatis 官网:https://mybatis.org/mybatis-3/zh/index.html
mybatis 配置:https://mybatis.org/mybatis-3/zh/configuration.html#settings
maven repository:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter
整合 mybatis 有2种方式,可以使用纯手动配置版,也可以使用注解配置混合版。
测试用数据表

1、配置版
1.1、引入场景启动器
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency>
1.2、声明 Bean
import lombok.Data;@Datapublic class Depart {private String id;private String departname;private String org_code;private String org_type;}

1.3、声明 Mapper 接口
@Mapperpublic interface DepartMapper {public Depart getDepart(String id);}

1.4、配置文件
指定全局配置文件位置、SQL映射文件位置。
application.yml
mybatis:config-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mybatis/mapper/*.xmlconfiguration:# map-underscore-to-camel-case: true
mybatis-config.xml
全局配置文件模板
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- <settings>--><!-- 开启驼峰命名规则--><!-- <setting name="mapUnderscoreToCamelCase" value="true"/>--><!-- </settings>--></configuration>
DepartMapper.xml
SQL映射文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.admin.mapper.DepartMapper"><select id="getDepart" resultType="com.example.admin.bean.Depart">select *from t_s_departwhere id = #{id}</select></mapper>
1.5、编写 service
@Servicepublic class DepartService {@AutowiredDepartMapper departMapper;public Depart getById(String id) {return departMapper.getDepart(id);}}
1.6、编写 controller
@Controller@Slf4jpublic class IndexController {@AutowiredDepartService departService;@ResponseBody@GetMapping(path = "/depart")public Depart getById(@RequestParam("id") String id) {return departService.getById(id);}}
1.7、测试效果
访问:
http://127.0.0.1/depart?id=402881e5751011b80175102f1e730002
1.8、一些优化
建议在 application.yml 文件中配置 mybatis 设置来代替 mybatis-config.xml 全局配置文件。
application.yml
mybatis:# config-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mybatis/mapper/*.xmlconfiguration:# map-underscore-to-camel-case: true
2、注解版
在配置版中,有一个数据表的 SQL 映射文件
DepartMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.admin.mapper.DepartMapper"><select id="getDepart" resultType="com.example.admin.bean.Depart">select *from t_s_departwhere id = #{id}</select></mapper>
可以在定义 Mapper 接口时使用 @Select 注解,
@Mapperpublic interface DepartMapper {@Select("select * from t_s_depart where id = #{id}")public Depart getById(String id);}
3、混合版
两种模式都可以混合使用
最佳实战
- 引入mybatis-starter
- 配置application.yaml中,指定mapper-location位置即可
- 编写Mapper接口并标注@Mapper注解
- 简单方法直接注解方式
- 复杂方法编写mapper.xml进行绑定映射
- @MapperScan(“com.atguigu.admin.mapper”) 简化,其他的接口就可以不用标注@Mapper注解
