SpringBoot+mybatis实现增删改查

SpringBoot+mybatis实现增删改查

1.创建一个项目。
2.用mybatis逆向工程创建,还不会逆向工程的看我上一篇文章
Mybatis逆向工程创建方法
3.创建好会有如下图的三个文件
SpringBoot+mybatis实现增删改查_第1张图片
4.新建一个controller文件夹在里面新建一个PersonController类。
5.编写如下代码:

package com.springboot.controller;


import com.springboot.beans.HttpResponseEntity;
import com.springboot.common.Constans;
import com.springboot.dao.entity.PersonEntity;
import com.springboot.service.PersonService;
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 PersonController {
    @Autowired//注入(自动找Service)
    /**
     * 实例化Service
     */
    private PersonService personService;
    //查询数据操作
    @RequestMapping(value = "/selectPerson",method = RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity selectPerson(@RequestBody PersonEntity personEntity){
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity ();
        try {
            PersonEntity selectPerson = personService.selectPerson(personEntity);
            httpResponseEntity.setData ( selectPerson );
            httpResponseEntity.setCode(Constans.SUCCESS_CODE);//自定义成功状态码
            httpResponseEntity.setMessage(Constans.SELECT_SUCCESS_MESSAGE);//自定义成功状态消息

        } catch (Exception e) {
            httpResponseEntity.setCode(Constans.ADD_EXIST_CODE);//自定义失败状态码
            httpResponseEntity.setMessage(Constans.SELECT_EXIST_MESSAGE);//自定义失败状态消息
        }
        return httpResponseEntity;
    }
    //----------------同理下面代码----------------
    //增加数据操作
    @RequestMapping(value = "/addPerson",method = RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity addPerson(@RequestBody PersonEntity personEntity){
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity ();
        try{
            int addInfoPerson = personService.addPerson(personEntity);
            httpResponseEntity.setData ( addInfoPerson );
            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;
    }
    //删除数据操作
    @RequestMapping(value = "/deletePerson",method = RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity deletePerson(@RequestBody PersonEntity personEntity){
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity ();
        try{
            int deleteInfoPerson = personService.deletePerson(personEntity);
            httpResponseEntity.setData ( deleteInfoPerson );
            httpResponseEntity.setCode(Constans.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constans.DELETE_SUCCESS_MESSAGE);
        }catch (Exception e){
            httpResponseEntity.setCode(Constans.ADD_EXIST_CODE);
            httpResponseEntity.setMessage(Constans.DELETE_EXIST_MESSAGE);
        }
        
        return httpResponseEntity;
    }
    //修改数据操作
    @RequestMapping(value = "/updatePerson",method = RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity updatePerson(@RequestBody PersonEntity personEntity){
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try{
            int updateInfoPerson = personService.updatePerson(personEntity);
            httpResponseEntity.setData(updateInfoPerson);
            httpResponseEntity.setCode(Constans.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constans.UPDATE_SUCCESS_MESSAGE);
        }catch (Exception e){
            httpResponseEntity.setCode(Constans.ADD_EXIST_CODE);
            httpResponseEntity.setMessage(Constans.UPDATE_EXIST_MESSAGE);
        }
       
        return httpResponseEntity;
    }
}

6.新建一个service文件夹,在里面新建一个PersonService类
代码如下:

package com.springboot.service;

import com.springboot.dao.PersonEntityMapper;
import com.springboot.dao.entity.PersonEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PersonService {
    @Autowired
    private PersonEntityMapper personEntityMapper;
    @Transactional
    /*
    根据ID查询信息
     */
    public PersonEntity selectPerson(PersonEntity personEntity){
        PersonEntity personInfoEntity = personEntityMapper.selectPerson(personEntity.getId () );
        return personInfoEntity;
    }
    /*
    添加信息
     */
    public int addPerson(PersonEntity personEntity) {
        int personInfoEntity = personEntityMapper.addPerson(personEntity);
        return personInfoEntity;
    }
    /*
    根据ID删除信息
     */
    public int deletePerson(PersonEntity personEntity) {
        int personInfoEntity = personEntityMapper.deletePerson(personEntity.getId ());
        return personInfoEntity;
    }
    /*
    修改信息
     */
    public int updatePerson(PersonEntity personEntity) {
        int personInfoEntity = personEntityMapper.updatePerson(personEntity);
        return personInfoEntity;
    }
}

7.我们在PersonEntityMapper接口里面写上
如下代码:

package com.springboot.dao;

import com.springboot.dao.entity.PersonEntity;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonEntityMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table person
     *
     * @mbg.generated
     */
    int deleteByPrimaryKey(String id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table person
     *
     * @mbg.generated
     */
    int insert(PersonEntity record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table person
     *
     * @mbg.generated
     */
    int insertSelective(PersonEntity record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table person
     *
     * @mbg.generated
     */
    PersonEntity selectByPrimaryKey(String id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table person
     *
     * @mbg.generated
     */
    int updateByPrimaryKeySelective(PersonEntity record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table person
     *
     * @mbg.generated
     */
    int updateByPrimaryKey(PersonEntity record);
//----------------------------下面四句需要我们来自己写--------------------------------------------------
    PersonEntity selectPerson(String id);


    int addPerson(PersonEntity personEntity);

    int deletePerson(String id);

    int updatePerson(PersonEntity personEntity);
}

8.下面我们需要在PersonEntityMapper.xml里面进行配置
因为PersonEntityMapper.xml文件用逆向工程可以自动创建,我们只需要修改id就行。也可以根据自己的实际情况修改SQL语句。
这里我们以查询为例,删除,增加,修改都类似。


说明:id="selectPerson"里面的selectPerson为PersonController类里面
value = “/selectPerson” 这个。

这样就算全部完成了

9.附赠自定义状态码类

package com.springboot.common;

public class Constans {

    /**
     * Descriptions:公用成功失败状态码
     */
    public static final String SUCCESS_CODE = "666"; //成功状态码
    public static final String ADD_EXIST_CODE = "20001"; //失败状态码
    public static final String ADD_EXIST_MESSAGE = "添加失败"; //添加失败
    public static final String SELECT_EXIST_MESSAGE ="查询失败";
    public static final String SELECT_SUCCESS_MESSAGE ="查询成功";
    public static final String ADD_SUCCESS_MESSAGE ="添加成功";
    public static final String UPDATE_EXIST_MESSAGE ="修改失败";
    public static final String UPDATE_SUCCESS_MESSAGE ="修改成功";
    public static final String DELETE_EXIST_MESSAGE ="删除失败";
    public static final String DELETE_SUCCESS_MESSAGE ="删除成功";

}

10.接下来我们用Postman进行测试
首先我们先增加3条数据
SpringBoot+mybatis实现增删改查_第2张图片
说明:本机IP地址+:8080+/+接口名(如:查询接口selectPerson,修改接口updatePerson,删除接口deletePerson,增加接口addPerson)
SpringBoot+mybatis实现增删改查_第3张图片
SpringBoot+mybatis实现增删改查_第4张图片

这样数据库中也可以看到
SpringBoot+mybatis实现增删改查_第5张图片
接下来我们进行修改
SpringBoot+mybatis实现增删改查_第6张图片

下面进行删除操作
删除ID为3的数据
SpringBoot+mybatis实现增删改查_第7张图片

进行查找操作
查找ID为1的数据
SpringBoot+mybatis实现增删改查_第8张图片

表演完事
。。。。。。。。。。。。。。开溜。。。

你可能感兴趣的:(SpringBoot)