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、项目目录结构
![](https://img-blog.csdnimg.cn/img_convert/2f66c488ddf98d536ba11821ac7ce291.png)
2.1、pom.xml添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
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<Car> 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=樊奥
![](https://img-blog.csdnimg.cn/img_convert/f65d83a81054c94ee796165c65e031fa.png)
![](https://img-blog.csdnimg.cn/img_convert/b1e976eb4e7d3d8a2c7f8fa40f245556.png)
![](https://img-blog.csdnimg.cn/img_convert/dea9fbda34ffc30858c374ce11e62038.png)
![](https://img-blog.csdnimg.cn/img_convert/64e43b31f367dd26a7e8afbabbfa0185.png)