集成参考官方链接:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

SpringBoot基本依赖

  1. <!-- 1. 继承父项目,里面有所需的各种版本-->
  2. <parent>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <groupId>org.springframework.boot</groupId>
  5. <version>2.3.4.RELEASE</version>
  6. </parent>
  7. <dependencies>
  8. <!-- 2. web项目依赖,里面已经集成了各种SpringMVC相关的依赖-->
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. <!-- 热部署 -->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-devtools</artifactId>
  17. </dependency>
  18. <!-- 只需在编译期间有即可scope设置成provided-->
  19. <dependency>
  20. <groupId>org.projectlombok</groupId>
  21. <artifactId>lombok</artifactId>
  22. <scope>provided</scope>
  23. </dependency>
  24. <!-- 当模型文件里面属性修改时,配置文件里面会有提示信息-->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-configuration-processor</artifactId>
  28. </dependency>
  29. <!-- 页面模板-->
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  33. </dependency>
  34. </dependencies>
  35. <build>
  36. <finalName>springBoot_mybatis</finalName>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. </plugin>
  42. </plugins>
  43. <!-- 说明资源的位置(哪些东西算是资源) -->
  44. <resources>
  45. <!-- resources文件里面的内容要进行打包 -->
  46. <resource>
  47. <directory>src/main/resources</directory>
  48. </resource>
  49. <!-- java文件里面的内容要进行打包 -->
  50. <resource>
  51. <directory>src/main/java</directory>
  52. <includes>
  53. <include>**/*.properties</include>
  54. <include>**/*.xml</include>
  55. </includes>
  56. </resource>
  57. </resources>
  58. </build>

SpringBoot中集成mybatis所需依赖

  1. <!-- SpringBoot集成MyBatis所需的依赖-->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>2.1.3</version>
  6. </dependency>
  7. <!-- 使用MySQL数据库-->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. </dependency>
  12. <!-- 连接池使用druid-->
  13. <dependency>
  14. <groupId>com.alibaba</groupId>
  15. <artifactId>druid-spring-boot-starter</artifactId>
  16. <version>1.2.1</version>
  17. </dependency>

application.yml文件信息

  1. spring:
  2. datasource:
  3. type: com.alibaba.druid.pool.DruidDataSource
  4. driver-class-name: com.mysql.cj.jdbc.Driver
  5. username: root
  6. password: root
  7. url: jdbc:mysql://localhost:3306/customer_test?serverTimezone=UTC
  8. druid:
  9. initial-size: 5
  10. max-active: 10
  11. mybatis:
  12. # mapper映射文件的位置
  13. # mapper-locations: classpath:/mappers/*.xml
  14. # 类设置别名,默认是类名首字母小写
  15. type-aliases-package: com.lff.domain
  16. # mybatis核心配置文件位置
  17. # config-location: classpath:mybatis-config.xml
  18. #配置,如开启驼峰等
  19. # configuration:
  20. # map-underscore-to-camel-case: true
  21. # use-generated-keys: true

数据源相关的配置可以参考:

  • datasource相关

Snipaste_2021-12-05_13-01-06.png
Snipaste_2021-12-05_13-01-24.png

  • 连接池Druid配置,参考DruidDataSourceWrapper

image.png

Mybatis相关配置可以参考MybatisProperties
Snipaste_2021-12-05_13-09-33.png

入口文件编写

  1. @SpringBootApplication //标识这个是Springboot的启动类
  2. @MapperScan("com.lff.dao") //扫描dao生成代理对象
  3. public class Application {
  4. public static void main(String[] args) {
  5. //可以理解固定写法,启动
  6. SpringApplication.run(Application.class,args);
  7. }
  8. }

一定要有@MapperScan(“com.lff.dao”)生成Dao的代理对象。

MyBatis核心配置文件有3种编写方式

a) xml的方式,在application.yml文件里面指定配置文件的位置

  1. mybatis:
  2. config-location: classpath:mybatis-config.xml

mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!-- 注意 XML 头部的声明,它用来验证 XML 文档的正确性。-->
  3. <!DOCTYPE configuration
  4. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  5. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  6. <configuration>
  7. <!-- 其他设置 -->
  8. <settings>
  9. <!-- 使用新生成记录的主键 -->
  10. <setting name="useGeneratedKeys" value="true"/>
  11. <!-- 数据库:开启驼峰标识 -->
  12. <setting name="mapUnderscoreToCamelCase" value="true"/>
  13. </settings>
  14. </configuration>

b) 注解的方式,参考:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

  1. @Configuration
  2. public class MyBatisConfig {
  3. @Bean
  4. public ConfigurationCustomizer customizer() {
  5. return (configuration) -> {
  6. configuration.setMapUnderscoreToCamelCase(true);
  7. configuration.setUseGeneratedKeys(true);
  8. };
  9. }
  10. }

c)直接在application.yml文件中配置

  1. mybatis:
  2. #配置,如开启驼峰等
  3. configuration:
  4. map-underscore-to-camel-case: true
  5. use-generated-keys: true

业务逻辑和以前一样写Controller层、Service层、Dao层、mapper文件就可以啦。