创建一个普通的java类
导入三个jar包
在项目下创建一个generatorConfig.xml文件 里面内容如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <!-- <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />--> 8 9 <context id="DB2Tables" targetRuntime="MyBatis3"> 10 <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" 11 connectionURL="jdbc:mysql://localhost:3306/test" 12 userId="root" 13 password="ych521mm"> 14 </jdbcConnection> 15 16 <javaTypeResolver> 17 <property name="forceBigDecimals" value="false"/> 18 </javaTypeResolver> 19 20 <!-- 生成实体类 --> 21 <javaModelGenerator targetPackage="com.ujy.pojo" targetProject=".\src"> 22 <property name="enableSubPackages" value="true"/> 23 <property name="trimStrings" value="true"/> 24 </javaModelGenerator> 25 26 <!-- 生成映射文件 --> 27 <sqlMapGenerator targetPackage="com.ujy.mapper" targetProject=".\src"> 28 <property name="enableSubPackages" value="true"/> 29 </sqlMapGenerator> 30 31 <!-- 生成接口 --> 32 <javaClientGenerator type="XMLMAPPER" targetPackage="com.ujy.mapper" targetProject=".\src"> 33 <property name="enableSubPackages" value="true"/> 34 </javaClientGenerator> 35 36 <table tableName="user" domainObjectName="User"/> 37 <table tableName="person" domainObjectName="Person"/> 38 </context> 39 </generatorConfiguration>
在建一个生成类 随意就行
里面建造main方法
内容如下
1 public class Generator { 2 public static void main(String[] args) throws Exception { 3 List<String> warnings = new ArrayList<String>(); 4 boolean overwrite = true; 5 File configFile = new File("generatorConfig.xml"); 6 ConfigurationParser cp = new ConfigurationParser(warnings); 7 Configuration config = cp.parseConfiguration(configFile); 8 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 9 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 10 myBatisGenerator.generate(null); 11 } 12 }
测试即可直接生成