集成参考官方链接:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
SpringBoot基本依赖
<!-- 1. 继承父项目,里面有所需的各种版本-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<!-- 2. web项目依赖,里面已经集成了各种SpringMVC相关的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 只需在编译期间有即可scope设置成provided-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- 当模型文件里面属性修改时,配置文件里面会有提示信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- 页面模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springBoot_mybatis</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- 说明资源的位置(哪些东西算是资源) -->
<resources>
<!-- resources文件里面的内容要进行打包 -->
<resource>
<directory>src/main/resources</directory>
</resource>
<!-- java文件里面的内容要进行打包 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
SpringBoot中集成mybatis所需依赖
<!-- SpringBoot集成MyBatis所需的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- 使用MySQL数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池使用druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
application.yml文件信息
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/customer_test?serverTimezone=UTC
druid:
initial-size: 5
max-active: 10
mybatis:
# mapper映射文件的位置
# mapper-locations: classpath:/mappers/*.xml
# 类设置别名,默认是类名首字母小写
type-aliases-package: com.lff.domain
# mybatis核心配置文件位置
# config-location: classpath:mybatis-config.xml
#配置,如开启驼峰等
# configuration:
# map-underscore-to-camel-case: true
# use-generated-keys: true
数据源相关的配置可以参考:
- datasource相关
- 连接池Druid配置,参考DruidDataSourceWrapper
Mybatis相关配置可以参考MybatisProperties
入口文件编写
@SpringBootApplication //标识这个是Springboot的启动类
@MapperScan("com.lff.dao") //扫描dao生成代理对象
public class Application {
public static void main(String[] args) {
//可以理解固定写法,启动
SpringApplication.run(Application.class,args);
}
}
一定要有@MapperScan(“com.lff.dao”)生成Dao的代理对象。
MyBatis核心配置文件有3种编写方式
a) xml的方式,在application.yml文件里面指定配置文件的位置
mybatis:
config-location: classpath:mybatis-config.xml
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 注意 XML 头部的声明,它用来验证 XML 文档的正确性。-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 其他设置 -->
<settings>
<!-- 使用新生成记录的主键 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 数据库:开启驼峰标识 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
b) 注解的方式,参考:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
@Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer customizer() {
return (configuration) -> {
configuration.setMapUnderscoreToCamelCase(true);
configuration.setUseGeneratedKeys(true);
};
}
}
c)直接在application.yml文件中配置
mybatis:
#配置,如开启驼峰等
configuration:
map-underscore-to-camel-case: true
use-generated-keys: true
业务逻辑和以前一样写Controller层、Service层、Dao层、mapper文件就可以啦。