mybatis-plus主子表插入实战,2023-03-18
创始人
2025-05-30 06:32:42
0

Mybatis-plus关联关系|Mybatis-plus关联表插入|Mybatis-plus多表同时插入|mybatis-plus主外键关系插入操作

1、数据库表结构:

CREATE TABLE `car` (

`Id` bigint(12) NOT NULL AUTO_INCREMENT,

`Name` varchar(255) DEFAULT NULL,

`Ctime` datetime DEFAULT NULL,

PRIMARY KEY (`Id`)

) ENGINE=InnoDB AUTO_INCREMENT=202303160004 DEFAULT CHARSET=utf8;

CREATE TABLE `test` (

`Id` bigint(12) NOT NULL AUTO_INCREMENT,

`PId` bigint(12) DEFAULT NULL,

`Ctime` datetime DEFAULT NULL,

PRIMARY KEY (`Id`)

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

2、项目目录结构

2.1、pom.xml添加依赖

com.alibaba

fastjson

1.2.40

com.baomidou

mybatis-plus-boot-starter

3.4.2

mysql

mysql-connector-java

5.1.47

com.alibaba

druid-spring-boot-starter

1.1.20

2.2、实体类

src\main\java\com\xdy\springboot4vue\entity\Car.java

src\main\java\com\xdy\springboot4vue\entity\Test.java

package com.xdy.springboot4vue.entity;

import com.baomidou.mybatisplus.annotation.IdType;

import com.baomidou.mybatisplus.annotation.TableId;

import java.util.Date;

public class Car {

@TableId(value = "Id", type = IdType.AUTO)

private Long id;

private String name;

private Date ctime;

public Car(Long id, String name, Date ctime) {

this.id = id;

this.name = name;

this.ctime = ctime;

}

public Car( String name, Date ctime) {

this.name = name;

this.ctime = ctime;

}

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Date getCtime() {

return ctime;

}

public void setCtime(Date ctime) {

this.ctime = ctime;

}

@Override

public String toString() {

return "Car{" +

"id=" + id +

", name='" + name + '\'' +

", ctime=" + ctime +

'}';

}

}

package com.xdy.springboot4vue.entity;

import com.baomidou.mybatisplus.annotation.IdType;

import com.baomidou.mybatisplus.annotation.TableId;

import java.util.Date;

public class Test {

@TableId(value = "Id",type = IdType.AUTO)

private int id;

private Long pId;

private Date ctime;

public Test(Long pId, Date ctime) {

this.pId = pId;

this.ctime = ctime;

}

public Test(int id, Long pId, Date ctime) {

this.id = id;

this.pId = pId;

this.ctime = ctime;

}

@Override

public String toString() {

return "Test{" +

"id=" + id +

", pId=" + pId +

", ctime=" + ctime +

'}';

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public Long getpId() {

return pId;

}

public void setpId(Long pId) {

this.pId = pId;

}

public Date getCtime() {

return ctime;

}

public void setCtime(Date ctime) {

this.ctime = ctime;

}

}

2.3、接口定义

src\main\java\com\xdy\springboot4vue\mapper\CarMapper.java

src\main\java\com\xdy\springboot4vue\mapper\TestMapper.java

package com.xdy.springboot4vue.mapper;

import com.xdy.springboot4vue.entity.Car;

import org.apache.ibatis.annotations.*;

import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper

@Repository

public interface CarMapper {

@Select("select * from car")

public List find();

//insert into car(`name`,ctime) values('伍娟','2022-12-08 23:02:28')

@Insert("insert into car(`name`,ctime) values(#{name},#{ctime})")

@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "Id")

int add(Car car);

@Delete("delete from car where Id=#{id}")

int delete(int id);

}

package com.xdy.springboot4vue.mapper;

import com.xdy.springboot4vue.entity.Test;

import org.apache.ibatis.annotations.Insert;

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Options;

import org.springframework.stereotype.Repository;

@Mapper

@Repository

public interface TestMapper {

@Insert("insert into Test(`pId`,`ctime`) values(#{pId},#{ctime})")

@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "Id")

int add(Test test);

//insert into Test(`pId`,`ctime`) values(7,CURDATE())

}

2.4、控制器定义

src\main\java\com\xdy\springboot4vue\controller\CarController.java

package com.xdy.springboot4vue.controller;

import com.alibaba.fastjson.JSONObject;

import com.xdy.springboot4vue.entity.Car;

import com.xdy.springboot4vue.entity.Test;

import com.xdy.springboot4vue.mapper.CarMapper;

import com.xdy.springboot4vue.mapper.TestMapper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;

import org.springframework.web.bind.annotation.*;

import java.util.Date;

import java.util.List;

@RestController()

@RequestMapping("/api")

public class CarController {

@Autowired

private CarMapper carMapper;

@Autowired

private TestMapper testMapper;

// 事务插入主子表

@Transactional

@PostMapping("/addproductdetail")

public String insertCarTest(String name){

System.out.println("前端传过来参数为: "+name); //vue传参

Car c = new Car(name,new Date());

carMapper.add(c);

System.out.println("获取数据库返回主键,c.id = "+c.getId());

Test objTest = new Test(c.getId(),new Date());

testMapper.add(objTest);

JSONObject result = new JSONObject();

result.put("status",0);

result.put("message","主子表保存成功");

return result.toJSONString();

}

}

2.5、配置文件

src\main\resources\application.properties

server.port=3000

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/myweb?useSSL=false&useUnicode=true&characterEncoding=utf8

spring.datasource.username=你的用户名

spring.datasource.password=你的密码

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

2.6、测试

http://localhost:3000/api/addproductdetail?name=樊奥

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...