springboot的基本增删改查

springboot增删改查

  • 1、编写MybatisController.java
  • 2、在postman中访问接口(先启动项目)
    • 2-1、增
    • 2-2、删
    • 2-3、改
    • 2-4、查

参考:
springboot搭建
springboot集成mysql
springboot集成mybatis【使用generatorConfig.xml配置自动生成代码】

在集成mysql和mybatis后,对数据进行简单的增删改查操作

参考springboot集成mybatis中的代码继续下面的步骤(springboot集成mybatis【使用generatorConfig.xml配置自动生成代码】)

1、编写MybatisController.java

package com.study.mybatis.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.study.mybatis.dao.MybatisUserMapper;
import com.study.mybatis.entity.MybatisUser;
import com.study.mybatis.entity.MybatisUserImpl;
import com.study.mybatis.entity.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


import java.util.List;


/**
* @Author: JING
* @Created: 2022/2/8 17:38
* @Description:
* @Version: 1.0.0
* @Since JDK 1.8
*/
@RestController
@EnableAutoConfiguration
@RequestMapping("/study/web")
public class MybatisController {
    @Autowired
    MybatisUserMapper mybatisUserMapper;


    /**
     * 查询
     */
    @CrossOrigin
    @RequestMapping(value = "/getMybatisUserInfo",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    List<MybatisUser> getMybatisUserInfo(){
        MybatisUserImpl mybatisUserImpl = new MybatisUserImpl();
        List<MybatisUser> mybatisUserList = mybatisUserMapper.selectByExample(mybatisUserImpl);
        return mybatisUserList;
    }


    /**
     * 新增
     * @param userInfo 传参对象
     * @return 返回结果
     */
    @CrossOrigin
    @RequestMapping(value = "/insertMybatisUserInfo",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    JSONObject insertMybatisUserInfo(@RequestBody UserInfo userInfo){
        MybatisUser mybatisUser = JSON.toJavaObject((JSONObject) JSONObject.toJSON(userInfo),MybatisUser.class);
        mybatisUserMapper.insert(mybatisUser);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("新增数据成功:",mybatisUser);


        return jsonObject;
    }


    /**
     * 以参数形式新增数据
     * @param id id
     * @param userName userName
     * @param userId userId
     * @param address address
     * @return 返回结果
     */
    @CrossOrigin
    @RequestMapping(value = "/insertMybatisUserInfo2",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    JSONObject insertMybatisUserInfo2(@Param("id")Integer id,@Param("userName") String userName,@Param("userId")String userId,@Param("address")String address){
        MybatisUser mybatisUserParam = new MybatisUser();
        mybatisUserParam.setId(id);
        mybatisUserParam.setUserName(userName);
        mybatisUserParam.setUserId(userId);
        mybatisUserParam.setAddress(address);
        mybatisUserMapper.insert(mybatisUserParam);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("参数新增数据:",mybatisUserParam);
        return jsonObject;


    }


    /**
     * 修改数据
     * @param userInfo 修改传参
     * @return 返回结果
     */
    @CrossOrigin
    @RequestMapping(value = "/updateMybatisUserInfo",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    JSONObject updateMybatisUserInfo(@RequestBody UserInfo userInfo){
        MybatisUserImpl mybatisUserImpl = new MybatisUserImpl();
        MybatisUserImpl.Criteria criteria = mybatisUserImpl.createCriteria();
        criteria.andIdEqualTo(userInfo.getId());
        MybatisUser mybatisUser = JSON.toJavaObject((JSONObject) JSONObject.toJSON(userInfo),MybatisUser.class);
        mybatisUserMapper.updateByExample(mybatisUser,mybatisUserImpl);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("修改数据成功:",mybatisUser);
        return jsonObject;
    }


    /**
     * 删除数据
     * @param userInfo 删除数据所需的参数对象
     * @return 返回结果
     */
    @CrossOrigin
    @RequestMapping(value = "/deleteMybatisUserInfo",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    JSONObject deleteMybatisUserInfo(@RequestBody UserInfo userInfo){
        MybatisUserImpl mybatisUserImpl = new MybatisUserImpl();
        MybatisUserImpl.Criteria criteria = mybatisUserImpl.createCriteria();
        criteria.andIdEqualTo(userInfo.getId());
        mybatisUserMapper.deleteByExample(mybatisUserImpl);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("删除数据成功,数据id为:",userInfo.getId());
        return jsonObject;
    }


}

2、在postman中访问接口(先启动项目)

2-1、增

url

127.0.0.1:8080/study/web/insertMybatisUserInfo

请求体

{"id":"5","userName":"温宁","userId":"鬼将军","address":"岐山不夜天"}

springboot的基本增删改查_第1张图片
springboot的基本增删改查_第2张图片
springboot的基本增删改查_第3张图片

2-2、删

url

127.0.0.1:8080/study/web/deleteMybatisUserInfo

请求体

{"id":"4"}

springboot的基本增删改查_第4张图片
springboot的基本增删改查_第5张图片

2-3、改

url

127.0.0.1:8080/study/web/updateMybatisUserInfo

请求体

{"id":"5","userName":"魏婴","userId":"夷陵老祖","address":"夷陵乱葬岗"}

springboot的基本增删改查_第6张图片
springboot的基本增删改查_第7张图片

2-4、查

url

127.0.0.1:8080/study/web/getMybatisUserInfo

springboot的基本增删改查_第8张图片
springboot的基本增删改查_第9张图片
END

你可能感兴趣的:(springboot集成,增删改查,spring,boot,java,intellij-idea)