mybatis generator http://www.mybatis.org/generator/
1、首先要引用jar包
<!-- MyBatis Generator Core--> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency>
2、添加一个配置文件mgb.xml(MBG配置(mybatis generator简称))
http://www.mybatis.org/generator/configreference/xmlconfig.html
<?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> <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver" connectionURL="jdbc:db2:TEST" userId="db2admin" password="db2admin"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="test.model" targetProject="MBGTestProjectsrc"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="test.xml" targetProject="MBGTestProjectsrc"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="MBGTestProjectsrc"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" > <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
mbg.xml文件是对红色框框里的内容进行修改路径
最后一个修改table
去掉注释
事例
<!--如果不配置这个,生成的文件会有很多的注释,不方便看代码--> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator>
最后修改后的mbg.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> <!--导入的类什么的,我们不需要,直接删除--> <!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />--> <context id="DB2Tables" targetRuntime="MyBatis3"> <!--如果不配置这个,生成的文件会有很多的注释,不方便看代码--> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--配置数据库连接--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm-crud" userId="root" password="123456"> </jdbcConnection> <!--配置数据库连接--> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--JAVA模型生成,指定javabean生成位置--> <javaModelGenerator targetPackage="com.dotcore.model" targetProject=".srcmainjava"> <!--JAVA模型生成,指定javabean生成位置--> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--指定sql映射文件生成位置--> <sqlMapGenerator targetPackage="mapper" targetProject=".srcmain esources"> <!--指定sql映射文件生成位置--> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--指定dao生成的位置,mapper接口--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.dotcore.dao" targetProject=".srcmainjava"> <!--指定dao生成的位置,mapper接口--> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--table指定表的生成策略--> <table tableName="tbl_emp" domainObjectName="Employee"></table> <table tableName="tbl_dept" domainObjectName="Department"></table> </context> </generatorConfiguration>
配置写好后,最后就是生成文件了
点击java program
package com.dotcore.test; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @author xuerr * @version 2017/5/3 */ public class MBGTest { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("mbg.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } }
run后,就会在相应的位置生成需要的文件