1.库表设计
2.实体类
mport cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data;
/**
* @author YaoHongli
* @since 2023-08-07 16:31:26
*/
@Data
@TableName(value = "goods_bill",autoResultMap = true)
public class GoodsBill {
private Integer id;
@TableField(value = "goods",typeHandler = JacksonTypeHandler.class)
private JSONArray goods;
@TableField(value = "user_message",typeHandler = JacksonTypeHandler.class)
private JSONObject userMessage;
}
3.Mapper层
@Mapper
public interface GoodsBillMapper {
void addGoodsBill(GoodsBill goodsBill);
List<GoodsBill> getGoodsList();
}
4.Service层
public interface GoodsBillService{
void addGoodsBill(GoodsBill goodsBill);
List<GoodsBill> getGoodsList();
}
5.ServiceImpl
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GoodsBillServiceImpl implements GoodsBillService {
@Autowired
private GoodsBillMapper goodsBillMapper;
@Override
public void addGoodsBill(GoodsBill goodsBill){
goodsBillMapper.addGoodsBill(goodsBill);
}
@Override
public List<GoodsBill> getGoodsList() {
return goodsBillMapper.getGoodsList();
}
}
6.Controller
@RestController
public class GoodsBillController {
@Autowired
private GoodsBillService goodsBillService;
@RequestMapping("/goodsBillList")
public List<GoodsBill> goodsBillList() {
List<GoodsBill> goodsBillList = goodsBillService.getGoodsList();
return goodsBillList;
}
@RequestMapping("/addGoodsBill")
public String addGoodsBill(@RequestBody GoodsBill goodsBill){
goodsBillService.addGoodsBill(goodsBill);
return goodsBill.toString();
}
7.Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yhl.log.mapper.GoodsBillMapper">
<resultMap id="BaseResultMap" type="com.yhl.log.entity.GoodsBill">
<id column="id" jdbcType="BIGINT" property="id"></id>
<result column="goods" jdbcType="OTHER" property="goods" javaType="cn.hutool.json.JSONArray" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"></result>
<result column="user_message" jdbcType="OTHER" property="userMessage" javaType="cn.hutool.json.JSONObject" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"></result>
</resultMap>
<select id="getGoodsList" resultMap="BaseResultMap">
select * from goods_bill
</select>
<insert id="addGoodsBill" useGeneratedKeys="true" keyProperty="id">
insert into goods_bill(goods,user_message)
values(#{goods,jdbcType=OTHER,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},#{userMessage,jdbcType=OTHER,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler})
</insert>
</mapper>
8.application文件
server:
port: 9999
spring:
datasource:
url: "jdbc:mysql://localhost/jsontest"
username: "root"
password: "123456"
mybatis:
mapper-locations: classpath:/mapper/*Mapper.xml
type-handlers-package: com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler
configuration:
map-underscore-to-camel-case: true
9.pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
</dependencies>
10.获取列表
11.新增订单
请求头是contentType:application/json
参考博客有Mybatis读取和存储json类型的数据_怪 咖@的博客-CSDN博客SpringBoot中MyBatis 处理 MySQL5.7 的json字段数据_springboot mybatis json字段_LiXiuu的博客-CSDN博客