一、概念与机制
1、概念
2、基本原理
二、操作
1、配置POM
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
2、MBG配置文件
文件名必须是: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="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis-example"
userId="root"
password="mysql">
</jdbcConnection>
<javaModelGenerator targetPackage="com.zengchuiyu.mybatis.entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.zengchuiyu.mybatis.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zengchuiyu.mybatis.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="t_emp" domainObjectName="Employee"/>
<table tableName="t_customer" domainObjectName="Customer"/>
<table tableName="t_order" domainObjectName="Order"/>
</context>
</generatorConfiguration>
注意:自己配置具体的信息,如数据库连接、包名、对应的表
3、执行MBG插件的generate目标
4、效果
三、QBC查询
1、 概念
QBC:Query By Criteria
QBC查询最大的特点就是将SQL语句中的WHERE子句进行了组件化的封装,让我们可以通过调用Criteria对象的方法自由的拼装查询条件。
2、 例子
public class QBCTest {
private SqlSessionFactory factory;
@Before
public void init() throws IOException {
factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
}
@Test
public void test1(){
EmployeeExample example = new EmployeeExample();
EmployeeExample.Criteria criteria01 = example.createCriteria();
criteria01.andEmpSalaryLessThan(1000.00)
.andEmpNameLike("%o%");
SqlSession session = factory.openSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
List<Employee> employeeList = mapper.selectByExample(example);
for (Employee employee : employeeList) {
System.out.println("employee = " + employee);
}
session.close();
}
}
最终效果:
Preparing: select emp_id, emp_name, emp_salary from t_emp WHERE ( emp_salary < ? and emp_name like ? ) [==> Parameters: 1000.0(Double), %o%(String)]
通过逆向工程以及QBC查询可以大大提高开发效率,动态组装我们需要的查询条件。
|