MyBatis 示例-类型处理器
MyBatis 提供了很多默认类型处理器,参考官网地址:链接,除了官网提供的类型处理器,我们也可以自定义类型处理器。
具体做法为:实现 org.apache.ibatis.type.TypeHandler 接口, 或继承 org.apache.ibatis.type.BaseTypeHandler 类 , 然后可以选择性地将它映射到一个 JDBC 类型。
测试类:com.yjw.demo.TypeHandlerTest
比如我们要自定义一个性别的枚举类型处理器,实现步骤如下所示:
创建类型处理器
定义性别的枚举
/**
* 性格枚举
*
* @author yinjianwei
* @date 2018/09/27
*/
public enum Sex {
MALE(1, "男"), FEMALE(2, "女");
private Integer code;
private String value;
private Sex(Integer code, String value) {
this.code = code;
this.value = value;
}
/**
* 根据code获得value
*
* @param code
* @return
*/
public static String getValue(Integer code) {
String value = null;
for (Sex sex : Sex.values()) {
if (sex.getCode().equals(code)) {
value = sex.getValue();
}
}
return value;
}
/**
* 根据code获取sex
*
* @param code
* @return
*/
public static Sex getSex(Integer code) {
for (Sex sex : Sex.values()) {
if (sex.getCode().equals(code)) {
return sex;
}
}
return null;
}
/**
* @return the code
*/
public Integer getCode() {
return code;
}
/**
* @param code the code to set
*/
public void setCode(Integer code) {
this.code = code;
}
/**
* @return the value
*/
public String getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(String value) {
this.value = value;
}
}
创建性别类型处理器 SexEnumTypeHandler
/**
* 性别类型处理器
*
* @author yinjianwei
* @date 2018/09/27
*/
public class SexEnumTypeHandler extends BaseTypeHandler<Sex> { /**
* 入参处理
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Sex parameter, JdbcType jdbcType) throws SQLException {
ps.setInt(i, parameter.getCode());
} /**
* 返回结果处理
*/
@Override
public Sex getNullableResult(ResultSet rs, String columnName) throws SQLException {
int code = rs.getInt(columnName);
if (rs.wasNull()) {
return null;
} else {
return Sex.getSex(code);
}
} /**
* 返回结果处理
*/
@Override
public Sex getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int code = rs.getInt(columnIndex);
if (rs.wasNull()) {
return null;
} else {
return Sex.getSex(code);
}
} /**
* 存储过程返回结果(CallableStatement)处理
*/
@Override
public Sex getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int code = cs.getInt(columnIndex);
if (cs.wasNull()) {
return null;
} else {
return Sex.getSex(code);
}
} }
这里使用的是继承 org.apache.ibatis.type.BaseTypeHandler 类的方式,重写父类的四个方法,分别对入参和返回结果做了类型转换处理。
配置类型处理器
有两种方式配置类型处理器,一种是在 MyBatis 的配置文件中配置,可以实现类型处理器的自动发现;另外一种是显式地为那些 SQL 语句设置要使用的类型处理器。
MyBatis 配置文件中配置
application-dev.yml
mybatis:
type-aliases-package: com.yjw.demo.mybatis.biz.pojo.entity;com.yjw.demo.mybatis.biz.pojo.query
mapper-locations: classpath:mapper/*.xml
configLocation: classpath:mybatis-config.xml
# configuration:
# lazy-loading-enabled: true
# aggressive-lazy-loading: false
mybatis.configLocation:指定 MyBatis 的 XML 配置文件路径。
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeHandlers>
<typeHandler javaType="com.yjw.demo.mybatis.common.constant.Sex"
jdbcType="TINYINT"
handler="com.yjw.demo.mybatis.common.type.SexEnumTypeHandler"/>
</typeHandlers>
</configuration>
mybatis-config.xml 文件配置 typeHandler,通过显示的指定 javaType 和 jdbcType 实现类型处理器的自动发现,比如在调用如下 insert 配置的时候就不需要显示的指定 typeHandler,就可以实现类型转换的功能。
StudentMapper.xml
<insert id="insert" parameterType="studentDO" keyProperty="id" useGeneratedKeys="true">
insert into t_student (name, sex, selfcard_no, note)
values (
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=TINYINT},
#{selfcardNo,jdbcType=BIGINT},
#{note,jdbcType=VARCHAR}
)
</insert>
显示指定类型处理器
如果在 mybatis-config.xml 配置文件中没有配置 typeHandler,可以在各个映射文件中显示配置需要使用的类型处理器,也可以实现类型转换的功能。
比如在调用如下 insert 配置的时候显示的指定 typeHandler。
StudentMapper.xml
<insert id="insert" parameterType="studentDO" keyProperty="id" useGeneratedKeys="true">
insert into t_student (name, sex, selfcard_no, note)
values (
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=TINYINT,typeHandler=com.yjw.demo.mybatis.common.type.SexEnumTypeHandler},
#{selfcardNo,jdbcType=BIGINT},
#{note,jdbcType=VARCHAR}
)
</insert>
上面的例子只展现了入参的类型转换的效果,返回结果的效果参考 com.yjw.demo.mybatis.biz.dao.StudentDao#listByConditions 方法,typeHandler 的配置如下。
<resultMap id="BaseResultMap" type="studentDO">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sex" jdbcType="TINYINT" property="sex" />
<!--<result column="sex" jdbcType="TINYINT" property="sex"
typeHandler="com.yjw.demo.mybatis.common.type.SexEnumTypeHandler"/>-->
<result column="selfcard_no" jdbcType="BIGINT" property="selfcardNo" />
<result column="note" jdbcType="VARCHAR" property="note" />
</resultMap>
如果在 MyBatis 的配置文件(mybatis-config.xml)中配置了 typeHandler 了,这里就不需要显示的配置了。
MyBatis 实用篇
MyBatis 示例-类型处理器的更多相关文章
- Mybatis的类型处理器
Mybatis在预处理语句(PreparedStatement)中设置一个参数时,会用默认的typeHandler进行处理. 这句话是什么意思呢,当你想用姓名查询一个人的信息时 <select ...
- mybatis枚举类型处理器
1. 定义枚举值的接口 public abstract interface ValuedEnum { int getValue(); } 所有要被mybatis处理的枚举类继承该接口 2. 定义枚举类 ...
- MyBatis 示例-传递多个参数
映射器的主要元素: 本章介绍 select 元素中传递多个参数的处理方式. 测试类:com.yjw.demo.MulParametersTest 使用 Map 传递参数(不建议使用) 使用 MyBat ...
- MyBatis 示例-简介
简介 为了全面熟悉 MyBatis 的使用,整理一个 MyBatis 的例子,案例中包含了映射器.动态 SQL 的使用.本章先介绍项目结构和配置. 项目地址:链接 数据库表的模型关系:链接 项目结构 ...
- MyBatis 示例-联合查询
简介 MyBatis 提供了两种联合查询的方式,一种是嵌套查询,一种是嵌套结果.先说结论:在项目中不建议使用嵌套查询,会出现性能问题,可以使用嵌套结果. 测试类:com.yjw.demo.JointQ ...
- MyBatis 示例-缓存
MyBatis 提供两种类型的缓存,一种是一级缓存,另一种是二级缓存,本章通过例子的形式描述 MyBatis 缓存的使用. 测试类:com.yjw.demo.CacheTest 一级缓存 MyBati ...
- MyBatis 示例-动态 SQL
MyBatis 的动态 SQL 包括以下几种元素: 详细的使用参考官网文档:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html 本章内容简单描述这 ...
- MyBatis 示例-插件
简介 利用 MyBatis Plugin 插件技术实现分页功能. 分页插件实现思路如下: 业务代码在 ThreadLocal 中保存分页信息: MyBatis Interceptor 拦截查询请求,获 ...
- MyBatis 示例-主键回填
测试类:com.yjw.demo.PrimaryKeyTest 自增长列 数据库表的主键为自增长列,在写业务代码的时候,经常需要在表中新增一条数据后,能获得这条数据的主键 ID,MyBatis 提供了 ...
随机推荐
- spring中基于注解使用ehcache
继续上篇,这篇介绍服务层缓存,基于注解的方式使用ehcache 注解的标签主要有4个:@Cacheable.@CacheEvict.@CachePut.@Caching,他们的用法是: @Cachea ...
- [VB.NET Tips]对于基本数据类型的提示
1.类型字符 有时需要直接量后面加上类型字符以明确指定类型,下面把常用的类型字符列出来 类型 字符 Short S Integer I Long L Decimal D Char c Single F ...
- Flume系列二之案例实战
Flume案例实战 写在前面 通过前面一篇文章http://blog.csdn.net/liuge36/article/details/78589505的介绍我们已经知道flume到底是什么?flum ...
- 如何搭建基于Docker的gitlab服务器集成CI/CD实现DEVOPS(完整版)
From this lesson you will learn about 1,How to install and configure a docker based gitlab server 2, ...
- 虚拟机中设置 CentOS 静态 IP
作为开发人员在工作和学习中通过虚拟机使用 Linux 的情况肯定会非常多,但是 Linux 自带的终端使用体验较差,所以绝大多数使用者都会在宿主机上使用第三方 SSH工具(例如 Xshell)进行连接 ...
- 移动端适配 rem 设置
refresh(); window.onresize = function(){ setTimeout(function(){ refresh(); },10) ...
- ELK 学习笔记之 elasticsearch启动时Warning解决办法
elasticsearch启动时Warning解决办法: 转载:http://www.dajiangtai.com/community/18136.do?origin=csdn-geek&dt ...
- C++set 和 multiset的使用
最后一个自由支配的暑假,学一些自己感兴趣的部分,也算为大三作准备. C++中set集合的使用 定义一个int类型的集合 set<int> s: set<int>::iterat ...
- 搭建docker+swoole+php7 的环境
最近在学习swoole php扩展,苦恼于其运行环境不能在win系统下运行, 但开发代码一直在win系统上,很无奈,,,, 所以就用docker来代替,舒服~ 有很多相关docker的swoole镜像 ...
- Java并发——线程间的等待与通知
前言: 前面讲完了一些并发编程的原理,现在我们要来学习的是线程之间的协作.通俗来说就是,当前线程在某个条件下需要等待,不需要使用太多系统资源.在某个条件下我们需要去唤醒它,分配给它一定的系统资源,让它 ...