springboot的基本增删改查
springboot增删改查
- 1、根据表中的字段创建实体类UserInfo.java
- 2、编写MybatisController.java
- 3、在postman中访问接口(先启动项目)
- 3-1、增
- 3-2、删
- 3-3、改
- 3-4、查
参考:
springboot搭建
springboot集成mysql
springboot集成mybatis【使用generatorConfig.xml配置自动生成代码】
在集成mysql和mybatis后,对数据进行简单的增删改查操作
参考springboot集成mybatis中的代码继续下面的步骤(springboot集成mybatis【使用generatorConfig.xml配置自动生成代码】)
1、根据表中的字段创建实体类UserInfo.java
package com.study.mybatis.entity;import lombok.Data;/**
* @Author: JING
* @Created: 2022/2/9 15:56
* @Description:
* @Version: 1.0.0
* @Since JDK 1.8
*/
@Data
public class UserInfo {private Integer id;private String userName;private String userId;private String address;
}
2、编写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 {@AutowiredMybatisUserMapper 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;}}
3、在postman中访问接口(先启动项目)
3-1、增
url
127.0.0.1:8080/study/web/insertMybatisUserInfo
请求体
{"id":"5","userName":"温宁","userId":"鬼将军","address":"岐山不夜天"}
3-2、删
url
127.0.0.1:8080/study/web/deleteMybatisUserInfo
请求体
{"id":"4"}
3-3、改
url
127.0.0.1:8080/study/web/updateMybatisUserInfo
请求体
{"id":"5","userName":"魏婴","userId":"夷陵老祖","address":"夷陵乱葬岗"}
3-4、查
url
127.0.0.1:8080/study/web/getMybatisUserInfo
END
springboot的基本增删改查相关推荐
- SpringBoot+MyBatisPlus+Vue增删改查
SpringBoot+MyBatisPlus+Vue增删改查 建议在写之前会熟练使用vue脚手架,以及掌握vue相关知识 本文章会教你构建一个springboot+mybatisplus+vue 的增 ...
- SpringBoot集成thymeleaf增删改查示例
有小伙伴找我要个 thymeleaf 的 demo,说自己集成的总是报错,所以就有了这篇- 关于什么是 thymeleaf 我就不赘述了,相信搜到这篇的大部分是奔着如何集成来的. 本文源码先附上:ht ...
- Springboot+Mybatis-plus实现增删改查功能超详细
1.后端实现 用到的依赖:Springboot,Mybatis-plus,lombok,mysql驱动 1.1.利用IDEA2021创建一个Springboot项目 注意:Name(项目名称) loc ...
- SpringBoot项目实现增删改查
接上一章:Java 连接MySql_原你是阳光(#`O′)的博客-CSDN博客,实现单表增删改查 引入依赖 <dependencies><!-- springmvc的场景启动器 -- ...
- layui+SpringBoot实现表格增删改查
前言 本文将演示如何使用Springboot(后端框架)和layui(前端框架)将数据库中的数据渲染到前端页面,以及对前端页面的数据实现增删改. 效果图如下: 完整项目已上传至GitHub,项目地址: ...
- springboot+JPARepository实现增删改查
结构.png 首先我们创建一个数据库映射的实体类,上一节讲了的 package com.alun;import javax.persistence.Entity; import javax.persi ...
- SpringBoot的ResultFul增删改查
文章目录 项目实验 1.resultfulCRUD-员工列表 thymeleaf公共页面元素抽取 2.CRUD-员工添加 3.CRUD-员工修改 4.CRUD-员工删除 项目实验 1.resultfu ...
- springboot整合mybatis增删改查(三):mybatis逆向工程
上一篇已经把项目基本框架完善,接下来就是利用Mybatis Generator逆向工程进行mybatis的整合. 我们在创建项目开始的时候已经勾选web,mybatis,sql等,但是这些依赖还是不够 ...
- springboot整合mongodb增删改查入门
1.配置与依赖 2.实体与repository层 3.测试 适合新手. 要求:安装好mongodb 代码和运行程序如下: 目录: 1.配置与依赖 <dependency><group ...
最新文章
- 12个不可不知的Sublime Text应用技巧和诀窍
- 验证码识别--type7
- 电容充放电原理图_干货 | 入门开关电源,这些原理图一定要读懂
- python安装了怎么使用_Python PyCharm 安装与简单使用
- [数论]拓展中国剩余定理
- 安卓Day16-bug
- View controller-based status bar
- 拼多多上线“女装11.11爆款必买团”活动 一款打底裤一天卖出4.2万单
- java 取上下文路径_取得上下文路径的方式(getContextPath)
- 如何正确在NSMutableDictionary中加入一个变量int
- java类中代码执行顺序
- 小甲鱼解密系列调试篇——OD使用教程笔记(持续更新中)
- 数据字典的一个简单案例
- 虚拟机打开了服务器维护,Vmware WorkStation 打开远程服务器上的虚拟机
- 2016年总结与2017展望
- 用html做龙卷风特效,抖音HTML龙卷风特效代码是啥?
- freenom 申请免费域名
- 使用web3.py获取zkSync中的数据
- 计算机网络原理ip计算,计算机网络原理IP地址计算题
- 印度软件和中国软件工程师_印度的软件公司类型