Chinaunix首页 | 论坛 | 博客
  • 博客访问: 515688
  • 博文数量: 99
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 783
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-12 09:11
文章分类

全部博文(99)

文章存档

2023年(2)

2022年(1)

2020年(1)

2019年(1)

2018年(4)

2017年(16)

2016年(60)

2015年(1)

2013年(3)

2006年(10)

我的朋友

分类: Java

2016-04-27 15:49:26

一、作用及默认的处理

类型处理器的作用就是
  • 查询时把数据库存储的值转换成java类型 
  • 修改是把java类型转换成数据库类型存储,处理      

 下面这个表格描述了默认的类型处理器。 

类型处理器

Java 类型
JDBC 类型
BooleanTypeHandler
java.lang.Boolean, boolean
任何兼容的布尔值
ByteTypeHandler
java.lang.Byte, byte
任何兼容的数字或字节类型
ShortTypeHandler
java.lang.Short, short
任何兼容的数字或短整型
IntegerTypeHandler
java.lang.Integer, int
任何兼容的数字和整型
LongTypeHandler
java.lang.Long, long
任何兼容的数字或长整型
FloatTypeHandler
java.lang.Float, float
任何兼容的数字或单精度浮点型
DoubleTypeHandler
java.lang.Double, double
任何兼容的数字或双精度浮点型
BigDecimalTypeHandler
java.math.BigDecimal
任何兼容的数字或十进制小数类型
StringTypeHandler
java.lang.String
CHAR 和 VARCHAR 类型
ClobTypeHandler
java.lang.String
CLOB 和 LONGVARCHAR 类型
NStringTypeHandler
java.lang.String
NVARCHAR 和 NCHAR 类型
NClobTypeHandler
java.lang.String
NCLOB 类型
ByteArrayTypeHandler
byte[]
任何兼容的字节流类型
BlobTypeHandler
byte[]
BLOB 和 LONGVARBINARY 类型
DateTypeHandler
java.util.Date
TIMESTAMP 类型
DateOnlyTypeHandler
java.util.Date
DATE 类型
TimeOnlyTypeHandler
java.util.Date
TIME 类型
SqlTimestampTypeHandler
java.sql.Timestamp
TIMESTAMP 类型
SqlDateTypeHandler
java.sql.Date
DATE 类型
SqlTimeTypeHandler
java.sql.Time
TIME 类型
ObjectTypeHandler
Any
其他或未指定类型
EnumTypeHandler
Enumeration Type
VARCHAR-任何兼容的字符串类型, 作为代码存储(而不是索引)
EnumOrdinalTypeHandler
Enumeration Type
Any compatible NUMERIC or DOUBLE, as the position is stored (not the code itself). 

二、自定义类型

       例如我们在数据库经常把时间存储为一个14位的VARCHAR(YYYYmmddssss)。而前台java层为java.util.Date,在这里我们就简单做一个类型处理。
  • 定义类型处理                 
  1. package com.elements.typehandler;

  2. import java.sql.CallableStatement;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;

  8. import org.apache.ibatis.type.BaseTypeHandler;
  9. import org.apache.ibatis.type.JdbcType;

  10. public class DataTimeTypeHandler extends BaseTypeHandler<Date> {

  11.     /**
  12.      *
  13.      * 这里应该是在保存数据的时候,把前台的日期类型转换成数据的varchar 我们使用了SimpleDateFormat来转换data类型
  14.      * */
  15.     @Override
  16.     public void setNonNullParameter(PreparedStatement ps, int i,
  17.             Date parameter, JdbcType jdbcType) throws SQLException {
  18.         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  19.         ps.setString(i, sdf.format(parameter));
  20.     }

  21.     /**
  22.      * 下面三个函数应该都是返回到java层时调用,我们把varchar转换成Date类型
  23.      */
  24.     @Override
  25.     public Date getNullableResult(ResultSet rs, String columnName)
  26.             throws SQLException {
  27.         String varchartime = rs.getString(columnName);
  28.         if (varchartime!=null)
  29.         return new Date(Long.parseLong(varchartime));
  30.         else return null;
  31.     }

  32.     @Override
  33.     public Date getNullableResult(ResultSet rs, int columnIndex)
  34.             throws SQLException {
  35.         String varchartime = rs.getString(columnIndex);
  36.         if (varchartime!=null)
  37.             return new Date(Long.parseLong(varchartime));
  38.             else return null;
  39.     }

  40.     @Override
  41.     public Date getNullableResult(CallableStatement cs, int columnIndex)
  42.             throws SQLException {
  43.         String varchartime = cs.getString(columnIndex);
  44.         if (varchartime!=null)
  45.             return new Date(Long.parseLong(varchartime));
  46.             else return null;
  47.     }

  48. }
  • mybatis-config.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3.   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4.   "">

  5. <configuration>
  6.  
  7.     <properties resource="jdbc.properties" />

  8.   <typeHandlers>
  9.      <typeHandler handler="com.elements.typehandler.DataTimeTypeHandler" javaType="java.util.Date" jdbcType="VARCHAR"/>
  10.       <package name="com.elements.user"/>
  11.   </typeHandlers>

  12.     <environments default="dev">
  13.         <environment id="dev">
  14.             <transactionManager type="JDBC" />
  15.             <dataSource type="POOLED">
  16.                 <property name="driver" value="${driver}" />
  17.                 <property name="url" value="${url}" />
  18.                 <property name="username" value="${username}" />
  19.                 <property name="password" value="${password}" />
  20.             </dataSource>
  21.         </environment>
  22.     </environments>



  23.     <mappers>
  24.         <mapper class="com.elements.user.dao.UserMapper" />
  25.         
  26.     </mappers>

  27. </configuration>

