1)引入依賴java
<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
引入配置mysql
# 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)整合MyBatisgit
tk.mybatis是在MyBatis的基礎上提供了不少工具,讓開發更加高效github
<!-- 整合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分頁插件web
PageHelper是MyBatis的分頁插件,支持多數據源,多數據庫,能夠簡化數據庫的分頁查詢操做,整合過程極其簡單,只須要引入依賴便可。spring
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency>
5)配置逆向工程代碼自動生成sql
<!-- 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==apache
<?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>