mybatis-自定义类型处理器


typeHandler

在Mybatis映射中,能够自动将jdbc类型映射为java类型

之所以数据能够接受成功,是因为有默认的类型处理器。

自定义类型处理器

  • 数据存储时,自动将list集合,转为字符串(格式自定义)
  • 数据查询时,将查到的字符串再转为List集合

1.在数据库中添加favorite

alter table user add coulmn favorute vatchar(255);

2.在User类中,添加相应的属性

private List<String> favorite;

public setFavorite(String favorite){
    this.favorite=favorite;
}
public getFavorite(){
    return favorite;
}

3.自定义一个类型转换器

package com.ethan.dao;

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 java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;



@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListVarcharHandler implements TypeHandler<List<String>> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
        StringBuffer sb = new StringBuffer();
        for (String string : strings) {
            sb.append(string).append(",");
        }
        preparedStatement.setString(i,sb.toString());
    }

    @Override
    public List<String> getResult(ResultSet resultSet, String s) throws SQLException {
        String favs = resultSet.getString(s);
        if (favs!=null){
            return Arrays.asList(favs.split(","));
        }

        return null;
    }

    @Override
    public List<String> getResult(ResultSet resultSet, int i) throws SQLException {

        String favs = resultSet.getString(i);
        if (favs!=null){
            return Arrays.asList(favs.split(","));
        }

        return null;
    }

    @Override
    public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException {
        String favs = callableStatement.getString(i);
        if (favs!=null){
            return Arrays.asList(favs.split(","));
        }
        return null;
    }
}
  • @MappedJdbcTypes注解指定要处理的jdbc数据类型
  • @MappedTypes指定要处理的java类型
  • setParameter 设置参数
  • getResult 处理查询

4 、Mapper

<insert id="addUser" parameterType="user">
        insert into test01.user(username, address,favorite) values
        (#{username},#{address},#{favorite,typeHandler=com.ethan.dao.ListVarcharHandler})
    </insert>

5、测试

@Test
    public void test01() throws IOException {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = factory.openSession();
        User user = new User();
        user.setUsername("前三");
        user.setAddress("北极");
        List<String> favorite = new ArrayList<String>();
        favorite.add("足球");
        favorite.add("篮球");
        favorite.add("乒乓球");
        user.setFavorite(favorite);
        int i = sqlSession.insert("com.ethan.dao.UserMapper.addUser", user);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }

6、测试结果


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM