Mybatis多对多,复杂增删改查(特殊需求循环插入,分组查询)

本文介绍了在Java项目中使用Mybatis处理多对多关系的实践经验,包括如何进行复杂增删改查操作,特别是面对特殊需求如循环插入和分组查询。在数据库设计时,注意字符串转换,尤其是将日期转换为数组。在添加数据时,针对自动增长主键的处理,使用jdbc的getGeneratedKeys获取并赋值。在更新多对多关联表时,由于1-2, 2-3形式的关系,通常需要先删除旧数据再插入新数据。此外,展示了@Service中处理中间表数据的循环插入技巧,以及UserMapper和XML映射文件中多对多关系的逻辑外键映射内容。" 113979788,10538435,Win10 PyCharm 中安装Pygame库的解决方案,"['Python', 'Pygame', 'PyCharm', '库安装', '错误解决']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2021.8.31
从25号开始练习复杂的mybatis多对多,从设计数据库思路到实现需求功能转移到实体项目中
1.之前很少看过字符转换的详细内容从今往后会注意字符串转换此项目为转数组(date)实体项目会有UUID生成的字符串
2.在添加时如果原表设计的首个id是自动增长的,在xml中要设置是否使用jdbc的getGenerateKeys方法获取主键并赋值到keyProperty设置的主键字段中将要插入字段对应的生成的id抽出来
3.@service中如果中间表角色id不想用逗号的形式要做循环插入数据
4.修改多表连接的中间表 因为是1-2 2-3的形式不能做修改只能删除干净后重新插入新数据

表结构
在这里插入图片描述
实体类Entity—user

package com.example.unicom.entity;

import lombok.Data;

@Data
public class User {
   
   
    private Integer id;
    private String name;
    private String phone;
    private Integer age;
    private String sex;
    private String date;
    private String name1;
    private String js_id;
}

Entity—js(死表)

package com.example.unicom.entity;

import lombok.Data;

@Data
public class Js {
   
   
    private Integer id;
    private String name;
    private String date;
}

Entity—user_role

package com.example.unicom.entity;

import lombok.Data;

@Data
public class UserJs {
   
   
    private Integer id;
    private Integer id1;
    private Integer user_id;
    private String js_id;
}

UsrtController

package com.example.unicom.controller;

import com.example.unicom.entity.User;
import com.example.unicom.entity.base.GeneralResponse;

import com.example.unicom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 孙翊轩
 * @since 2021-08-25
 */
@RestController
@RequestMapping("/isp/unicom/user")
public class UserController {
   
   
    @Autowired
    private UserService userService;
//    @Autowired
//    private UserJsService userJsService;
    @RequestMapping(value = "selectUser",method = RequestMethod.GET)
    public GeneralResponse selectUser(){
   
   
        try{
   
   
            List<User>userList=userService.selectUser();
            return new GeneralResponse("SUCCESS","查询成功",userList);
        }catch (Exception e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为人低调的孙翊轩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值