使用Mybatis插件generator自动生成代码
使用MyBatis插件generator通过连接数据库自动生成实体类,Mapper.xml,dao等文件
1、pom引用包
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!-- 这个包是写了分页的工具包--> <dependency> <groupId>yhqutil</groupId> <artifactId>yhqutil</artifactId> <version>1.0</version> </dependency> <!-- 编译配置--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/mybatis/generatorConfig.xml</configurationFile> <verbose>false</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <dependency> <groupId>yhqutil</groupId> <artifactId>yhqutil</artifactId> <version>1.0</version> </dependency> </dependencies> </plugin>
2、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> <context id="my" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <plugin type="com.yhq.util.MySQLLimitPlugin"></plugin> <commentGenerator> <property name="suppressDate" value="false"/>
//重复生成 <property name="suppressAllComments" value="false"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/yhqdb?useSSL=false&serverTimezone=UTC" userId="yhq" password="yhq" ></jdbcConnection> <javaModelGenerator targetPackage="com.MultipleDataSource.entities" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- sqlMapGenerator标签是生成mapper.xml --> <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- javaClientGenerator标签是生成dao --> <javaClientGenerator targetPackage="com.MultipleDataSource.mapper" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>--> <table tableName="kpoint" domainObjectName="KnowledgePoint"> <!--<columnRenamingRule searchString="^D_" replaceString=""/>--> </table> </context> </generatorConfiguration>
3、maveny启动generator命令
mvn mybatis-generator:generate
以上内容原创来自每特学院,每特学员学习并整理!
给学院打波广告:
http://www.mayikt.com/