关于Mybatis no typehandler found for property xxx(代表某个字段)

  • 遇到这个问题的背景是,更新完新jar包之后报这个错,后来问大家原来数据库有一个字段是改为存的json(real)格式的数据,功能组用的底层数据库链接已经封装好了怎么处理json格式映射。数据迁移组,又自己从新封装了一套数据库链接没有对json格式映射处理所以会出现这个问题。

  • 以上是背景

现在要解决这个json格式映射问题

首先我们要创建自己的SqlSessionFactory

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sl.handler.MyBatisHandler;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author shuliangzhao
 * @Title: MyBatisConfigurtion
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/5/28 21:52
 */
@Configuration
public class MyBatisConfigurtion {

    @Value("${mybatis.typeAliasesPackage}")
    private String typeAliasesPackage;

    //  配置mapper的扫描,找到所有的mapper.xml映射文件
    @Value("${mybatis.mapperLocations}")
    private String mapperLocations;


    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    @Primary
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        sqlSessionFactoryBean.setConfiguration(buildConfiguration());
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplate() throws Exception {
        return  new SqlSessionTemplate(sqlSessionFactory());
    }

    @Bean
    @Primary
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    private org.apache.ibatis.session.Configuration buildConfiguration() {
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.getTypeHandlerRegistry().register(JSONObject.class, JdbcType.VARCHAR, MyBatisHandler.class);
        configuration.getTypeHandlerRegistry().register(JSONArray.class, JdbcType.VARCHAR, MyBatisHandler.class);
        return configuration;
    }


}

其中 buildConfiguration() 这个方法这个是最关键的

还要实现自己的Handler


import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

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

/**
 * @author shuliangzhao
 * @Title: MysqlHandler
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/5/28 22:20
 */
public class MyBatisHandler extends BaseTypeHandler {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSON.toJSONString(o));
    }

    @Override
    public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String sqlJson = resultSet.getString(s);
        if (null != sqlJson) {
            return JSON.parse(sqlJson);
        }
        return null;
    }

    @Override
    public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String sqlJson = resultSet.getString(i);
        if (null != sqlJson) {
            return JSON.parse(sqlJson);
        }
        return null;
    }

    @Override
    public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String sqlJson = callableStatement.getString(i);
        if (null != sqlJson) {
            return JSON.parse(sqlJson);
        }
        return null;
    }
}
 
 

只是把主要代码列出来了,如果大家不会整合可以百度springboot 整合mybatis
或者参考 https://www.cnblogs.com/changhai/p/8891190.html

你可能感兴趣的:(关于Mybatis no typehandler found for property xxx(代表某个字段))