XML映射配置文件
XML映射配置文件
Type Handlers 类型处理器
每当MyBatis在PreparedStatement上设置参数或从ResultSet中检索值时,都会使用TypeHandler以适合Java类型的方式检索值。下表描述了默认的TypeHandlers。
注意 从版本3.4.5开始,MyBatis默认支持JSR-310(日期和时间API)
可以 override 原有的 handlers
可以创造自己的 handler 补充没实现的类
实例:
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
/**
* 日期转换
* 数据库是varchar,java类型是Date
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(Date.class)
public class TestTypeHandler extends BaseTypeHandler<Date> {
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i,String.valueOf(parameter.getTime()));
}
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
return new Date(rs.getLong(columnName));
}
public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return new Date(rs.getLong(columnIndex));
}
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getDate(columnIndex);
}
}
<!--配置方式一-->
<!--当我们进行数据库的读取操作的时候,秒数就会自动转为Date对象-->
<resultMap id="testResultMap" type="Test">
<result typeHandler="typehandlers.TestTypeHandler" column="reg" javaType="java.util.Date"
jdbcType="VARCHAR" property="reg"/>
</resultMap>
<insert id="insertExample" parameterType="Test">
insert into test(reg) values (#{reg,typeHandler=typehandlers.TestTypeHandler})
</insert>
<!--配置方式二 在config中配置-->
<!--当我们进行数据库的读取操作的时候,秒数就会自动转为Date对象-->
<typeHandlers>
<typeHandler handler="typehandlers.TestTypeHandler"/>
</typeHandlers>
- plugins
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.util.Properties;
/**
* 在映射语句的执行中拦截对某些点的调用
* Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
* ParameterHandler (getParameterObject, setParameters)
* ResultSetHandler (handleResultSets, handleOutputParameters)
* StatementHandler (prepare, parameterize, batch, update, query)
*/
@Intercepts(@Signature(type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
))
public class TestPlugin implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
BoundSql boundSql = mappedStatement.getBoundSql(invocation.getArgs()[1]);
System.out.println(String.format("sql语句:%s,返回值:%s",boundSql.getSql(),boundSql.getParameterObject()));
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target,this);
}
public void setProperties(Properties properties) {
}
}
<plugins>
<plugin interceptor="plugins.TestPlugin"></plugin>
</plugins>
占位符中指定默认值
<!--占位符中指定默认值,启用此功能-->
<!--
文档:
http://www.mybatis.org/mybatis-3/configuration.html-->
<properties resource="db.properties">
<property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/>
</properties>
<!--MyBatis 3.4.2开始,您可以在占位符中指定默认值-->
<dataSource type="POOLED">
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username:root}"/>
<property name="password" value="${db.password:root}"/>
</dataSource>
XML映射配置文件的更多相关文章
- Java数据持久层框架 MyBatis之API学习三(XML 映射配置文件)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- mybatis学习(一)-------XML 映射配置文件详解
XML 映射配置文件 MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配 ...
- MyBatis官方文档——XML 映射配置文件
XML 映射配置文件 MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 ...
- MyBatis XML 映射配置文件
配置文件的基本结构 configuration —— 根元素 properties —— 定义配置外在化 settings —— 一些全局性的配置 typeAliases —— 为一些类定义别名 ty ...
- 四、XML映射配置文件
MyBatis的XML配置文件包含了影响MyBatis行为甚深的设置和属性信息.XML文档的高层级结构如下: ----configuration配置 --------properties属性 ---- ...
- Mybatis学习--XML映射配置文件
学习笔记,选自Mybatis官方中文文档:http://www.mybatis.org/mybatis-3/zh/configuration.html MyBatis 的配置文件包含了影响 MyBat ...
- Mybatis XML 映射配置文件 -- 熟悉配置
来源:http://www.mybatis.org/mybatis-3/zh/configuration.html properties mybatis读取属性顺序. 如果属性在不只一个地方进行了配置 ...
- MyBatis中XML 映射配置文件的简单介绍
官网写的比较具体,可以查看以下的网站: http://www.mybatis.org/mybatis-3/zh/configuration.html 另外,实际用到标准的CRUD的操作和查询列表, & ...
- 【mybatis xml】数据层框架应用--Mybatis 基于XML映射文件实现数据的CRUD
使用MyBatis框架进行持久层开发 MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBa ...
随机推荐
- 逆袭之旅DAY16.东软实训.Oracle.修改用户
2018-07-12 15:49:51
- learning at command AT+CGSN
AT command AT+CGSN [Purpose] Learning how to get mobile module international Mobile Equipment ...
- UVa 10795 - A Different Task 对称, 中间状态, 数位DP 难度: 3
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 每天CSS学习之border-radius
css3的border-radius属性,我们用之来画圆角边框. 1.border-radius:none;//表示不用圆角边框,边框会变成方形. 2.border-radius:水平方向{1,4}[ ...
- 十三. Python基础(13)--生成器进阶
十三. Python基础(13)--生成器进阶 1 ● send()方法 generator.send(value) Resumes the execution, and "sends&qu ...
- 7.1 C++模板基本概念及语法 《C++模板与标准模板库》
参考:http://www.weixueyuan.net/view/6398.html 总结: 模板是另一种代码重用机制. 需要设计的几个类,其功能都是一样的,仅仅只是需要操作的数据类型不同. 有更好 ...
- Java 将图片转成base64,传到前台展示
后台代码: public String getBase64(SysFile sysFile){ String imgStr = ""; try { File file = new ...
- DevExpress ASP.NET Core Controls v18.2新功能详解
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Core ...
- Android开发 ---Activity的7种运行状态
Android开发 ---Activity的7种运行状态 创建 --> 启动 --> 运行 --> 暂停 --> 停止 --> 销毁 重启 操作图解: 1.MainA ...
- Problem A: 平面上的点——Point类 (I)
Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定.现在我们封装一个“Point类”来实现平面上的点的操作. 根据“append.cc”,完成Point类的构造方 ...