美文网首页
Mybatis-Generator 进阶用法 快速开发 跟sql

Mybatis-Generator 进阶用法 快速开发 跟sql

作者: 优乐美奥利奥 | 来源:发表于2018-07-15 14:55 被阅读0次

熟悉Mybatis-generator的朋友对下面这个Mapper接口应该不陌生

public interface EmployeeMapper {
    int deleteByPrimaryKey(Long id);

    int insert(Employee entity);

    Employee selectByPrimaryKey(Long id);

    List<Employee> selectAll();

    int updateByPrimaryKey(Employee entity);
}

当我们点击生成后, generator便帮我们生成了这5个方法
但随着业务的复杂性的增加, 这5个方法根本不够用, 甚至说, 很多时候用不上, 基本都是自己编写新的方法和对应的sql.
在xml中编写sql, 没有编译器帮我们检查语法, 或者使用了mybatis的标签,如foreach,where,if...等. 这样的sql即便是高手, 也不敢拍胸口说绝对没问题.
增加了一个mapper方法, 写一条sql, 最后还要一番测试

然而, 作为ORM框架的头马, Mybatis已经给出了解决方案, 下面正文开始

首先, 打开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>

此配置文档已做了大量简略, 不可直接使用, 贴出来是让大家留意<context>标签中的targetRuntime属性的值"MyBatis3Simple"

当targetRuntime的值为"MyBatis3Simple"时, 生成的mapper便只有5个方法, 当我们把Simple删掉后再生成一遍, 神奇的事情发生了

`此时应为`
<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, 多了一个EmployeeExample 的类, 再打开xml文件, 已经自动生成了一大堆对应的sql.

怎么回事呢, 下面通过几个业务场景来说明这些方法的使用

Employee表中只有几个字段:
id, name, age, dept_id
可以自行创建表尝试
  • 查询年龄大于30岁的员工
EmployeeExample example = new EmployeeExample();
example.createCriteria().andAgeGreaterThan(30);
List<Employee> employeeList = employeeMapper.selectByExample(example);
`嗯,没错,就是这么简单`

EmployeeExample, 可以理解为是一个封装条件的对象, 这个对象里面封装了所有字段的>=<like等各种条件, 当example.createCriteria()的时候, 就可以往后面加条件了,支持链式编程.

  • 查询名字含有李字的员工
EmployeeExample example = new EmployeeExample();
example.createCriteria().andNameLike("%李%");
List<Employee> employeeList = employeeMapper.selectByExample(example);
`嗯,没有问题`

看到这里, 也许你疑惑他底层是怎么实现的, 当你点方法进去看源码和看完xml的sql语句时, 也就理解了

  • 把部门id为1的员工的部门id改为2
EmployeeExample example = new EmployeeExample();
example.createCriteria().andDeptIdEqualTo(1L);
Employee employee = new Employee();
employee.setDeptId(2L);
employeeMapper.updateByExampleSelective(employee, example);
`嗯, 更新的时候得传更新后的对象和条件进去`

当我们只想更新某些字段的时候, 只需要传一个只含有某些的字段的对象进去就行, selective会帮我们筛选, 只更新有值的字段

  • 删除所有部门id为1的员工
EmployeeExample example = new EmployeeExample();
example.createCriteria().andDeptIdEqualTo(1L);
employeeMapper.deleteByExample(example);
`嗯, 还是这么简单`

以上便是Mybatis-Generator 进阶用法的简单使用, 更多功能大家可以在实际开发中慢慢探索.

自从用了高阶用法, 除掉个别需求, 我已经基本3个月没写过sql了. 而是变成使用java写sql, 维护性, 可读性都增强了, 只要方法使用对了, sql就不会错, 不用像以前得一遍遍的去测试sql有没写错, mybatis标签有没用对.

最后, 赞美一句Mybatis大法好

相关文章

  • Mybatis-Generator 进阶用法 快速开发 跟sql

    熟悉Mybatis-generator的朋友对下面这个Mapper接口应该不陌生 当我们点击生成后, genera...

  • SQL进阶用法

    1. 创建时间与更新时间 (1)–添加CreateTime 设置默认时间CURRENT_TIMESTAMP (2)...

  • 2018-09-05

    目录 一、GPS定位 1、基本用法 2、进阶用法 3、快速求和 4、快速更新报表 5、快速找不同 6、不复制隐藏行...

  • GPS定位和选择性粘贴

    一、基本用法 二、进阶用法 三、快速求和 四、快速更新报表 五、快速找不同 六、不复制隐藏行 七、空行的插入与删除...

  • Gps 定位

    今天学习的是GPS定位和选择性粘贴学习大纲:1、基本用法 2、进阶用法 3、快速求和 4、快速更新报表 5、快速找...

  • 分列快速提取有效信息

    Day 6 分列快速提取有效信息 2019.01.15 一、基本用法 ① 分隔符合 ② 固定宽度 二、进阶用法 ①...

  • GPS定位和选择性粘贴

    今天进入特训营的第5天,学习GPS定位和选择性粘贴,从基本用法、进阶用法、快速求和、快速更新报表、快速找不同、不复...

  • 追“你”到天涯海角 2019-7-15

    今天是参加E站到底特训营的第五天,主要学习了GPS定位的基本用法、进阶用法、快速求和、快速更改报表、快速找...

  • GPS定位和选择性粘贴

    ①GPS定位 1.基本用法 2.进阶用法 3.快速求和 4.快速更新报表 5.快速找不同 6.不复制隐藏行 7.空...

  • 分列快速,快速分列

    今天的课程是通过分列 快速提取有效信息 1、基本用法 2、进阶用法 3、文本转数值 4、不规则日期巧转换 5、快速...

网友评论

      本文标题:Mybatis-Generator 进阶用法 快速开发 跟sql

      本文链接:https://www.haomeiwen.com/subject/bxrppftx.html