mybatis typeHandler自定义类型转换器使用

实体类

package com.ghgcn.mybatis.entity;

public class PhoneNumber {

    private String countryCode;
    private String stateCode;
    private String number;

    public PhoneNumber() {
    }

    public PhoneNumber(String countryCode, String stateCode, String number) {
        super();
        this.countryCode = countryCode;
        this.stateCode = stateCode;
        this.number = number;
    }

    public PhoneNumber(String string) {
        if (string != null) {
            String[] parts = string.split("-");
            if (parts.length > 0)
                this.countryCode = parts[0];
            if (parts.length > 1)
                this.stateCode = parts[1];
            if (parts.length > 2)
                this.number = parts[2];
        }
    }

    public String getAsString() {
        return countryCode + "-" + stateCode + "-" + number;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getStateCode() {
        return stateCode;
    }

    public void setStateCode(String stateCode) {
        this.stateCode = stateCode;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "PhoneNumber [countryCode=" + countryCode + ", stateCode=" + stateCode + ", number=" + number + "]";
    }

}

package com.ghgcn.mybatis.entity;

import java.io.Serializable;
import java.util.Date;

public class Student implements Serializable {
    
    private static final long serialVersionUID = -7301987836735803448L;
    private Integer studId;
    private String name;
    private String email;
    private Date dob;
    
    private  PhoneNumber phone;
    
    public Integer getStudId() {
        return studId;
    }
    
    public void setStudId(Integer studId) {
        this.studId = studId;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public Date getDob() {
        return dob;
    }
    
    public void setDob(Date dob) {
        this.dob = dob;
    }

    
    public PhoneNumber getPhone() {
        return phone;
    }

    
    public void setPhone(PhoneNumber phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student [studId=" + studId + ", name=" + name + ", email=" + email + ", dob=" + dob + ", phone=" + phone
                + "]";
    }

    
    
    
    
}

typehandler

package com.ghgcn.mybatis.typehandlers;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import com.ghgcn.mybatis.entity.PhoneNumber;


public class PhoneTypeHandler extends BaseTypeHandler {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, PhoneNumber parameter, JdbcType jdbcType)
            throws SQLException {
        
        ps.setString(i, parameter.getAsString());
        
    }

    @Override
    public PhoneNumber getNullableResult(ResultSet rs, String columnName) throws SQLException {
       
        return new PhoneNumber(rs.getString(columnName));
    }

    @Override
    public PhoneNumber getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        
        return new PhoneNumber(rs.getString(columnIndex));
    }

    @Override
    public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return new PhoneNumber(cs.getString(columnIndex));
    }

}

config.xml中


        
    
  • 完配置



    
    
    
        
        
        
        
        
        
        
        

        
        
        
        
        
        
        
        
        
        
    

    
    
        
    


        
    
    

        
            

            
                
                
                
                
            
        
    


    
        

    

mapper.xml





    
        
        
        
        
        
        

    


    
        stud_id,name,email,dob,phone
    

    

    
    
        INSERT INTO
        STUDENT  (NAME,EMAIL,DOB,PHONE)
        VALUES(#{name},#{email},#{dob},#{phone})
    
    
    
    UPDATE STUDENT
    SET NAME=#{name},
    EMAIL=#{email},
    DOB=#{dob},
    PHONE=#{phone}
    WHERE stud_id = #{studId}
    

测试

 SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();

        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        Student student = studentMapper.findStudentById(7);

        logger.info(" findStudentById  {} ", student);

2018-04-19 17:44:34.400 [main] INFO com.ghgcn.mybatis.test.StudentTest2 - findStudentById Student [studId=7, name=phone1, [email protected], dob=Thu Apr 19 00:00:00 CST 2018, phone=PhoneNumber [countryCode=0086, stateCode=0755, number=13410110407]]

你可能感兴趣的:(mybatis typeHandler自定义类型转换器使用)