1.MyBatis逆向简介
mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo…),可以让程序员将更多的精力放在繁杂的业务逻辑上。
1).generator下载
2).generator文档
3).jar包
ehcache-core-2.6.8.jar
log4j.jar
mybatis-3.4.1.jar
mybatis-ehcache-1.0.3.jar
mybatis-generator-core-1.3.2.jar
mysql-connector-java-5.1.37-bin.jar
ojdbc6.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.2.jar
4).MBG使用步骤
(1).编写MBG的配置文件(重要几处配置)
①.jdbcConnection:配置数据库连接信息
②.javaModelGenerator:配置javaBean的生成策略
③.sqlMapGenerator:配置sql映射文件生成策略
④.javaClientGenerator:配置Mapper接口的生成策略
⑤.table:配置要逆向解析的数据表;tableName:表名;domainObjectName:对应的javaBean名
(2).运行代码生成器生成代码
5).MBG注意
(1).targetRuntime=“MyBatis3“可以生成带条件的增删改查
(2).targetRuntime=“MyBatis3Simple“可以生成基本的增删改查
(3).如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问题。
2.Generator
1).配置Generator
http://mybatis.org/generator/configreference/xmlconfig.html
(1).
参考文档:http://mybatis.org/generator/configreference/context.html
<!-- <context>元素用于指定生成一组对象的环境。子元素用于指定要连接的数据库,要生成的对象的类型以及要进行自检的表。 -->
<!-- targetRuntime="MyBatis3Simple":生成简单版的CRUD -->
<!-- targetRuntime="MyBatis3":生成复杂版的CRUD -->
<!-- id:此上下文的唯一标识符。该值将在某些错误消息中使用。 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
</context>
(2).jdbcConnection
该元素用于指定内省表所需的数据库连接的属性。MyBatis Generator使用JDBC的DatabaseMetaData类来发现您在配置中指定的表的属性。每个
参考文档:http://mybatis.org/generator/configreference/jdbcConnection.html
<!-- jdbcConnection:指定如何连接到目标数据库 -->
<!-- MyBatis Generator使用JDBC的DatabaseMetaData类来发现您在配置中指定的表的属性。-->
<!-- 每个<context>元素都需要一个<connectionFactory>或<jdbcConnection >元素。 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
userId="root"
password="root123">
</jdbcConnection>
(3).javaTypeResolver
参考文档:http://mybatis.org/generator/configreference/javaTypeResolver.html
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
(4).javaModelGenerator
参考文档:http://mybatis.org/generator/configreference/javaModelGenerator.html
<!-- javaModelGenerator:指定javaBean的生成策略 -->
<!-- targetProject:生成目标包名 -->
<!-- targetProject:目标工程 -->
<!-- http://mybatis.org/generator/configreference/javaModelGenerator.html -->
<javaModelGenerator targetPackage="com.atguigu.mybatis.bean"
targetProject=".\\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
(5).sqlMapGenerator
该元素用于定义SQL映射生成器的属性。SQL Map Generator为每个自省表构建MyBatis格式的SQL map XML文件。
仅当您选择的javaClientGenerator需要XML时,此元素才是
基于MyBatis Dynamic SQL的运行时不会生成XML,并且如果指定了该元素,则会忽略该元素。
如果未指定javaClientGenerator,则适用以下规则:
参考文档:http://mybatis.org/generator/configreference/sqlMapGenerator.html
<!-- sqlMapGenerator:sql映射生成策略; -->
<sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"
targetProject=".\\conf">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
(6).javaClientGenerator
该元素用于定义Java客户端生成器的属性。
Java客户端生成器构建Java接口和类,以方便使用所生成的Java模型和XML映射文件。对于MyBatis,生成的对象采用mapper接口的形式。
该元素是
参考文档:http://mybatis.org/generator/configreference/javaClientGenerator.html
<!-- javaClientGenerator:指定mapper接口所在的位置 -->
<!-- http://mybatis.org/generator/configreference/javaClientGenerator.html -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.atguigu.mybatis.dao" targetProject=".\\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
(7).table
2).运行Generator
(1).选择MBG运行方式
MyBatis Generator(MBG)可以通过以下方式运行
每种方法在链接页面上都有详细说明。
原版:
中文翻译:
(2).MBG XML运行方式
①.生成所需文件XML|JAVA
@Test
public void testMbg() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
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);
}
②.获取ID(在Simple情况下)
@Test
public void testSimple() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
List<Employee> list = mapper.selectAll();
for (Employee employee : list) {
System.out.println(employee.getId());
}
}finally {
openSession.close();
}
}
③.拼接查询条件(在MyBatis3情况下)
@Test
public void testMyBatis3() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//1.查询所有ID
List<Employee> emps = mapper.selectByExample(null);
// for(Employee employee:emps) {
// System.out.println(employee.getId());
// }
//2.查询查询员工名字中有e字母的和员工性别是1的 封装员工查询条件的example
EmployeeExample example = new EmployeeExample();
//创建一个Criteria,这个Criteria就是拼装查询条件;
Criteria criteria = example.createCriteria();
criteria.andLastNameLike("%e%");
criteria.andGenderEqualTo("1");
Criteria criteria2 = example.createCriteria();
criteria2.andEmailLike("%e");
example.or(criteria2);
List<Employee> list = mapper.selectByExample(example);
for(Employee employee:list) {
System.out.println(employee.getId());
}
}finally{
openSession.close();
}
}
④.完整Test代码
package com.atguigu.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.jupiter.api.Test;
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;
import com.atguigu.mybatis.bean.Employee;
import com.atguigu.mybatis.bean.EmployeeExample;
import com.atguigu.mybatis.bean.EmployeeExample.Criteria;
import com.atguigu.mybatis.dao.EmployeeMapper;
class MyBatisTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testMbg() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
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);
}
// @Test
// public void testSimple() throws IOException {
// SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
// SqlSession openSession = sqlSessionFactory.openSession();
// try {
// EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
// List
// for (Employee employee : list) {
// System.out.println(employee.getId());
// }
// }finally {
// openSession.close();
// }
// }
@Test
public void testMyBatis3() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//1.查询所有ID
List<Employee> emps = mapper.selectByExample(null);
// for(Employee employee:emps) {
// System.out.println(employee.getId());
// }
//2.查询查询员工名字中有e字母的和员工性别是1的 封装员工查询条件的example
EmployeeExample example = new EmployeeExample();
//创建一个Criteria,这个Criteria就是拼装查询条件;
Criteria criteria = example.createCriteria();
criteria.andLastNameLike("%e%");
criteria.andGenderEqualTo("1");
Criteria criteria2 = example.createCriteria();
criteria2.andEmailLike("%e");
example.or(criteria2);
List<Employee> list = mapper.selectByExample(example);
for(Employee employee:list) {
System.out.println(employee.getId());
}
}finally{
openSession.close();
}
}
}
参考文档:https://github.com/mybatis/generator
http://mybatis.org/generator/quickstart.html
手机扫一扫
移动阅读更方便
你可能感兴趣的文章