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 提供了 ...
随机推荐
- Sping学习笔记(一)----Spring源码阅读环境的搭建
idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...
- hadoop之hdfs命令详解
本篇主要对hadoop命令和hdfs命令进行阐述,yarn命令会在之后的文章中体现 hadoop fs命令可以用于其他文件系统,不止是hdfs文件系统内,也就是说该命令的使用范围更广可以用于HDFS. ...
- [Advanced Python] 10 - Transfer parameters
动态库调用 一.Python调用 .so From: Python调用Linux下的动态库(.so) (1) 生成.so:.c to .so lolo@-id:workme$ gcc -Wall -g ...
- Scrapy项目 - 数据简析 - 实现豆瓣 Top250 电影信息爬取的爬虫设计
一.数据分析截图(weka数据分析截图 ) 本例实验,使用Weka 3.7对豆瓣电影网页上所罗列的上映电影信息,如:标题.主要信息(年份.国家.类型)和评分等的信息进行数据分析,Weka 3.7数据分 ...
- opencv边缘检测-拉普拉斯算子
sobel算子一文说了,索贝尔算子是模拟一阶求导,导数越大的地方说明变换越剧烈,越有可能是边缘. 那如果继续对f'(t)求导呢? 可以发现"边缘处"的二阶导数=0. 我们可以利用这 ...
- hadoop之mapreduce详解(优化篇)
一.概述 优化前我们需要知道hadoop适合干什么活,适合什么场景,在工作中,我们要知道业务是怎样的,能才结合平台资源达到最有优化.除了这些我们当然还要知道mapreduce的执行过程,比如从文件的读 ...
- Spring 梳理-el表达式和jstl
JSP中有这么几种元素 1: Scriptlet <% ... %> 2: 声明元素 <%! ... %> 3: Java表达式 <%= ... %> 4: 指令元 ...
- 代理(Proxy)设计模式
目录 概述 静态代理 UML类图 代码实现 代码地址 静态代理的不足 动态代理之jdk实现 UML类图 代码实现 利用JDK实现动态代理的优点 利用JDK实现动态代理的不足 代码地址 动态代理之cgl ...
- 【博客美化】添加github图标
<a href="https://github.com/cai3231" target="_blank"> <img style=" ...
- 移动端适配 rem 设置
refresh(); window.onresize = function(){ setTimeout(function(){ refresh(); },10) ...