使用之前先在pom文件中加上如下依赖:
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- mybatis自动生成实体类 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
这两个依赖是程序能够自动生成实体类的基础,必不可少。下面是生成步骤:
①在当前项目中建立一个generator.xml文件,这个文件名字可以自取,也不能太随意
②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>
<!-- <properties resource="db.properties" /> -->
<classPathEntry location="E:\Developments Tools\mysql-connector-java-5.1.33.jar" />
<context id="mysql2Beans" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 删除代码中带有 代码生成器的注释信息 -->
<property name="suppressAllComments" value="false" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="你的数据库连接"
userId="登录名"
password="密码" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.lyt.usermanage.pojo" targetProject="E:\ConvertBean">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.lyt.usermanage.mapper" targetProject="E:\ConvertBean">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.lyt.usermanage.dao" targetProject="E:\ConvertBean" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!--无论字段是什么类型,生成的类属性都是varchar -->
<!-- <table schema="btupayprod" tableName="T_INFO_MARKETING_CFG" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="fasle">
</table> -->
<!-- <columnOverride column="AFTER_AMT" jdbcType="VARCHAR" /> -->
<!-- <table schema="btupayprod" tableName="t_log_online_payment" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="fasle">
无论字段是什么类型,生成的类属性都是varchar <columnOverride column="AFTER_AMT" jdbcType="VARCHAR"
/> </table> -->
<table schema="test" tableName="user_third" domainObjectName="LytUserThird" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table schema="test" tableName="user_stat" domainObjectName="LytUserStat" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table schema="test" tableName="user_relation" domainObjectName="LytUserRelation" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
具体配置标签请跳转:Mybatis Generator详解
注意:table有多少写多少,这是个手动活
③反转生成的两种方式
方式一:编写工具类运行
package com.lyt.usermanage.utils;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
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.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MyBatisGeneratorUtils {
public static void main(String[] args) {
try {
System.out.println("start generator ...");
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File(MyBatisGeneratorUtils.class.getResource("/mybatis/generator.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("end generator!");
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
方式二:配置bat文件执行
执行完成后你就可以到你配置的生成文件保存路径去找文件了