文章目录
- 一、json的接收与发送
- 1.postman工具发送格式
- 2.controller接收处理
- 二、查询集合数据
- 1.利用mybatis-plus进行分页处理
- 三.读取数据配置mapper.xml
- 输出结果
提示:以下是本篇文章正文内容,下面案例可供参考
一、json的接收与发送
1.postman工具发送格式
//json样式
{
"className": "班级名称",
"classCode": "班级编码",
"classLevel": 1,
"OneManySub01": [{
"classId": "1",
"name": "姓名1",
"age": "22",
"sex": "1"
},
{
"classId": "2",
"name": "姓名2",
"age": "23",
"sex": "0"
}
],
"OneManySub02": [{
"classId": 1,
"subjectName": "科目名称",
"subjectType": "2"
},
{
"classId": 2,
"subjectName": "科目名称",
"subjectType": "1"
}
]
}
2.controller接收处理
/**
* 新增数据
* @return
*/
@ApiOperation("新增数据")
@RequestMapping("/insetOneManyMain")
public Object insetManyMain(@RequestBody JSONObject json){
JSONObject object = new JSONObject();
try {
/*
班级名称
*/
String className = json.getString("className");
/*
班级编码
*/
String classCode = json.getString("classCode");
/*
班级级别
*/
Integer classLevel = json.getInteger("classLevel");
/*
默认登录人
*/
String login="数飞";
/*
设置当前新增时间
*/
SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//修改格式
Date date = new Date();//new 当前时间
//解析OneManySub01
List<OneManySub01> oneManySub01s = json.getJSONArray("OneManySub01").toJavaList(OneManySub01.class);
//解析OneManySub02
List<OneManySub02> oneManySub02s = json.getJSONArray("OneManySub02").toJavaList(OneManySub02.class);
//循环解析数据保存
OneManySub01 manySub011 = new OneManySub01();
for (OneManySub01 manySub01 : oneManySub01s) {
manySub011.setClassId(manySub01.getClassId());
manySub011.setName(manySub01.getName());
manySub011.setAge(manySub01.getAge());
manySub011.setSex(manySub01.getSex());
tSUserService.insertOneManySub01(manySub011);
}
OneManySub02 manySub021 = new OneManySub02();
for (OneManySub02 manySub02 : oneManySub02s) {
manySub021.setClassId(manySub02.getClassId());
manySub021.setSubjectName(manySub02.getSubjectName());
manySub021.setSubjectType(manySub02.getSubjectType());
tSUserService.insertOneManySub02(manySub021);
}
OneManyMain manyMain = new OneManyMain();
manyMain.setClassName(className);
manyMain.setClassCode(classCode);
manyMain.setClassLevel(classLevel);
manyMain.setCreAtor(login);
manyMain.setCreateTime(time.format(date));
boolean fan=tSUserService.inserOneManyMain(manyMain);
if (fan){
object.put("code",200);
object.put("执行数据量",1);
object.put("msg","操作成功");
return object;
}else {
object.put("code",0);
object.put("执行数据量",0);
object.put("msg","新增失败请稍后再试");
return object;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
二、查询集合数据
1.利用mybatis-plus进行分页处理
/**
* 条件匹配
*/
@RequestMapping("/matching")
public TableDataInfo listmatching(OneManyMain OneManyMain){
startPage();
List<OneManyMain> list = tSUserService.listmatching(OneManyMain);
return getDataTable(list);
}
封装类的处理:因为是多表查询所以需要将其他表集合配置里面
三.读取数据配置mapper.xml
//type 主表就不在说了 id等后面配置resultMap用的
<resultMap type="com.inspur.crane.platform.domain.OneManyMain" id="OneManyMain">
<id column="id" property="id"></id>
<result property="className" column="class_name"/>
<result property="classCode" column="class_code"/>
<result property="classLevel" column="class_level"/>
<result property="creAtor" column="creator"/>
<result property="createTime" column="create_time"/>
<result property="modiFor" column="modifor"/>
<result property="modifyTime" column="modify_time"/>
<result property="logo" column="logo"/>
<!--
进行数据关联
property="在主表中配置的名称" javaType="List集合" ofType="封装名"
-->
<collection property="oneManySub01s" javaType="List" ofType="OneManySub01">
<id column="id" property="id"></id>
<result column="class_id" property="classId"></result>
<result column="name" property="name"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
</collection>
<!--进行数据关联-->
<collection property="oneManySub02s" javaType="List" ofType="OneManySub02">
<id column="id" property="id"></id>
<result column="class_id" property="classId"></result>
<result column="subject_name" property="subjectName"></result>
<result column="subject_type" property="subjectType"></result>
</collection>
</resultMap>
添加条件以及resultMap
<select id="listmatching" resultMap="OneManyMain">
SELECT * FROM `one_many_main` man LEFT JOIN one_many_sub_01 sua ON man.class_code=sua.class_id LEFT JOIN one_many_sub_02 sub on sua.class_id=sub.class_id
<where>
<if test="className != null "> and class_name = #{className}</if>
</where>
</select>
输出结果
{
"total": 2,
"rows": [
{
"id": 2,
"className": "一班级",
"classCode": "1",
"classLevel": 1,
"creAtor": "数飞",
"createTime": "2022-12-13 09:38:41",
"modiFor": null,
"modifyTime": null,
"logo": null,
"oneManySub01s": [
{
"id": 2,
"classId": 1,
"name": "张三",
"age": 22,
"sex": 1
}
],
"oneManySub02s": [
{
"id": 2,
"classId": 1,
"subjectName": "专科",
"subjectType": 2
}
]
}
],
"code": 200,
"msg": "查询成功"
}