Mybatis学习之自定义typehandler(四)

代码结构:


MyStringTypeHandler.java

package com.kinsey.woo.handler;

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

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.apache.log4j.Logger;

@MappedTypes({String.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class MyStringTypeHandler implements TypeHandler{
	
	
	private Logger log = Logger.getLogger(MyStringTypeHandler.class);

	@Override
	public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException{
		log.info("使用我的自定义setParameter");
		ps.setString(i, parameter);
	}
			 
	@Override
	public String getResult(ResultSet rs, String columnName) throws SQLException {
		log.info("使用我的自定义getResult(ResultSet rs, String columnName)");
		return rs.getString(columnName);
	}
	
	@Override
	public String getResult(ResultSet rs, int columnIndex) throws SQLException {
		log.info("使用我的自定义getResult(ResultSet rs, int columnIndex)");
		return rs.getString(columnIndex);
    }
			       
    @Override
    public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
    	  log.info("使用我的自定义getResult(CallableStatement cs, int columnIndex)");
      return cs.getString(columnIndex);
    }

				
}

RoleMapper.java
package com.learn.chapter3.mapper;

import com.learn.chapter3.po.Role;

public interface RoleMapper {
	public Role getRole(long id);
}

RoleMapper.xml




 

 	
 		
 		
 		
 	
 	
 	
 	
 	
 	
 
 

Role.java

package com.learn.chapter3.po;

import org.apache.ibatis.type.Alias;

@Alias("role")
public class Role {
	private Long id;
	private String roleName;
	private String note;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	
	
	
}

mybatis-config.xml





		
	
		
	
	
	
	
		
	
	
	
	
		
			
				
			
			
				
				
				
				
			
		
	
	
	
	
	
		
	



结果:

Mybatis学习之自定义typehandler(四)_第1张图片






你可能感兴趣的:(MyBatis)