一:对象转json, 存入数据库
也就是在对象中加几个注解,例如我写的这个例子:
public class Situation {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
@TableField(typeHandler = JacksonTypeHandler.class)
private List<Switch> switches;
@TableField(typeHandler = JacksonTypeHandler.class)
private List<TerminalInfo> terminals;
@TableField(typeHandler = JacksonTypeHandler.class)
private List<Link> links;
}
加了@TableField(typeHandler = JacksonTypeHandler.class)注解, 就可以存入对象为json格式
二:json转对象(从数据库中读取数据)
1. mybatis
我用的是注解形式,再Mapper里面加入注解,举个例子:
@Select("select * from situation_info where name = #{name}")
@Results({
@Result(column = "switches", property = "switches", typeHandler = JacksonTypeHandler.class),
@Result(column = "terminals", property = "terminals", typeHandler = JacksonTypeHandler.class),
@Result(column = "links", property = "links", typeHandler = JacksonTypeHandler.class)
})
Situation getSituationByName(String name);
加个@Results注解 就ok了
2. mybatis-plus
在所需对象上面加入一个注解即可 如:
@TableName(value = "situation_info",autoResultMap = true)
public class Situation {
@TableId(type = IdType.AUTO)
private Integer id;
}
这种就行
|