mybatis-plus主子表插入实战,2023-03-18

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添加依赖

<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=樊奥

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

赵海燕

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

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

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

打赏作者

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

抵扣说明:

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

余额充值