相比之前的版本(v1.1.0),此版本(v2.1.0)做了较大的改动。

项目地址:

github https://github.com/cnsvili/mybatis-jpa

gitee https://gitee.com/svili/mybatis-jpa

当前版本主要提供2个功能:

1.生成符合JPA规范的ResultMap,支持结果集的嵌套;

2.简化Insert/Update语句的构建。

解决实际开发中的痛点:

1.Mybatis提供的ResultSet不够灵活,ResultMap构建繁琐;

2.Insert/Update构建繁琐,特别是cloumn列很多时,构建和修改都很麻烦。

~~~mybatis generator不符合我个人习惯,不做评价。

鉴于后续版本修改,可能导致文章与版本不一致,建议阅读代码仓库的wiki文档,本文只列举简单示例,说明版本特性。

1 ResultTypePlugin

e.g.

mybatis.xml

<configuration>
<plugins>
<plugin interceptor="com.mybatis.jpa.plugin.ResultTypePlugin">
</plugin>
</plugins>
</configuration>

JavaBean

@Entity
public class UserArchive {// <resultMap id="xxx" type="userArchive"> @Id
private Long userId;// <id property="id" column="user_id" /> /** 默认驼峰与下划线转换 */
private String userName;// <result property="username" column="user_name"/> /** 枚举类型 */
@Enumerated(EnumType.ORDINAL)
private SexEnum sex;// <result property="sex" column="sex" typeHandler=EnumOrdinalTypeHandler/> /** 属性名与列名不一致 */
@Column(name = "gmt_create")
private Date createTime;// <result property="createTime" column="gmt_create"/>
}// </resultMap>

mapper.xml

<!-- in xml,declare the resultType -->
<!-- 使用resultType -->
<select id="selectById" resultType="userArchive">
SELECT * FROM t_sys_user_archive WHERE user_id = #{userId}
</select>

2 DefinitionStatementScanner

Spring 容器初始化完成后执行

@Service
public class DefinitionStatementInit { @Autowired
private SqlSessionFactory sqlSessionFactory; @PostConstruct
public void init() {
Configuration configuration = sqlSessionFactory.getConfiguration();
StatementBuildable statementBuildable = new DefinitionStatementBuilder(configuration);
DefinitionStatementScanner.Builder builder = new DefinitionStatementScanner.Builder();
DefinitionStatementScanner definitionStatementScanner = builder.configuration(configuration).basePackages(new String[]{"com.mybatis.jpa.mapper"})
.statementBuilder(statementBuildable).build();
definitionStatementScanner.scan();
}
}

Mapper

@Mapper
@Repository
public interface UserUpdateMapper { @InsertDefinition(selective = true)
int insert(User user); @UpdateDefinition(selective = true, where = " user_id = #{userId}")
int updateById(User user);
}

以上~

Mybatis JPA 插件简介(v2.1.0)的更多相关文章

  1. Mybatis JPA 插件简介

    前段时间了解到Spring JPA,感觉挺好用,但其依赖于Hibernate,本人看到Hibernate就头大(不是说Hibernate不好哈,而是进阶太难),于是做了一个迷你版的Mybatis JP ...

  2. Mybatis JPA 代码构建

    前段时间了解到Spring JPA,感觉挺好用,但其依赖于Hibernate,本人看到Hibernate就头大(不是说Hibernate不好哈,而是进阶太难),于是做了一个迷你版的Mybatis JP ...

  3. Mybatis jpa mini 代码解析

    源码地址(git):https://github.com/LittleNewbie/mybatis-jpa 一.Mybatis简介 mybatis中文官方文档:http://www.mybatis.o ...

  4. Springboot+MyBatis+JPA集成

      1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Sp ...

  5. spring boot、cloud v2.1.0.RELEASE 使用及技术整理

    2018年10月30日 springboot v2.1.0.RELEASE 发布: https://github.com/spring-projects/spring-boot/releases/ta ...

  6. 集成Springboot+MyBatis+JPA

    1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...

  7. Mybatis Plugin插件安装破解及使用

    2018年2月更新 2018年2月份,提供一个网上比较多的一个版本V3.21版本,下载资源里面有个已整合版直接解压放入C:\Users\你的用户名\.IntelliJIdea2017.3\config ...

  8. mybatis3.0-[topic10-14] -全局配置文件_plugins插件简介/ typeHandlers_类型处理器简介 /enviroments_运行环境 /多数据库支持/mappers_sql映射注册

    mybatis3.0-全局配置文件_   下面为中文官网解释 全局配置文件的标签需要按如下定义的顺序: <!ELEMENT configuration (properties?, setting ...

  9. MyBatis 示例-插件

    简介 利用 MyBatis Plugin 插件技术实现分页功能. 分页插件实现思路如下: 业务代码在 ThreadLocal 中保存分页信息: MyBatis Interceptor 拦截查询请求,获 ...

随机推荐

  1. 【Python】 获取MP3信息replica

    replica 初衷是想要整理iphone中的音乐.IOS(我自己的手机还是IOS8.3,新版本的系统可能有变化了)自带的音乐软件中所有音乐文件都存放在/var/mobile/Media/iTunes ...

  2. Python -- Records项目学习

    Records学习笔记 Records链接地址 1. __getitem__(self, key) 内建方法(Build-in) 例子: class Test(object): def __getit ...

  3. 强烈推荐!!!Fiddler抓取https设置详解(图文)

    很多实用fiddler抓包,对于http来说不需太多纠结,随便设置下就能用,但是抓取https就死活抓不了, 诸如以下问题: creation of the root certificate was ...

  4. 巨人大哥谈Web应用中的Session(session详解)

    巨人大哥谈Web应用中的Session(session详解) 虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术. ...

  5. Java并发编程实战(chapter_2)(对象发布、不变性、设计线程安全类)

    一.发布与溢出 "发布(Publish)"一个对象的意思是指,使对象能够在当前作用于之外的代码中使用.这个"之外",尤为关键,各种出问题的地方,都是因为这个&q ...

  6. ThreadLocal 原理和使用场景分析

    ThreadLocal 不知道大家有没有用过,但至少听说过,今天主要记录一下 ThreadLocal 的原理和使用场景. 使用场景 直接定位到 ThreadLocal 的源码,可以看到源码注释中有很清 ...

  7. 新手使用mac上的textedit写HTML时遇到的问题及解决办法

    刚开始在mac上学习HTML,总结一下遇到的问题和解决办法 问题:使用textedit编写html,在网页上却仍然显示的是代码. 解决办法: 打开textedit后打开文本编辑 选择偏好设置 按如图所 ...

  8. PTA 第二周作业 张乐

    题目1:整数的四则运算 1.实验代码 #include <stdio.h> int main() { int A,B; scanf("%d %d",&A,&am ...

  9. 网络1711c语言第3次作业总结

    作业地址:https://edu.cnblogs.com/campus/jmu/JMUC--NE17111712/homework/1166 总结 1.评分细则 评分注意事项 注意用Markdown语 ...

  10. 下载文件downloadFile

    public static void downLoadFile(InputStream inStream, String fileName) { if (StringUtils.isBlank(fil ...