SpringBoot整合tk.mybatis及pageHelper分页插件及mybatis逆
向⼯程
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.sql.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.st-on-borrow=true
3)整合MyBatis
添加依赖
<!-- 整合tk.mybatis -->
<dependency>
<groupId&batis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
application.properties中添加mybatis的配置
mybatis.mapper-locations=classpath:mapper/*.xml
创建通⽤的⽗级接⼝
batis;
batis.mappermon.Mapper;
batis.mappermon.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)配置逆向⼯程代码⾃动⽣成
1. 引⼊依赖插件
<!-- MyBatis 逆向⼯程插件配置start -->
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/l
</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&batis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
</plugin><!-- MyBatis 逆向⼯程插件配置end -->
2. 代码⽣成器配置⽂件l
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/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.ator.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="ity" targetProject="src/main/java" />
<!-- 配置l⽂件存放路径 -->
<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>
3. jdbc.properties
jdbc.sql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web13?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123
4. 插件⾃动成命令
mvn mybatis-generator:generate
在STS中,项⽬右键-Run as->->Goals处输⼊:mybatis-generator:generate ,run即可整合mybatis、分页插件、逆向⼯程及druid数据源完整l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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&batis</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&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/l </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&batis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
</plugin><!-- MyBatis 逆向⼯程插件配置end -->
</plugins>
</build>
</project>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论