IDEA MyBatis Generator 生成代码
Generator的xml详细说明见:http://mybatis.org/generator/configreference/xmlconfig.html
中文:http://mbg.cndocs.tk/index.html
Maven pom.xml加入如下配置:
<plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin> </plugins>
记得一定要把以上代码放在<build></build> 中,之前放在外面,调了好长时间不知道什么原因,如下图
然后IDEA中出现如下插件
配置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 resource="init.properties"/> <!-- 指定数据连接驱动jar地址 --> <classPathEntry location="${classPath}"/> <!-- 一个数据库一个context --> <context id="my" targetRuntime="MyBatis3"> <!-- 注释 --> <commentGenerator> <property name="suppressDate" value="true"/><!-- 是否生成注释代时间戳--> <property name="suppressAllComments" value="false"/><!-- 是否取消注释 --> </commentGenerator> <!-- jdbc连接 --> <jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"/> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com.egan.model" targetProject="D:/workspace/firstMavenWeb/src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="com.egan.mapping" targetProject="D:/workspace/firstMavenWeb/src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com.egan.dao" targetProject="D:/workspace/firstMavenWeb/src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>--> <!-- 配置表信息 --> <table schema="sharrygo" tableName="custom_acc" domainObjectName="CustomAcc" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="sharrygo" tableName="toy_cars" domainObjectName="ToyCars" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
配置init.properties
#Mybatis Generator configuration classPath=E:/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar driver=com.mysql.jdbc.Driver url=jdbc:mysql://10.2.8.***:***/*** username=*** password=***
然后在maven中双击如下图生成代码
代码生成如下: