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>
05-03 22:46