使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
工具:myeclipse
Mysql数据库
先决条件:项目必须成功连接数据库
一、首先加载Mybatis-Generator的jar包:
在pom.xml中配置:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
二、在src/main/resources下新建generatorConfig.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="mysqltables" targetRuntime="MyBatis3"
defaultModelType="conditional">
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/dida"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="cn.tedu.ttms.attachment.entity"
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="cn.tedu.ttms.attachment.dao"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="user" domainObjectName="User"></table>
<table tableName="addr" domainObjectName="Addr"></table>
<table tableName="companyinfo" domainObjectName="Companyinfo"></table>
<table tableName="education" domainObjectName="Education"></table>
<table tableName="exerciation" domainObjectName="Exerciation"></table>
<table tableName="picture" domainObjectName="Picture"></table>
<table tableName="synopsis" domainObjectName="Synopsis"></table>
</context>
</generatorConfiguration>
<!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="mysqltables" targetRuntime="MyBatis3"
defaultModelType="conditional">
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/dida"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="cn.tedu.ttms.attachment.entity"
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="cn.tedu.ttms.attachment.dao"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="user" domainObjectName="User"></table>
<table tableName="addr" domainObjectName="Addr"></table>
<table tableName="companyinfo" domainObjectName="Companyinfo"></table>
<table tableName="education" domainObjectName="Education"></table>
<table tableName="exerciation" domainObjectName="Exerciation"></table>
<table tableName="picture" domainObjectName="Picture"></table>
<table tableName="synopsis" domainObjectName="Synopsis"></table>
</context>
</generatorConfiguration>
三、写一个测试类,实现文件自动生成
在cn.tedu.ttms.attachment.dao新建BMGTest.java类
package cn.tedu.ttms.attachment.dao;
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 BMGTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("./src/main/resources/generatorConfig.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);
}
}
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 BMGTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("./src/main/resources/generatorConfig.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);
}
}
四、点击Run AS---》 java aplication
1.如果没有报错,则表示成功,刷新项目即可查看配置文件。
2.如果有报“关于xml加载提示:
Error on line 1 of document : 前言中不允许有内容”错误,需要用txt,记事本等方式打开,并把格式保存改为ANSI。
最后再复制粘贴到项目并运行测试类,成功后会生成如下信息
以上就是我分享的东西。