SpringBoot+mybatis+mysql实现多表插入 - Go语言中文社区

SpringBoot+mybatis+mysql实现多表插入


SpringBoot+mybatis+mysql实现多表插入

1.新建一个springboot工程
2.在数据库中我们新建2张表,建表语句如下:
job表:

CREATE TABLE `job` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `job` varchar(255) DEFAULT NULL,
  `role_id` int(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


role表:

CREATE TABLE `role` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  `age` int(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3.RoleController

package com.springboot.controller;

import com.springboot.beans.HttpResponseEntity;
import com.springboot.common.Constans;
import com.springboot.dao.entity.Role;
import com.springboot.service.RoleService;
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;

@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    @RequestMapping(value = "/addRoleJob",method = RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity addRoleJob(@RequestBody Role role){
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity ();
        try{
            int addRoleJob = roleService.addRoleJob(role);
            httpResponseEntity.setData ( addRoleJob );
            httpResponseEntity.setCode ( Constans.SUCCESS_CODE );
            httpResponseEntity.setMessage(Constans.ADD_SUCCESS_MESSAGE);
        }catch (Exception e){
            httpResponseEntity.setCode ( Constans.ADD_EXIST_CODE );
            httpResponseEntity.setMessage ( Constans.ADD_EXIST_MESSAGE );
        }
        return httpResponseEntity;
    }
}

4.RoleService

package com.springboot.service;

import com.springboot.dao.JobMapper;
import com.springboot.dao.RoleMapper;
import com.springboot.dao.entity.Job;
import com.springboot.dao.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private JobMapper jobMapper;
    @Transactional
    public int addRoleJob(Role role) {
        roleMapper.addRoleJob(role);
        Job job = new Job();
        job.setRoleId(role.getId());
        job.setJob(role.getJob());
        int i = jobMapper.addJob(job);
        return i;
    }
}

5.RoleMapper

int addRoleJob(Role role);

6.RoleMapper.xml

<insert id="addRoleJob" parameterType="com.springboot.dao.entity.Role" keyProperty="id" keyColumn="id" useGeneratedKeys="true">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into role (name, sex,
    age)
    values (#{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},
    #{age,jdbcType=INTEGER})
  </insert>

7.在Role实体里加一个 job字段,生成get,set方法

private String job;

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

8.JobMapper

int addJob(Job job);

9.JobMapper.xml

<insert id="addJob" parameterType="com.springboot.dao.entity.Job" keyProperty="id" keyColumn="id" useGeneratedKeys="true">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into job (job, role_id
    )
    values (#{job,jdbcType=VARCHAR}, #{roleId,jdbcType=INTEGER}
    )
  </insert>

10.代码就写完了,运行一下,我们用postman测试一下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
完成了。

你还在犹豫什么?赶紧自己动手看看吧!

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_42370891/article/details/84494385
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