Mybatis Generator 1.4.0 发布,自动代码工具

栏目: 软件资讯 · 发布时间: 6年前

内容简介:Mybatis Generator是一个自动代码工具,此版本相较于旧版本有很多重要的更新。 Mybatis Generator目前有5种运行模式,分别为:MyBatis3DynamicSql、MyBatis3Kotlin、MyBatis3、MyBatis3Simple、MyBatis3DynamicSq...

Mybatis Generator是一个自动代码工具,此版本相较于旧版本有很多重要的更新。

Mybatis Generator目前有5种运行模式,分别为:MyBatis3DynamicSql、MyBatis3Kotlin、MyBatis3、MyBatis3Simple、MyBatis3DynamicSqlV1。

<context id="MysqlTables" targetRuntime="MyBatis3DynamicSql" defaultModelType="flat">
 <!-- 省略中间 -->
</context>

1、MyBatis3、MyBatis3Simple是比较老的模式,通常生成mapper接口、model实体和XML文件(当javaClientGenerator type为 ANNOTATEDMAPPER 时不生成XML)。这两种模式生成的文件只需要依赖Mybatis3即可正常工作,但是对于很多操作不支持。如果想要使用数据库函数或者多表联合查询,这种模式下是不支持的,需要写额外的SQL。

2、MyBatis3DynamicSqlV1是比较新的模式。除了需要依赖Mybatis3以外,还需要mybatis-dynamic-sql才能正常工作。相比上面的老模式,这种模式更加方便。

        Student obj = new Student();
        obj.setEmail("xxx@xx.com");

        //跟新id为8的学生信息
        studentMapper.updateByPrimaryKeySelective(obj);

        //将学生id大于10并且名字含有"张"或者性别为1的进行更新。
        // sql: update student set email='xxx@xx.com' where id >10 and (name like '%张%' or sex =1)
        studentMapper.updateByExampleSelective(obj).where(student.id, isGreaterThan(10L))
                .and(student.name, isLike("%张%"), SqlBuilder.or(student.sex, isEqualTo(1)))
                .build().execute();
        //多表join查询,这主要是Mybatis-Dynamic-Sql的功能
        SelectStatementProvider selectStatement =select(student.allColumns()).from(student).join(teacher)
                .on(student.id, equalTo(teacher.id))
                .where(teacher.age, isGreaterThanOrEqualTo(30))
                .build().render(RenderingStrategies.MYBATIS3);
        List<Student> list = studentMapper.selectMany(selectStatement);

3、MyBatis3DynamicSql、MyBatis3Kotlin这两种模式是类似的,一种是使用java,一种是Kotlin。这是在MyBatis3DynamicSqlV1基础上改进而来。这次的更新主要集中在这种模式上面。

  • 删除了*ByExample的方法。
  • 增加了Optional支持。
  • 增加了批量新增的支持。
        Student obj = new Student();
        obj.setEmail("xxx@xx.com");
        //跟原来一样的更新
        studentMapper.updateByPrimaryKeySelective(obj);
        //批量新增
        studentMapper.insertMultiple(Collections.singleton(obj));
        //按条件更新 类似updateByExample  sql: update student set email = #{email} where id >5 and ( name like '%wang%' or sex =1 )
        studentMapper.update(completer ->
                completer.set(student.email).equalTo(obj.getEmail())
                        .where(student.id, isGreaterThanOrEqualTo(5L))
                        .and(student.name, isLike("%wang%"), SqlBuilder.or(student.sex, isEqualTo(1)))
        );
        //按条件搜索 类似 selectByExample  sql: select * from student where id >= 5 and ( name like '%wang%' or sex =1 )
        List<Student> list = studentMapper.select(completer ->
                completer.where(student.id, isGreaterThanOrEqualTo(5L))
                        .and(student.name, isLike("%wang%"), SqlBuilder.or(student.sex, isEqualTo(1)))
        );
        // Optional 支持
        Optional<Student> optional = studentMapper.selectByPrimaryKey(5L);
        optional.ifPresent(value -> System.out.println(value.getEmail()));
        //Mybatis-Dynamic-Sql 功能
        SelectStatementProvider selectStatement =select(student.allColumns()).from(student).join(teacher)
                .on(student.id, equalTo(teacher.id))
                .where(teacher.age, isGreaterThanOrEqualTo(30))
                .build().render(RenderingStrategies.MYBATIS3);
        List<Student> studentList = studentMapper.selectMany(selectStatement);

默认模式为MyBatis3DynamicSql,强烈推荐。具体更新,请查看官网Mybatis Generator官方文档


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Inside the C++ Object Model

Inside the C++ Object Model

Stanley B. Lippman / Addison-Wesley Professional / 1996-5-13 / USD 64.99

Inside the C++ Object Model focuses on the underlying mechanisms that support object-oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritanc......一起来看看 《Inside the C++ Object Model》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具