首先添加mybatis-generator-core-1.3.5.jar
然后寫配置generator文件,你可以賦值下面代碼 改下包
<?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 >
<!-- 設(shè)置mysql驅(qū)動路徑 -->
<classPathEntry location="E:\1.JAVA\1.javaCode\ssm\ssm4\WebContent/WEB-INF/lib/mysql-connector-java-5.1.39.jar" />
<!-- 此處指定生成針對MyBatis3的相關(guān)類掉弛,xml -->
<context id="context1" targetRuntime="MyBatis3">
<!-- jdbc連接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/admin?characterEncoding=utf8" userId="root" password="8954036abc!@#.." />
<!-- 生成vo對象 -->
<javaModelGenerator targetPackage="com.boolib.model" targetProject="src" >
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="com.boolib.model" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成Model類存放位置-->
<javaClientGenerator
targetPackage="com.boolib.dao"
targetProject="src" type="XMLMAPPER" />
<table tableName="user"
domainObjectName="user"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
接下來 生成utils代碼
package com.boolib.utils.generator;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
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;
public class MyBatisGeneratorUtil {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File(MyBatisGeneratorUtil.class.getResource("generatorConfig.xml").getFile());
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);
System.out.println(warnings);
}
}
配置好后直接運行這個utils