BaseTypeHandler转换--学习笔记

mybati类型转化

mybatisxml文件配置
//


    
    
    



    




    
        
        

        
        
        
        
        
            
            
            
            
        
    




    
    
    

映射文件RoleMapper.xml配置





    insert into behavior(id,role_name,date,note) values(#{id},#{rolename},#{date},#{note})

pojo类
import java.util.Date;

public class Role {
    private int id;
    private String  rolename;
    private String note;
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", rolename='" + rolename + '\'' +
                ", note='" + note + '\'' +
                ", date=" + date +
                '}';
    }

    public void setId(int 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;
    }
}

mapper接口

public interface Rolempper {

   public List<Role> get_info(int id) throws Exception;
   public  Role find_info(int id);
   public int insert_talbe(Role role);

}

通过继承BaseTypeHandler类进行转换

public class DateTyperHeader extends BaseTypeHandler <Date>{

//javaType转换为jdbcType
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        //将Date类型转换为long类型存储在数据库中
        long time= date.getTime();
        preparedStatement.setLong(i,time);
    }
//剩下三个是jdbcType转换为javaType
    @Override
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        //将long类转化为Date类型
        long along=resultSet.getLong(s);
        Date date=new Date(along);
        return date;
    }

    @Override
    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        long along=resultSet.getLong(i);
        Date date=new Date(along);
        return date;
}

    @Override
    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        long along=callableStatement.getLong(i);
        Date date=new Date(along);
        return date;

    }
}

BaseTypeHandler转换--学习笔记_第1张图片数据库储存图片
实现类

   InputStream reader = Resources.getResourceAsStream( "mybatis_cfg.xml");
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

        SqlSession session= sessionFactory.openSession();
        Rolempper mapper=session.getMapper(Rolempper.class);
        RowBounds rowBounds=new RowBounds(0,5);
        //Role user=mapper.get_info(1);

        Role u=new Role();
        u.setId(5);
        u.setRolename("lee_1");
        u.setNote("note_test");
        u.setDate(new Date());
        Role user= mapper.find_info(4);
       // int i=mapper.insert_talbe(u);

        System.out.println(user.getDate());
        session.commit();

BaseTypeHandler转换--学习笔记_第2张图片最后的打印结果,数据库中存储的是为long类型的毫秒值,而编译器打印的是是具体时间,转换成功

你可能感兴趣的:(mybatis)