Mybatis JPA 插件简介
前段时间了解到Spring JPA,感觉挺好用,但其依赖于Hibernate,本人看到Hibernate就头大(不是说Hibernate不好哈,而是进阶太难),于是做了一个迷你版的Mybatis JPA.
代码地址(github): https://github.com/svili365/mybatis-jpa
代码地址(gitee): https://gitee.com/svili/mybatis-jpa
QQ交流群:246912326
因为版本更新,可能会导致博客与代码不对应,强烈建议阅读代码仓库的wiki文档
maven
<dependency>
<groupId>com.littlenb</groupId>
<artifactId>mybatis-jpa</artifactId>
<version>2.1.1</version>
</dependency>
1.1 版本说明
v2.0.1:纯净版的resultTypePlugin
v2.1.0:在v2.0.1基础上,增加SQL构建,InsertDefinition|UpdateDifinition
v2.1.1:增加自定义枚举值,ICodeEnum,@CodeEnum,IntCodeEnumTypeHandler,StringCodeEnumTypeHandler
1.2 工作模式
1.)mybatis-jpa 是基于Mybatis的增强插件,没有对依赖包(源代码)造成污染.
2.)ResultTypePlugin在运行时拦截,每个被拦截的方法会在初次调用时完成解析.
3.)mybatis-jpa SQL的解析和Statement的注册时机,是在Spring applicationContext初始化完成时,只会解析一次.
4.)由mybatis-jpa 解析的Mapper接口中定义的方法(method),将被注册到Mybatis Configuration中,即Mapper的代理和注入由依旧由Mybatis和Spring构建和管理,不影响原有的代码模式和工作模式.
1.3 约定
1.)Entity实体类需使用@Entity或@Table注解标记,类中字段类型不允许使用基本数据类型(如:使用Integer定义整形而不是int);
2.)ResultTypePlugin支持结果集的嵌套,SQL的构建(InsertDefinition|UpdateDifinition)会忽略实体类的嵌套.
3.)按照Mybatis约定,Enum枚举类型默认以 enum.name() 解析,若要解析为enum.ordinal(),需使用注解@Enumrated(value = EnumType.ORDINAL)标识.
4.)使用自定义枚举值,枚举类型需实现ICodeEnum接口,并使用注解@CodeEnum标记Field.@CodeEnum优先级高于@Enumrated.
插件清单
ResultTypePlugin
DefinitionStatementScanner
2.1 ResultTypePlugin
对于常规的结果映射,不需要再构建ResultMap,ResultTypePlugin增加了Mybatis对结果映射(JavaBean/POJO)中JPA注解的处理。
映射规则:
名称匹配默认为驼峰(Java Field)与下划线(SQL Column)
使用@Column注解中name属性指定SQL Column
使用@Transient注解标记非持久化字段(不需要结果集映射的字段)
类型处理:
Boolean-->BooleanTypeHandler
Enum默认为EnumTypeHandler
使用@Enumerated(EnumType.ORDINAL) 指定为 EnumOrdinalTypeHandler
Enum实现ICodeEnum接口实现自定义枚举值
使用@CodeEnum(CodeType.INT) 指定为 IntCodeEnumTypeHandler
或@CodeEnum(CodeType.STRING) 指定为 StringCodeEnumTypeHandler
@CodeEnum 优先级 高于 @Enumerated
结果集嵌套:
- 支持OneToOne
- 支持OneToMany
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/> /** 枚举类型,自定义值 */
@CodeEnum(CodeType.INT)
private PoliticalEnum political;// <result property="political" column="political" typeHandler=IntCodeEnumTypeHandler/> /** 属性名与列名不一致 */
@Column(name = "gmt_create")
private Date createTime;// <result property="createTime" column="gmt_create"/>
}// </resultMap>
mapper.xml
<!-- in xml,declare the resultType -->
<select id="selectById" resultType="userArchive">
SELECT * FROM t_sys_user_archive WHERE user_id = #{userId}
</select>
DefinitionStatementScanner
注册MappedStatement,基于注解,仅支持Insert和Update。
@InsertDefinition:
- selective: 默认值false(处理null属性)
@UpdateDefinition:
selective: 默认值false(处理null属性)
where: SQL condition
e.g.
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);
}
更多示例请查看test目录代码。
如果你想深入了解,项目代码目录还算清晰,源码中有大量必要的注释,你会发现有部分英文注释,不要慌,是我写的,现在感觉有些代码用英文描述反而会简单一些,用中文反而不能够被很好的理解.
代码的构建思路及代码解析,见博文:http://www.cnblogs.com/svili/p/7232323.html
因个人能力有限,如有不足之处,请多包涵,欢迎交流/指正.
Mybatis JPA 插件简介的更多相关文章
- Mybatis JPA 插件简介(v2.1.0)
相比之前的版本(v1.1.0),此版本(v2.1.0)做了较大的改动. 项目地址: github https://github.com/cnsvili/mybatis-jpa gitee https: ...
- Mybatis jpa mini 代码解析
源码地址(git):https://github.com/LittleNewbie/mybatis-jpa 一.Mybatis简介 mybatis中文官方文档:http://www.mybatis.o ...
- Mybatis JPA 代码构建
前段时间了解到Spring JPA,感觉挺好用,但其依赖于Hibernate,本人看到Hibernate就头大(不是说Hibernate不好哈,而是进阶太难),于是做了一个迷你版的Mybatis JP ...
- Springboot+MyBatis+JPA集成
1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Sp ...
- 集成Springboot+MyBatis+JPA
1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...
- mybatis3.0-[topic10-14] -全局配置文件_plugins插件简介/ typeHandlers_类型处理器简介 /enviroments_运行环境 /多数据库支持/mappers_sql映射注册
mybatis3.0-全局配置文件_ 下面为中文官网解释 全局配置文件的标签需要按如下定义的顺序: <!ELEMENT configuration (properties?, setting ...
- MyBatis 示例-插件
简介 利用 MyBatis Plugin 插件技术实现分页功能. 分页插件实现思路如下: 业务代码在 ThreadLocal 中保存分页信息: MyBatis Interceptor 拦截查询请求,获 ...
- mybatis分页插件PageHelp的使用
1.简介 PageHelper 是国内非常优秀的一款开源的 mybatis 分页插件,它支持基本主流与常用的数据库,例如 mysql.oracle.mariaDB.DB2.SQLite.Hsqld ...
- 关于struts2的过滤器和mybatis的插件的分析
网上一搜,发现一篇写的非常棒的博文,就直接复制过来了,供以后复习使用. 前辈博文链接:共三篇: http://jimgreat.iteye.com/blog/1616671: http://jimgr ...
随机推荐
- resnet densenet
1.resnet的skip connection是通过eltwise相加的 2.resnet做detection的时候是在conv4_x的最后一层(也就是stage4的最后一层),因为这个地方stri ...
- Linux关于压缩和解压缩实例
在谈到压缩和解压缩,我想说说它们的应用场景,其实它们主要的应用场景是有这么几个方面? (1)备份(几十个数据库每天进行备份,即包含数据又包含脚本,还有其他十分重要的日志文件等等); (2)降低服务器存 ...
- logback配置与使用(2)
转载:yaoh371 的logback.xml常用配置 <?xml version="1.0" encoding="UTF-8"?> <!-- ...
- mongodb、parse-server、parse-dashboard 的启动命令
1.mongodb启动: 1$ C:\MongoDB\Server\bin>mongod --logpath d:\mongodb\logs\log.log $ C:\MongoDB\Serve ...
- Spring 通过XML配置装配Bean
使用XML装配Bean需要定义对于的XML,需要引入对应的XML模式(XSD)文件,这些文件会定义配置Spring Bean的一些元素,简单的配置如下: <?xml version=" ...
- PAT——1008. 数组元素循环右移问题
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0 ...
- 不推荐在iOS的浏览器应用上使用click和mouseover
iOS上的Safari也支持click 和mouseover等传统的交互事件,只是不推荐在iOS的浏览器应用上使用click和mouseover,因为这两个事件是为了支持鼠标点击而设计 出来的.Cli ...
- 在mac下运行 npm run eject 出现报错问题解决方法
当使用create-react-app创建项目后,接着运行npm run eject时,如果出现下面的错误 可能是脚手架添加了.gitignore这个文件,但是没有本地仓库,可以使用以下代码解决这个问 ...
- 纯JS实现前端动态分页码
思路分析:有3种情况 第一种情况,当前页面curPage < 4 第二种情况,当前页面curPage == 4 第三种情况,当前页面curPage>4 此外,还要考虑,当前页码 curPa ...
- 学生管理系统增删查基本操作(dom4j/sax技术)
基本代码: student.xml <?xml version="1.0" encoding="UTF-8"?><student> &l ...