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. python settings :RROR 1130: Host 'XXXXXX' is not allowed to connect to this MySQL server

    pymysql.err.InternalError: (1130, u"Host '127.0.0.1' is not allowed to connect to this MySQL se ...

  2. linux下stat命令详解

    在linux系统下,使用stat(显示inode信息)命令可以查看一个文件的某些信息,我们先来尝试一下.  简单的介绍一下stat命令显示出来的文件其他信息: - File:显示文件名 - Size: ...

  3. CString 转换为 wchar_t *

    1.将CString转换为const char* CString str = _T("231222"); std::string strDp = CStringA(str);  / ...

  4. IOS应用内支付IAP从零开始详解

    前言 什么是IAP,即in-app-purchase 这几天一直在搞ios的应用内购,查了很多博客,发现几乎没有一篇博客可以完整的概括出所有的点,为了防止大伙多次查阅资料,所以写了这一篇博客,希望大家 ...

  5. 6.2 C++ string类型字符串的连接

    参考:http://www.weixueyuan.net/view/6391.html 总结: 对于string类型变量,我们可以直接用“+”或者“+=”进行字符串的连接,操作符非常方便. 用“+”风 ...

  6. byte[]->new String(byte[]) -> getByte()引发的不一致问题

    今天接短信接口,短信接口提供了sdk,我们可以直接用sdk发送请求然后发送对应短信. 但是想使用我们平台自定义的httpUtil实现. 然而忙了1天半,才解决这个问题,还是我同事帮忙找出问题并解决的. ...

  7. ubuntu 16.04 菜单栏中无网络链接的图标 无法上网 网络管理版本不兼容

    1 卸载network-manager 卸载后将不能联网 sudo apt-get remove network-manager 2 重新安装network-manager重启 引导界面选择ubunt ...

  8. IIS应用程序池自动回收问题的解决办法

    windows 2012 的w3wp.exe(IIS Worker Process)进程不及时释放导致占用内存过高,以下解决方法: IIS可以设置定时自动回收,默认回收是1740分钟,也就是29小时. ...

  9. kbmMW 5.07.00试用笔记

    在kbmMW 5.06.20试用笔记中遇到的问题,在这个版本中,基本都解决了.但还是发现修正后存在的小问题及新问题: 1.Resolve返回值错误 当提交的ClientQuery是执行一条sql语句, ...

  10. ubantu 设置默认python3.叽叽叽的环境变量

    wkp 发表于 2017-8-22 17:49:08 | 只看该作者 sudo vi ~/.bashrc 在里面加一句 alias python='python3' 或者再简单一点 alias p=' ...