User类
  1. package com.elements.user.model;

  2. import java.util.Date;

  3. public class User {
  4.     /**
  5.      * This field was generated by MyBatis Generator.
  6.      * This field corresponds to the database column user.userId
  7.      *
  8.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  9.      */
  10.     private Integer userid;

  11.     /**
  12.      * This field was generated by MyBatis Generator.
  13.      * This field corresponds to the database column user.UserName
  14.      *
  15.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  16.      */
  17.     private String username;

  18.     /**
  19.      * This field was generated by MyBatis Generator.
  20.      * This field corresponds to the database column user.UserEmail
  21.      *
  22.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  23.      */
  24.     private String useremail;

  25.     /**
  26.      * This field was generated by MyBatis Generator.
  27.      * This field corresponds to the database column user.createTime
  28.      *
  29.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  30.      */
  31.     private Date createtime;

  32.     /**
  33.      * This field was generated by MyBatis Generator.
  34.      * This field corresponds to the database column user.updateTime
  35.      *
  36.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  37.      */
  38.     private Date updatetime;

  39.     /**
  40.      * This method was generated by MyBatis Generator.
  41.      * This method returns the value of the database column user.userId
  42.      *
  43.      * @return the value of user.userId
  44.      *
  45.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  46.      */
  47.     public Integer getUserid() {
  48.         return userid;
  49.     }

  50.     /**
  51.      * This method was generated by MyBatis Generator.
  52.      * This method sets the value of the database column user.userId
  53.      *
  54.      * @param userid the value for user.userId
  55.      *
  56.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  57.      */
  58.     public void setUserid(Integer userid) {
  59.         this.userid = userid;
  60.     }

  61.     /**
  62.      * This method was generated by MyBatis Generator.
  63.      * This method returns the value of the database column user.UserName
  64.      *
  65.      * @return the value of user.UserName
  66.      *
  67.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  68.      */
  69.     public String getUsername() {
  70.         return username;
  71.     }

  72.     /**
  73.      * This method was generated by MyBatis Generator.
  74.      * This method sets the value of the database column user.UserName
  75.      *
  76.      * @param username the value for user.UserName
  77.      *
  78.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  79.      */
  80.     public void setUsername(String username) {
  81.         this.username = username == null ? null : username.trim();
  82.     }

  83.     /**
  84.      * This method was generated by MyBatis Generator.
  85.      * This method returns the value of the database column user.UserEmail
  86.      *
  87.      * @return the value of user.UserEmail
  88.      *
  89.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  90.      */
  91.     public String getUseremail() {
  92.         return useremail;
  93.     }

  94.     /**
  95.      * This method was generated by MyBatis Generator.
  96.      * This method sets the value of the database column user.UserEmail
  97.      *
  98.      * @param useremail the value for user.UserEmail
  99.      *
  100.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  101.      */
  102.     public void setUseremail(String useremail) {
  103.         this.useremail = useremail == null ? null : useremail.trim();
  104.     }

  105.     /**
  106.      * This method was generated by MyBatis Generator.
  107.      * This method returns the value of the database column user.createTime
  108.      *
  109.      * @return the value of user.createTime
  110.      *
  111.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  112.      */
  113.     public Date getCreatetime() {
  114.         return createtime;
  115.     }

  116.     /**
  117.      * This method was generated by MyBatis Generator.
  118.      * This method sets the value of the database column user.createTime
  119.      *
  120.      * @param createtime the value for user.createTime
  121.      *
  122.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  123.      */
  124.     public void setCreatetime(Date createtime) {
  125.         this.createtime = createtime ;
  126.     }

  127.     /**
  128.      * This method was generated by MyBatis Generator.
  129.      * This method returns the value of the database column user.updateTime
  130.      *
  131.      * @return the value of user.updateTime
  132.      *
  133.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  134.      */
  135.     public Date getUpdatetime() {
  136.         return updatetime;
  137.     }

  138.     /**
  139.      * This method was generated by MyBatis Generator.
  140.      * This method sets the value of the database column user.updateTime
  141.      *
  142.      * @param updatetime the value for user.updateTime
  143.      *
  144.      * @mbggenerated Wed Apr 27 15:05:09 CST 2016
  145.      */
  146.     public void setUpdatetime(Date updatetime) {
  147.         this.updatetime = updatetime;
  148.     }

  149.     @Override
  150.     public String toString() {
  151.         return "User [userid=" + userid + ", username=" + username
  152.                 + ", useremail=" + useremail + ", createtime=" + createtime
  153.                 + ", updatetime=" + updatetime + "]";
  154.     }
  155.     
  156.     
  157. }

  • UserMapper配置文件

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "" >
    3. <mapper namespace="com.elements.user.dao.UserMapper" >

    4.  
    5.   <resultMap id="BaseResultMap" type="com.elements.user.model.User" >
    6.    
    7.     <!--
    8.       WARNING - @mbggenerated
    9.       This element is automatically generated by MyBatis Generator, do not modify.
    10.       This element was generated on Wed Apr 27 15:05:09 CST 2016.
    11.     -->
    12.     <id column="userId" property="userid" jdbcType="INTEGER" />
    13.     <result column="UserName" property="username" jdbcType="VARCHAR" />
    14.     <result column="UserEmail" property="useremail" jdbcType="VARCHAR" />
    15.     <result column="createTime" property="createtime" jdbcType="VARCHAR" />
    16.     <result column="updateTime" property="updatetime" jdbcType="VARCHAR" />
    17.   </resultMap>
    18.   <sql id="Base_Column_List" >
    19.     <!--
    20.       WARNING - @mbggenerated
    21.       This element is automatically generated by MyBatis Generator, do not modify.
    22.       This element was generated on Wed Apr 27 15:05:09 CST 2016.
    23.     -->
    24.     userId, UserName, UserEmail, createTime, updateTime
    25.   </sql>
    26.   
    27.   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    28.     <!--
    29.       WARNING - @mbggenerated
    30.       This element is automatically generated by MyBatis Generator, do not modify.
    31.       This element was generated on Wed Apr 27 15:05:09 CST 2016.
    32.     -->
    33.     select
    34.     <include refid="Base_Column_List" />
    35.     from user
    36.     where userId = #{userid,jdbcType=INTEGER}
    37.   </select>
    38.   
    39.   <insert id="insert" parameterType="com.elements.user.model.User" >
    40.     <!--
    41.       WARNING - @mbggenerated
    42.       This element is automatically generated by MyBatis Generator, do not modify.
    43.       This element was generated on Wed Apr 27 15:05:09 CST 2016.
    44.     -->
    45.     insert into user (userId, UserName, UserEmail,
    46.       createTime, updateTime)
    47.     values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{useremail,jdbcType=VARCHAR},
    48.       #{createtime,jdbcType=VARCHAR}, #{updatetime,jdbcType=VARCHAR})
    49.   </insert>
    50.  
    51.  
    52. </mapper>  

注意:我们没有在mapper文件内显示的做类型转换  ,他已经默认把这两种类型(javaType="java.util.Date" jdbcType="VARCHAR" )的互转都调用我们的类型处理了                                                                           

代码下载地址:

阅读(2526) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~