熟悉Mybatis-generator的朋友對(duì)下面這個(gè)Mapper接口應(yīng)該不陌生
public interface EmployeeMapper {
int deleteByPrimaryKey(Long id);
int insert(Employee entity);
Employee selectByPrimaryKey(Long id);
List<Employee> selectAll();
int updateByPrimaryKey(Employee entity);
}
當(dāng)我們點(diǎn)擊生成后, generator便幫我們生成了這5個(gè)方法
但隨著業(yè)務(wù)的復(fù)雜性的增加, 這5個(gè)方法根本不夠用, 甚至說(shuō), 很多時(shí)候用不上, 基本都是自己編寫新的方法和對(duì)應(yīng)的sql.
在xml中編寫sql, 沒(méi)有編譯器幫我們檢查語(yǔ)法, 或者使用了mybatis的標(biāo)簽,如foreach,where,if...等. 這樣的sql即便是高手, 也不敢拍胸口說(shuō)絕對(duì)沒(méi)問(wèn)題.
增加了一個(gè)mapper方法, 寫一條sql, 最后還要一番測(cè)試
然而, 作為ORM框架的頭馬, Mybatis已經(jīng)給出了解決方案, 下面正文開(kāi)始
首先, 打開(kāi)generator-config.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="mysql" defaultModelType="conditional" targetRuntime="MyBatis3Simple">
<!--使用自定義插件Lombok生成-->
<plugin type="org.mybatis.generator.plugins.LombokPlugin" >
<property name="hasLombok" value="true"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql:///spring?useSSL=false" userId="root" password="admin">
</jdbcConnection>
<javaModelGenerator targetPackage="com.lwz.dao.domain" targetProject="src/main/java">
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.lwz.dao.mapper"
type="XMLMAPPER" targetProject="src/main/java">
</javaClientGenerator>
<table tableName="employee"><!--domainObjectName=""-->
</table>
</context>
</generatorConfiguration>
此配置文檔已做了大量簡(jiǎn)略, 不可直接使用, 貼出來(lái)是讓大家留意<context>標(biāo)簽中的targetRuntime屬性的值"MyBatis3Simple"
當(dāng)targetRuntime的值為"MyBatis3Simple"時(shí), 生成的mapper便只有5個(gè)方法, 當(dāng)我們把Simple刪掉后再生成一遍, 神奇的事情發(fā)生了
`此時(shí)應(yīng)為`
<context id="mysql" defaultModelType="conditional" targetRuntime="MyBatis3">
/**
* Created by Mybatis Generator 2018/07/15 14:14
*/
public interface EmployeeMapper {
long countByExample(EmployeeExample example);
int deleteByExample(EmployeeExample example);
int deleteByPrimaryKey(Long id);
int insert(Employee entity);
int insertSelective(Employee entity);
List<Employee> selectByExample(EmployeeExample example);
Employee selectByPrimaryKey(Long id);
int updateByExampleSelective(@Param("record") Employee record, @Param("example") EmployeeExample example);
int updateByExample(@Param("record") Employee record, @Param("example") EmployeeExample example);
int updateByPrimaryKeySelective(Employee entity);
int updateByPrimaryKey(Employee entity);
}
并且domain中除了Employee, 多了一個(gè)EmployeeExample 的類, 再打開(kāi)xml文件, 已經(jīng)自動(dòng)生成了一大堆對(duì)應(yīng)的sql.
怎么回事呢, 下面通過(guò)幾個(gè)業(yè)務(wù)場(chǎng)景來(lái)說(shuō)明這些方法的使用
Employee表中只有幾個(gè)字段:
id, name, age, dept_id
可以自行創(chuàng)建表嘗試
- 查詢年齡大于30歲的員工
EmployeeExample example = new EmployeeExample();
example.createCriteria().andAgeGreaterThan(30);
List<Employee> employeeList = employeeMapper.selectByExample(example);
`嗯,沒(méi)錯(cuò),就是這么簡(jiǎn)單`
EmployeeExample, 可以理解為是一個(gè)封裝條件的對(duì)象, 這個(gè)對(duì)象里面封裝了所有字段的>=<like等各種條件, 當(dāng)example.createCriteria()的時(shí)候, 就可以往后面加條件了,支持鏈?zhǔn)骄幊?
- 查詢名字含有李字的員工
EmployeeExample example = new EmployeeExample();
example.createCriteria().andNameLike("%李%");
List<Employee> employeeList = employeeMapper.selectByExample(example);
`嗯,沒(méi)有問(wèn)題`
看到這里, 也許你疑惑他底層是怎么實(shí)現(xiàn)的, 當(dāng)你點(diǎn)方法進(jìn)去看源碼和看完xml的sql語(yǔ)句時(shí), 也就理解了
- 把部門id為1的員工的部門id改為2
EmployeeExample example = new EmployeeExample();
example.createCriteria().andDeptIdEqualTo(1L);
Employee employee = new Employee();
employee.setDeptId(2L);
employeeMapper.updateByExampleSelective(employee, example);
`嗯, 更新的時(shí)候得傳更新后的對(duì)象和條件進(jìn)去`
當(dāng)我們只想更新某些字段的時(shí)候, 只需要傳一個(gè)只含有某些的字段的對(duì)象進(jìn)去就行, selective會(huì)幫我們篩選, 只更新有值的字段
- 刪除所有部門id為1的員工
EmployeeExample example = new EmployeeExample();
example.createCriteria().andDeptIdEqualTo(1L);
employeeMapper.deleteByExample(example);
`嗯, 還是這么簡(jiǎn)單`
以上便是Mybatis-Generator 進(jìn)階用法的簡(jiǎn)單使用, 更多功能大家可以在實(shí)際開(kāi)發(fā)中慢慢探索.
自從用了高階用法, 除掉個(gè)別需求, 我已經(jīng)基本3個(gè)月沒(méi)寫過(guò)sql了. 而是變成使用java寫sql, 維護(hù)性, 可讀性都增強(qiáng)了, 只要方法使用對(duì)了, sql就不會(huì)錯(cuò), 不用像以前得一遍遍的去測(cè)試sql有沒(méi)寫錯(cuò), mybatis標(biāo)簽有沒(méi)用對(duì).
最后, 贊美一句Mybatis大法好