当前位置: 首页 > news >正文

spingboot实现常规增删改查

启动类

@SpringBootApplication
@MapperScan(basePackages = "com.example.jzdoffice.demos.mapper")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication .class, args);}
}

UserMapper

package com.example.jzdoffice.demos.mapper;import com.example.jzdoffice.demos.domain.User;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface UserMapper {// 插入数据int insertSelective(User user);// 删除数据int deleteById(@Param("id") Long id);// 修改数据 批量更新加foreachint updateById(@Param("updated") User updated, @Param("id") Long id);// 查询所有数据List<User> selectByAll(User user);// 查询一条数据User SelectById(@Param("id") Long id);// 批量插入数据int insertList(@Param("list") List<User> list);//通过list集合实现批量删除int deleteByIds(@Param("ids") List<Integer> ids);}

userMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.jzdoffice.demos.mapper.UserMapper"><sql id="Base_Column_List">id,`name`,`password`,create_time,update_time</sql><resultMap id="BaseResultMap" type="com.example.jzdoffice.demos.domain.User"><result column="id" property="id"/><result column="name" property="name"/><result column="password" property="password"/><result column="create_time" property="createTime"/><result column="update_time" property="updateTime"/></resultMap><!--  插入数据 关键字冲突,记得在字段加`` --><insert id="insertSelective">INSERT INTO tb_user<trim prefix="(" suffix=")" suffixOverrides=","><if test="id != null">`id`,</if><if test="name != null">`name`,</if><if test="password != null">`password`,</if><if test="createTime != null">`create_time`,</if><if test="updateTime != null">`update_time`</if></trim>VALUES<trim prefix="(" suffix=")" suffixOverrides=","><if test="id != null">#{id},</if><if test="name != null">#{name},</if><if test="password != null">#{password},</if><if test="createTime != null">#{createTime},</if><if test="updateTime != null">#{updateTime}</if></trim></insert><!-- 删除数据 --><delete id="deleteById">deletefrom tb_userwhere id = #{id}</delete><!-- 修改数据 --><update id="updateById">update tb_user<set><if test="updated.id != null">id = #{updated.id},</if><if test="updated.name != null">name = #{updated.name},</if><if test="updated.password != null">password = #{updated.password},</if><if test="updated.createTime != null">create_time = #{updated.createTime},</if><if test="updated.updateTime != null">update_time = #{updated.updateTime},</if></set>where id = #{id}</update><!-- 查询所有数据-更据传递参数 --><select id="selectByAll" resultMap="BaseResultMap">select<include refid="Base_Column_List"/>from tb_user<where><if test="id != null">and id = #{id}</if><if test="name != null">and `name` = #{name}</if><if test="password != null">and `password` = #{password}</if><if test="createTime != null">and create_time = #{createTime}</if><if test="updateTime != null">and update_time = #{updateTime}</if></where></select><!-- 查询一条数据 --><select id="SelectById" resultMap="BaseResultMap">select<include refid="Base_Column_List"/>from tb_userwhere id = #{id}</select><!-- 批量插入数据 --><insert id="insertList">INSERT INTO tb_user(id,name,password,create_time,update_time)VALUES<foreach collection="list" item="element" index="index" separator=",">(#{element.id},#{element.name},#{element.password},#{element.createTime},#{element.updateTime})</foreach></insert><!--  批量删除  --><delete id="deleteByIds">deletefrom tb_user where id in<foreach collection="ids" open="(" close=")" separator="," item="id">#{id}</foreach></delete>
</mapper>

Service

package com.example.jzdoffice.demos.service;import java.util.Date;import com.example.jzdoffice.demos.domain.User;
import com.example.jzdoffice.demos.mapper.UserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public int insert(User user){return userMapper.insertSelective(user);}public int delete(Long id){return userMapper.deleteById(id);}public int update(User user){return userMapper.updateById(user,user.getId());}public List<User> selectAll(User user){//开启分页查询,当执行查询时,插件进行相关的sql拦截进行分页操作,返回一个page对象Page<User> page = PageHelper.startPage(1, 2);userMapper.selectByAll(user);System.out.println(page);// 只需要上面这两部就有我们需要的数据了 返回数据需要在封装return userMapper.selectByAll(user);}public User getUser(Long id){return userMapper.SelectById(id);}
}

UserController

package com.example.jzdoffice.demos.controller;import com.example.jzdoffice.demos.domain.User;
import com.example.jzdoffice.demos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;//插入用户信息@RequestMapping(value = "/insert", method = RequestMethod.POST)public String insert(@RequestBody User user) {int insert = userService.insert(user);String resp = insert > 0 ? Result.ok(insert) : Result.no();return resp;}//通过id删除用户信息@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)public String delete(@PathVariable Long id) {int result = userService.delete(id);if (result >= 1) {return "删除成功!";} else {return "删除失败!";}}//更改用户信息@RequestMapping(value = "/update", method = RequestMethod.POST)public String update(@RequestBody User user) {int result = userService.update(user);String resp = result > 0 ? Result.ok(result) : Result.no();return resp;}//查询所有用户的信息@RequestMapping(value = "/selectAll")@ResponseBody   //理解为:单独作为响应体,这里不调用实体类的toString方法public String listUser() {List<User> result = userService.selectAll(null);return Result.ok(result) ;}//通过id得到用户信息@RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)public String getUser(@PathVariable Long id) {User result = userService.getUser(id);return Result.ok(result) ;}}

Result-响应格式

package com.example.jzdoffice.demos.controller;import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;public class Result {public static String response(String code, Object data, String msg) {HashMap<String, Object> map = new HashMap<>();map.put("code", code);map.put("msg", msg);map.put("data", data);return JSONObject.toJSONString(map);}public static String ok(Object data) {return response("200", data,"成功");}public static String ok(Object data, String msg) {return response("200", data, msg == null ? "成功" : msg);}public static String no() {return response("400", "","失败");}}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • erlang学习:gen_server书上案例22.6练习题4
  • jmeter通过参数文件、循环组件实现多账号登陆
  • npm install 报错解决记录
  • Golang 使用redis stream实现一个实时推送功能
  • Groupings sets详解
  • 东方银行--用 MinIO 和 Dremio 替代 Hadoop
  • React18快速入门教程
  • C HTML格式解析与生成
  • 浅谈Kafka(二)
  • EmguCV学习笔记 VB.Net 第5章 图像变换
  • 【机器学习】 1. 总览介绍
  • 开源大屏设计工具DataRoom
  • Elasticsearch:使用 ELSER 进行语义搜索 - sparse_vector
  • 在pytorch中TensorBoard的使用
  • OpenCV c++ 实现图像马赛克效果
  • Android Volley源码解析
  • Cumulo 的 ClojureScript 模块已经成型
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • express.js的介绍及使用
  • Fastjson的基本使用方法大全
  • nfs客户端进程变D,延伸linux的lock
  • React-flux杂记
  • react-native 安卓真机环境搭建
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 机器学习学习笔记一
  • 基于axios的vue插件,让http请求更简单
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 前端存储 - localStorage
  • 前端相关框架总和
  • 树莓派 - 使用须知
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 我的zsh配置, 2019最新方案
  • 学习HTTP相关知识笔记
  • 在Mac OS X上安装 Ruby运行环境
  • 字符串匹配基础上
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • ​马来语翻译中文去哪比较好?
  • !!java web学习笔记(一到五)
  • # 职场生活之道:善于团结
  • #laravel部署安装报错loadFactoriesFrom是undefined method #
  • #每日一题合集#牛客JZ23-JZ33
  • (10)ATF MMU转换表
  • (42)STM32——LCD显示屏实验笔记
  • (52)只出现一次的数字III
  • (Windows环境)FFMPEG编译,包含编译x264以及x265
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • ..回顾17,展望18
  • .NET Core 和 .NET Framework 中的 MEF2
  • .Net Core缓存组件(MemoryCache)源码解析
  • .net framework4与其client profile版本的区别
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?