springBoot整合Mybatis generator
阿新 • • 發佈:2018-11-08
一、引入jar包
<!--boot整合mybatis的包 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--mybatis程式碼生成器jar包 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <!--postgresql 資料庫聯結器 --><dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.5</version> </dependency>
二、引入generator外掛
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- mybatis generator 自動生成程式碼外掛 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build>
三、修改屬性檔案
#社群
spring.datasource.commonunity.driver-class-name=org.postgresql.Driver
spring.datasource.commonunity.url=jdbc:postgresql://****:5432/test_***
spring.datasource.commonunity.username=**
spring.datasource.commonunity.password=**
#generator
generator.location=C:/maven/pushData/org/postgresql/postgresql/42.2.5/postgresql-42.2.5.jar
四、引入 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> <!-- 引入 properties 配置檔案 --> <properties resource="application.properties"/> <!-- 資料庫連結驅動包 --> <classPathEntry location="${generator.location}" /> <context id="MySqlTables" targetRuntime="MyBatis3"> <!-- 生成的Java檔案的編碼 --> <property name="javaFileEncoding" value="UTF-8" /> <!-- 格式化java程式碼 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" /> <!-- 格式化XML程式碼 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" /> <!-- 為生成的Java模型類新增序列化介面 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <!-- 為生成的Java模型建立一個toString方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 取消註釋 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="false" /> </commentGenerator> <jdbcConnection driverClass="${spring.datasource.commonunity.driver-class-name}" connectionURL="${spring.datasource.commonunity.url}" userId="${spring.datasource.commonunity.username}" password="${spring.datasource.commonunity.password}" /> <!-- java型別處理器 用於處理DB中的型別到Java中的型別,預設使用JavaTypeResolverDefaultImpl; 注意一點,預設會先嚐試使用Integer,Long,Short等來對應DECIMAL和 NUMERIC資料型別; --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.ryj.test.multipleDataSource.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources/mapper"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.ryj.test.multipleDataSource.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 生成表並且不生成example --> <table schema="" tableName="blacklist_lib" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" /> </context> </generatorConfiguration>
五、exclipse啟動 生成程式碼
右鍵點選專案名->run as ->maven build, 在main->goals 輸入 mybatis-generator:generate
生成結果如下圖所示: