-
The background is experiencing this issue, reported the wrong jar after you've updated the new package, then we ask the original database has a data field is changed to keep the json (real) format, with the underlying database link function group has a good package how to deal with json format maps. Data Migration Group, has its own set of database links from the new package so there will be no problem for json format mapping process.
-
These are the background
Now to solve this problem json format maps
First we have to create your own 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;
}
}
Which buildConfiguration () This method is the most critical
But also realize their 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;
}
}
Just put the main code listed, and if we do not integrate Baidu springboot can integrate mybatis
or reference https://www.cnblogs.com/changhai/p/8891190.html
Reproduced in: https: //www.jianshu.com/p/dd115a5dd2ca