MyBatis-MBG逆向工程
概念:
Mybatis Generator是一个功能强大的代码生成器,可以生成的代码包含了数据库表对应的实体类、Mapper接口类、Mapper XML映射文件和Example对象等,这些代码文件中几乎包含了全部的单表操作方法,使用MBG可以极大地方便我们使用MyBatis,减少很多重复工作。
步骤:
1)导入jar包:
mybatis-generator-core-1.3.2-javadoc.jar
mybatis-generator-core-1.3.2-sources.jar
mybatis-generator-core-1.3.2.jar
2)编写对应的xml文件: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>
<!-- <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" /> -->
<!--
targetRuntime="MyBatis3Simple"为简单版,只会生成一些简单的增删改查
targetRuntime="MyBatis3"为豪华版
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- jdbcConnection:指定如何连接到目标数据库 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
userId="root"
password="root">
</jdbcConnection>
<!-- javaTypeResolver: -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
javaModelGenerator:定义Java模型的属性,指定JavaBean的生成策略
targetPackage:目标包名
targetProject:目标工程, .\src当前工程的src
-->
<javaModelGenerator targetPackage="com.mybatis.bean" targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
sqlMapGenerator:SQL映射的生成策略
-->
<sqlMapGenerator targetPackage="com.mybatis.dao" targetProject=".\conf">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--
javaClientGenerator:指定mapper接口所在的位置
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.dao" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--
<table>指定要逆向分析哪些表,根据表要创建JavaBean
-->
<table tableName="tbl_employee" domainObjectName="Employee"></table>
<table tableName="tbl_dept" domainObjectName="Department"></table>
</context>
</generatorConfiguration>
3)编写运行代码
package com.mybatis.test;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import com.mybatis.bean.Employee;
import com.mybatis.bean.EmployeeExample;
import com.mybatis.bean.EmployeeExample.Criteria;
import com.mybatis.dao.EmployeeMapper;
public class MyBatisTest {
//运行mybatis生成器的方法
@Test
public void testMbg() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mgb.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);
}
//获取sqlSessionFactory的方法
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
//mgb.xml中targetRuntime="MyBatis3Simple"时的测试
@Test
public void testSimple() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee selectByPrimaryKey = mapper.selectByPrimaryKey(1);
System.out.println(selectByPrimaryKey);
}finally{
openSession.close();
}
}
}