IDEA Springboot 整合Mybatis 完整教程系列 02
阿新 • • 發佈:2019-01-27
02 配置mybatis,利用自動生成外掛,生產與資料庫表對應的
mapper介面,和xml檔案
(1)設定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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> <!-- mybatis generator 自動生成程式碼外掛 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <!--generator配置檔案的位置--> <configurationFile>${basedir}/src/main/resources/mybatis-generator-config.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> <!-- 如果不新增此節點mybatis的mapper.xml檔案都會被漏掉。 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.yml</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
值得注意的是,下圖指定的是mybatis-generator-config的配置檔案路徑
還有,如下,加入目錄下的所有配置檔案,防止遺漏
(2)mybatis-generator-config.xml
其中,生成entity類,和對應的路徑如下
生成mapper介面,和對應的路徑如下
生成mapper.xml,和對應的路徑如下
完整的mybatis-generator-config.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> <!--mysql 連線資料庫jar 這裡選擇自己本地位置--> <classPathEntry location="E:\jar\mysql-connector-java-5.1.0-bin.jar" /> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/easyloan" userId="root" password="122026"> </jdbcConnection> <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer,為 true時把JDBC DECIMAL 和 NUMERIC 型別解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成entity類的位置 --> <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src/main/java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper對映檔案生成的位置 如果maven工程只是單獨的一個工程,targetProject="src/main/java" 若果maven工程是分模組的工程,targetProject="所屬模組的名稱",例如: targetProject="ecps-manager-mapper",下同--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper介面生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定資料庫表 --> <!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名 schema 重複程式碼生成--> <table schema="" tableName="user" > <!--設定自增欄位--> <generatedKey column="user_id" identity="false" sqlStatement="JDBC"/> </table> <table schema="" tableName="department" > </table> <table schema="" tableName="audit" > <!--設定自增欄位--> <generatedKey column="aud_id" identity="false" sqlStatement="JDBC"/> </table> <table schema="" tableName="employee" > <!--設定自增欄位--> <generatedKey column="emp_id" identity="false" sqlStatement="JDBC"/> </table> <table schema="" tableName="expected_repay" > <!--設定自增欄位--> <generatedKey column="exp_id" identity="false" sqlStatement="JDBC"/> </table> <table schema="" tableName="linkman" > </table> <table schema="" tableName="loan_apply" > <!--設定自增欄位--> <generatedKey column="loan_id" identity="false" sqlStatement="JDBC"/> </table> <table schema="" tableName="manager" > <!--設定自增欄位--> <generatedKey column="m_id" identity="false" sqlStatement="JDBC"/> </table> <table schema="" tableName="manage" > </table> <table schema="" tableName="overdue_times" > <!--設定自增欄位--> <generatedKey column="overdue_id" identity="false" sqlStatement="JDBC"/> </table> <table schema="" tableName="real_repay" > <!--設定自增欄位--> <generatedKey column="real_id" identity="false" sqlStatement="JDBC"/> </table> //資料庫對應的表 <table schema="" tableName="user_login" > </table> </context> </generatorConfiguration>
(3)mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!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="useColumnLabel" value="true"/> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
上述的檔案以及對應的包目錄建立好之後,就可以使用mybatis generator 外掛自動生成程式碼了
在IDEA軟體介面中,有如下,點選一下generator,即可自動生成程式碼。
如果找不到這個自帶外掛,請按如下連結操作,手動配置一個外掛。