JSON格式数据存储问题一
问题描述:
在mysql5.7的情况下,将某一类型的数据以JSON格式存储到数据库中的某一字段中,并能够以原有类型从数据库中取出.
解决办法:
将某一类型的数据以JSON格式存储到数据库中的某一字段中
以Map<Integer, String>类型的数据为例,将其存入数据库中的map字段,并且可读出为Map<Integer, String>类型的数据:
(1)首先建立一个包含Map<Integer, String>类型的数据的实体类:
如下:
-
建立实体类对应的接口:
(3)建立JsonTypeHandler 继承 BaseTypeHandler,在JsonTypeHandler中主要重写以下两个方法:
/** 把Java类型参数转换为对应的数据库类型
* @param ps 当前的PreparedStatement对象
* @param i 当前参数位置
* @param parameter 当前参数的Java对象
* @param jdbcType 当前参数的数据库类型
* @throws SQLException*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter,JdbcType jdbcType) throws SQLException {
// TODO Auto-generated method stub
ps.setString(i, JSON.toJSONString(parameter));
}
/*
* 将数据库中某一字段的数据转换成Hashmap类型输出
* */
@SuppressWarnings("unchecked")
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
// TODO Auto-generated method stub
String json = rs.getString(columnName);
/** jackjson
*JSONObject obj = JSONObject.fromObject(json);
*Object map = JSONObject.toBean(obj,HashMap.class);
*/
HashMap map = JSON.parseObject(json, HashMap.class);
return (T) map;
}
(4)在mybatis-config.xml文件中添加<typeHanlders>标签,其中handler填写JsonTypeHandler的全类名,javaType填写JsonTypeHandler适用于javaBean中哪一类型的数据,可以不填.jdbcType填写JsonTypeHandler适用于数据库中哪一类型的数据,可以不填,在mapper.xml文件中补充即可.
<typeHandlers>
<typeHandler handler="com.das.java.typehandler.JsonTypeHandler" jdbcType="VARCHAR"/>
</typeHandlers>
(5)编辑接口对应的mapper.xml文件.
<resultMap type="com.das.java.bean.Students" id="userVO">
<result property="id" column="id"/>
<result property="student_name" column="student_name"/>
<result property="student_age" column="student_age"/>
<result property="student_gender" column="student_gender"/>
<result property="map" column="map" typeHandler="com.das.java.typehandler.JsonTypeHandler"/>
</resultMap>
<insert id="addStudent" parameterType="com.das.java.bean.Students">
insert into students(student_name,student_age,student_gender,map) values
<foreach collection="student" item="stu" separator=",">
(#{stu.student_name },
#{stu.student_age},
#{stu.student_gender}, #{stu.map,javaType=java.util.Map,jdbcType=VARCHAR,typeHandler=com.das.java.typehandler.JsonTypeHandler})
</foreach>
</insert>
<select id="getStudentById" resultMap="userVO">
select id student_name,student_age,student_gender,map from students where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
(6)编写测试类
/*
* 测试Map<Integer,String>
* */
@Test
public void test1() throws IOException {
SqlSessionFactory sqlSession = getSqlSession();
SqlSession session = sqlSession.openSession();
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1,"xiaoming");
map.put(2,"zhuhui");
Students student = new Students(1,"xiaoli",22,"0",map);
ArrayList<Students> array = new ArrayList<Students>();
array.add(student);
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
mapper.addStudent(array);
session.commit();
}finally {
session.close();
}
}
/*
* 测试从数据库中读取JSON类型数据
* */
@Test
public void test2() throws IOException {
SqlSessionFactory sqlSession = getSqlSession();
SqlSession session = sqlSession.openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
ArrayList<Integer> array = new ArrayList<Integer>();
array.add(57);
ArrayList<Students> student = mapper.getStudentById(array);
System.out.println(student);
session.commit();
}finally {
session.close();
}
}