一、引入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
生成结果如下图所示: