1、引入jar包java
<!--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>
2、引入generator插件spring
<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>
3、修改屬性文件sql
#社區
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
4、引入 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>
5、exclipse啓動 生成代碼api
右鍵點擊項目名->run as ->maven build, 在main->goals 輸入 mybatis-generator:generatemybatis
生成結果以下圖所示:app