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 ...
随机推荐
- Zabbix3.4.7监控windows进程
1.首先,找到要监控进程的主机 创建新的监控项 然后应用集选择processes,点击添加,此处是以zabbix_agentd.exe为例添加 2. 为此监控项添加触发器 注意触发器表达式的编写,上面 ...
- windows 文本操作
type命令 作用:显示文件类文件内容 (例如:.txt .Java .cpp .c .html 等属于文本类文件 .word excel ppt exe dll 等都不属于文本类文件) fc命令 作 ...
- java的八大排序
public class Sort2 { public static void main(String[] args) { Sort2 sort = new Sort2(); System.out.p ...
- Linux运维工程师需要掌握什么才能胜任工作呢
万丈高楼平地起,所有一切的高深的技术都离不开最基本的技术,那么作为运维工程师的你,什么是最基本的技术呢,毫无疑问是Linux,Linux 是你所有一切技术的根源,试想一下如果你连基础的操作命令都不知道 ...
- 2.BIND服务基础及域主服务器配置
一.BIND 现今使用最晚广泛的DNS服务器软件是BIND(Berkeley Internet Name Domain),最早由伯克利大学的一名学生编写,现在最新的版本是9,由ISC(Internet ...
- MySQL 占用cpu 100%
目前的线上数据库,分为主从两个库,从库用来做比较耗时的数据统计分析. 今天top了一下从库服务器,发现mysqld 在很长一段时间都占用105% cpu,一开始以为是从库在处理主库的binlog. 两 ...
- python笔记7-if中的is ;in ;not搭配用法
names="111 222 333" print("111" in names)#返回的是True,用in返回的是布尔值in在里面 print("1 ...
- tomcat的安装及配置
1.首先进tomcat官网下载zip压缩文件:http://tomcat.apache.org/download-90.cgi 2.解压缩到指定文件压(后面配置环境变量会用到) 3.配置环境变量 4. ...
- Java流对象理解
马士兵老师,曾在Java的学习过程中,将Java的流比作管道,认为很贴切,在此笔者也建议读者在学习过程中作类似比喻,形象化的学习 Java根据数据流向的不同分为输入流和输出流: Java根据处理数据类 ...
- MySQL:存储过程和函数
存储过程和函数 一.创建存储过程和函数 1.创建存储过程 语法: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic . ...