1)引入mybatis-generator的jar包
2)在工程目录下新建generator.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 <generatorConfiguration> 6 <!-- 数据库驱动jar包所在的位置 --> 7 <classPathEntry location="D:\mybatis\generator830\lib\mysql-connector-java-5.1.47.jar" /> 8 <context id="DB2Tables" targetRuntime="MyBatis3"> 9 <!-- 去除注释 --> 10 <commentGenerator> 11 <property name="suppressAllComments" value="true" /> 12 </commentGenerator> 13 <!-- 数据源信息 --> 14 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 15 connectionURL="jdbc:mysql://localhost:3306/mybatis" 16 userId="root" 17 password="root"> 18 </jdbcConnection> 19 <javaTypeResolver > 20 <property name="forceBigDecimals" value="false" /> 21 </javaTypeResolver> 22 <!-- 生成的实体类所在的位置 --> 23 <javaModelGenerator targetPackage="com.zhiyou.clg.bean" targetProject="./src"> 24 <property name="enableSubPackages" value="true" /> 25 <property name="trimStrings" value="true" /> 26 </javaModelGenerator> 27 <!-- 生成的映射文件所在的位置 --> 28 <sqlMapGenerator targetPackage="com.zhiyou.clg.mapper" targetProject="./resource"> 29 <property name="enableSubPackages" value="true" /> 30 </sqlMapGenerator> 31 <!-- 生成的dao所在的位置 --> 32 <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou.clg.dao" targetProject="./src"> 33 <property name="enableSubPackages" value="true" /> 34 </javaClientGenerator> 35 <!-- 表和实体类的对象关系 36 schema:该表所在的数据库 37 tableName:表名 38 domainObjectName:实体类名 39 --> 40 <table schema="DB2ADMIN" tableName="user" domainObjectName="User" 41 enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" > 42 <property name="useActualColumnNames" value="true"/> 43 <generatedKey column="ID" sqlStatement="DB2" identity="true" /> 44 <columnOverride column="DATE_FIELD" property="startDate" /> 45 <ignoreColumn column="FRED" /> 46 <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> 47 </table> 48 49 </context> 50 </generatorConfiguration>
3)在src目录下新建test包,再新建Test类,在main下复制官网内容,导包后运行
1 package com.zhiyou.clg.test; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.mybatis.generator.api.MyBatisGenerator; 9 import org.mybatis.generator.config.Configuration; 10 import org.mybatis.generator.config.xml.ConfigurationParser; 11 import org.mybatis.generator.exception.XMLParserException; 12 import org.mybatis.generator.internal.DefaultShellCallback; 13 14 public class TestGenerator { 15 public static void main(String[] args) throws Exception{ 16 List<String> warnings = new ArrayList<String>(); 17 boolean overwrite = true; 18 File configFile = new File("generator.xml"); 19 ConfigurationParser cp = new ConfigurationParser(warnings); 20 Configuration config = cp.parseConfiguration(configFile); 21 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 22 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 23 myBatisGenerator.generate(null); 24 } 25 }