在我们编写程序时,为数据库中的每张表都要编写对应的实体类,在这个过程中会耗费很多时间,许多的表和字段也很难保证不会打错。MyBatis给我们提供了自动生成实体类,实体映射文件,接口的功能。这大大减少了我们的重复工作。使用MyBatis Generator逆向工程只需三步:
1.引入MyBatis Generator jar包
mybatis-generator-core-1.3.5.jar
2.逆向工程生成规则
在工程下新建一个mbg.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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 此属性用于指定MBG是否将在生成的代码中包含任何coments
当属性为true时,不会向任何生成的元素添加注释 -->
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<!-- 指定如何连接到目标数据库 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/studentinfomanager"
userId="root" password="admin">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 用于为生成的Java模型对象指定目标包和目标项目 -->
<javaModelGenerator targetPackage="com.javaweb.bean"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 用于为生成的实体映射文件指定目标包和目标项目 -->
<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 用于为生成的接口指定目标包和目标项目 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.javaweb.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 为每张表设置实体类类名 -->
<table schema="DB2ADMIN" tableName="s_aattendance"
domainObjectName="StudentAattendance"></table>
<table schema="DB2ADMIN" tableName="s_badbehavior"
domainObjectName="StudentBadbehavior"></table>
<table schema="DB2ADMIN" tableName="s_basicmessage"
domainObjectName="StudentBasicmessage"></table>
<table schema="DB2ADMIN" tableName="s_charge" domainObjectName="StudentCharge"></table>
<table schema="DB2ADMIN" tableName="s_charge_launch"
domainObjectName="StudentChargeLaunch"></table>
<table schema="DB2ADMIN" tableName="s_class" domainObjectName="StudentClass"></table>
<table schema="DB2ADMIN" tableName="s_honour" domainObjectName="StudentHonour"></table>
<table schema="DB2ADMIN" tableName="s_login" domainObjectName="StudentLogin"></table>
<table schema="DB2ADMIN" tableName="s_performance"
domainObjectName="StudentPerformance"></table>
<table schema="DB2ADMIN" tableName="s_profession"
domainObjectName="StudentProfession"></table>
<table schema="DB2ADMIN" tableName="s_reg" domainObjectName="StudentReg"></table>
<table schema="DB2ADMIN" tableName="s_work" domainObjectName="StudentWork"></table>
</context>
</generatorConfiguration>
3.运行MyBatis Generator
使用XML配置文件从Java运行MBG
package com.javaweb.test;
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 TestApp {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//copy官方的例子,把这里改成自己定义的配置文件
File configFile = new File("mbg.xml");
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("===========");
}
}
刷新项目,我们所需要的实体类等文件就建好了。