Spring Boot整合druid数据源
1)引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2)application.properties
引入配置
# datasource config
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
spring.datasource.druid.username=root
spring.datasource.druid.password=123
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
3)整合MyBatis
tk.mybatis是在MyBatis的基础上提供了很多工具,让开发更加高效
- 添加依赖
<!-- 整合tk.mybatis -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
application.properties
中添加mybatis的配置
mybatis.type-aliases-package=com.fei.entity
mybatis.mapper-locations=classpath:mapper/*.xml
- 创建通用的父级接口
package tk.mybatis;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* 自己的Mapper,特别注意不能被扫描到,否则会出错
* Created by zxf on 2019年10月16日
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T>{
}
4)整合PageHelper分页插件
PageHelper是MyBatis的分页插件,支持多数据源,多数据库,可以简化数据库的分页查询操作,整合过程极其简单,只需要引入依赖即可。
- 引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
5)配置逆向工程代码自动生成
- 引入依赖插件
<!-- MyBatis 逆向工程 插件配置start -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
<verbose>true</verbose><!-- 允许移动生成的文件 -->
<overwrite>true</overwrite><!-- 是否覆盖 -->
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
</plugin><!-- MyBatis 逆向工程 插件配置end -->
- 代码生成器配置文件
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 引入 jdbc.properties数据库连接配置 -->
<properties resource="jdbc.properties" />
<!-- MyBatis3Simple:不生成 Example相关类及方法 -->
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<!-- 指定生成 Mapper 的继承模板 tk.mybatis插件 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.MyMapper" />
</plugin>
<!-- jdbc 数据库连接配置 -->
<jdbcConnection
driverClass="${jdbc.driver-class-name}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!-- 配置数据库表实体类存放路径 -->
<javaModelGenerator targetPackage="com.fei.entity" targetProject="src/main/java" />
<!-- 配置XxxMapper.xml文件存放路径 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" />
<!-- 配置XxxMapper实体接口存放路径 -->
<javaClientGenerator
targetPackage="com.fei.dao"
targetProject="src/main/java" type="XMLMAPPER" />
<!-- 配置需要生成的表 -->
<!-- tableName:数据库表名,%代表所有,domainObjectName:生成文件名 ,schema:数据源 -->
<table tableName="tb_item">
<generatedKey column="id" sqlStatement="Mysql" identity="true" />
</table>
</context>
</generatorConfiguration>
jdbc.properties
jdbc.driver-class-name=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web13?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123
- 插件自动成命令
mvn mybatis-generator:generate
在STS中,项目右键-Run as->Maven Build...->Goals处输入:mybatis-generator:generate ,run即可
整合mybatis、分页插件、逆向工程及druid数据源完整pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.fei</groupId>
<artifactId>spring-boot-mybatis-druid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mybatis-druid</name>
<description>My project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 整合数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 整合tk.mybatis及分页插件 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- MyBatis 逆向工程 插件配置start -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
<verbose>true</verbose><!-- 允许移动生成的文件 -->
<overwrite>true</overwrite><!-- 是否覆盖 -->
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
</plugin><!-- MyBatis 逆向工程 插件配置end -->
</plugins>
</build>
</project>