Spring Boot整合MyBatis有两种方式,即注解配置文件,这两种方式的基础环境和配置都是一样的。

基础配置准备

1、引入MyBatis依赖
新建一个SpringBoot项目,勾选需要的starter场景。

  1. <!--引入SpringBoot的MyBatis starter-->
  2. <!--该starter是MyBatis开发,用于兼容SpringBoot-->
  3. <dependency>
  4. <groupId>org.mybatis.spring.boot</groupId>
  5. <artifactId>mybatis-spring-boot-starter</artifactId>
  6. <version>1.3.1</version>
  7. </dependency>

搜狗截图20180305194443.png
2、配置数据源
配置数据源相关属性,数据源依然采用Druid,该部分的配置见上一章节,这里不再赘述。

3、创建数据库
创建数据库、数据表等,测试项目是否能连接上,创建数据表对应的Java Bean。

  1. /**
  2. * 数据表student对应的Java Bean,数据表中有id,name等字段
  3. **/
  4. public class Student {
  5. public Integer id;
  6. public String name;
  7. // 此处省略get方法、set方法、构造函数等
  8. }

方法一:注解版

Controller层

  1. @RestController
  2. public class StudentController {
  3. @Autowired
  4. public StudentMapper studentMapper;
  5. @GetMapping("/student/{id}")
  6. public Student getSudent(@PathVariable("id") Integer id){
  7. return studentMapper.getStudentById(id);
  8. }
  9. @GetMapping("/insertStudent")
  10. public Student insertStudent(Student student){
  11. studentMapper.insertStudent(student);
  12. return student;
  13. }
  14. }

Mapper层

  1. // 指定这是一个操作数据库的mapper
  2. @Mapper
  3. public interface StudentMapper {
  4. @Select("select * from student where id=#{id}")
  5. public Student getStudentById(Integer id);
  6. @Delete("delete from student where id=#{id}")
  7. public int deleteStudentById(Integer id);
  8. @Options(useGeneratedKeys = true,keyProperty = "id")
  9. @Insert("insert into student(name) values(#{name})")
  10. public int insertStudent(Student student);
  11. @Update("update student set name=#{name} where id=#{id}")
  12. public int updateStudent(Student student);
  13. }

Configuration 配置

  1. // 自定义MyBatis配置规则,给容器中添加一个 ConfigurationCustomizer
  2. @Configuration
  3. public class MyBatisConfig {
  4. @Bean
  5. public ConfigurationCustomizer configurationCustomizer(){
  6. return new ConfigurationCustomizer(){
  7. @Override
  8. public void customize(Configuration configuration) {
  9. configuration.setMapUnderscoreToCamelCase(true);
  10. }
  11. };
  12. }
  13. }

Spring Boot启动类

  1. // 使用MapperScan批量扫描所有的Mapper接口
  2. @MapperScan(value = "com.gmd.springboot.mapper")
  3. @SpringBootApplication
  4. public class SpringBootMyBatis0421Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(SpringBootMyBatis0421Application.class, args);
  7. }
  8. }

方法二:配置文件版

  1. mybatis:
  2. config-location: classpath:mybatis/mybatis-config.xml ## 指定全局配置文件的位置
  3. mapper-locations: classpath:mybatis/mapper/*.xml ## 指定sql映射文件的位置
<!--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>
<!--StudentMapper.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.zzz.springboot.mapper.StudentMapper">

    <select id="getStudent" resultType="com.gmd.springboot.Student">
        SELECT * FROM student WHERE id=#{id}
    </select>

    <insert id="insertStudent">
        INSERT INTO student(name) VALUES (#{name})
    </insert>
</mapper>

更多使用参照:
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

https://mybatis.org/mybatis-3/zh/index.html