XML映射配置文件

  1. http://www.mybatis.org/mybatis-3/configuration.html

  2. 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>
  1. 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映射配置文件的更多相关文章

  1. Java数据持久层框架 MyBatis之API学习三(XML 映射配置文件)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  2. mybatis学习(一)-------XML 映射配置文件详解

    XML 映射配置文件 MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配 ...

  3. MyBatis官方文档——XML 映射配置文件

    XML 映射配置文件 MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 ...

  4. MyBatis XML 映射配置文件

    配置文件的基本结构 configuration —— 根元素 properties —— 定义配置外在化 settings —— 一些全局性的配置 typeAliases —— 为一些类定义别名 ty ...

  5. 四、XML映射配置文件

    MyBatis的XML配置文件包含了影响MyBatis行为甚深的设置和属性信息.XML文档的高层级结构如下: ----configuration配置 --------properties属性 ---- ...

  6. Mybatis学习--XML映射配置文件

    学习笔记,选自Mybatis官方中文文档:http://www.mybatis.org/mybatis-3/zh/configuration.html MyBatis 的配置文件包含了影响 MyBat ...

  7. Mybatis XML 映射配置文件 -- 熟悉配置

    来源:http://www.mybatis.org/mybatis-3/zh/configuration.html properties mybatis读取属性顺序. 如果属性在不只一个地方进行了配置 ...

  8. MyBatis中XML 映射配置文件的简单介绍

    官网写的比较具体,可以查看以下的网站: http://www.mybatis.org/mybatis-3/zh/configuration.html 另外,实际用到标准的CRUD的操作和查询列表, & ...

  9. 【mybatis xml】数据层框架应用--Mybatis 基于XML映射文件实现数据的CRUD

    使用MyBatis框架进行持久层开发 MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBa ...

随机推荐

  1. pickle file in matlab

    Load pickle files in Matlab Posted on June 12, 2013 by xcorr   http://xcorr.net/2013/06/12/load-pick ...

  2. ural1297

    题解: 后缀数组 st表处理加速lcp 把串后面加一个不可能出现的字符 然后再把串倒过来放在后面 暴力枚举中心 判断lcp 代码: #include<bits/stdc++.h> usin ...

  3. OO Summary Ⅱ

    [第五次作业——多线程电梯] 类图 度量 协作图 设计分析: 多线程电梯是我第一次接触多线程,因此真的是无(瞎)从(g)下(2)手(写),感觉仿佛只是用一个调度器来调度3部电梯但又总觉得好像哪里不太对 ...

  4. ActiveMQ的P2P示例

    ActiveMQ的P2P示例(点对点通信) (1)下载安装activemq,启动activeMQ. 详细步骤参考博客:http://www.cnblogs.com/DFX339/p/9050878.h ...

  5. linux 删除日志

    https://jingyan.baidu.com/album/c1a3101e73129ade656deb9d.html?picindex=2 里面的 ls -s 可以看到目录 https://zh ...

  6. ubuntu14.04 解析不了域名—ubuntu的DNS配置

    问题描述: 电脑系统为ubuntu14.04,连上无线后,火狐浏览器打开www.baidu.com,提示找不到服务器,以及终端ping www.baidu.com,提示unkown host,但是浏览 ...

  7. minidebug学习分析 01 基本框架

    0x01  基本框架  基本框架就是CreateProcess启动目标程序,再通过调试事件DEBUG_EVENT在调试循环中监控程序的行为.  (1)CreatProcess BOOL CreateP ...

  8. table添加行

    需求是要实现表格的动态增加与删除,并且保留标题行和首行,找了半天jq插件,没找到合适的,所以自己写了个demo <!DOCTYPE html> <html> <head& ...

  9. jvm的基本结构以及各部分详解(转)

    原文链接:https://www.cnblogs.com/zwbg/p/6194470.html 1.java虚拟机的基本结构 图: 1.类加载器子系统从文件系统或者网络中加载Class信息,类信息( ...

  10. 使用kafka和zookeeper 构建分布式编译环境

    1:在每台机器上安装jdk, 脚本代码如下: 每一个机器上下载jdk,zookeeper,kafka 链接:https://www.oracle.com/technetwork/java/javase ...