导包
mybatis-generator-core-1.3.5.jar
配置generator.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>
<!--从本地获取数据库jar包--> <classPathEntry location="C://Users/pc/Desktop/第一次项目/mysql-connector-java-5.1.47-bin.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis?useSSL=false" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--构建实体类--> <javaModelGenerator targetPackage="com.zhiyou100.xf.bean" targetProject="./src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--构建sql映射--> <sqlMapGenerator targetPackage="com.zhiyou100.xf.mapper" targetProject="./resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--构建接口调用--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou100.xf.dao" targetProject="./src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--选择表生成实体类,false可生成简单sql--> <table schema="DB2ADMIN" tableName="users" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <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>
执行
public class Test { public static void main(String[] args) throws SQLException, IOException, InterruptedException, XMLParserException, InvalidConfigurationException { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("generator.xml");//配置的generator文件 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); } }