-
遇到這個問題的背景是继谚,更新完新jar包之后報這個錯花履,后來問大家原來數(shù)據(jù)庫有一個字段是改為存的json(real)格式的數(shù)據(jù)诡壁,功能組用的底層數(shù)據(jù)庫鏈接已經(jīng)封裝好了怎么處理json格式映射荠割。數(shù)據(jù)遷移組,又自己從新封裝了一套數(shù)據(jù)庫鏈接沒有對json格式映射處理所以會出現(xiàn)這個問題夺克。
-
以上是背景
現(xiàn)在要解決這個json格式映射問題
首先我們要創(chuàng)建自己的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() 這個方法這個是最關(guān)鍵的
還要實現(xiàn)自己的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<Object> {
@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