当前位置: 首页 > 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", "","失败");}}

http://www.mrgr.cn/news/12739.html

相关文章:

  • AI学习记录 - 怎么理解 torch 的 nn.Conv2d
  • 项目中Redis常见的一些问题(缓存穿透,缓存雪崩,内存耗尽等)
  • 网安面试过程中的近源攻击
  • 华为OD机试 - 找单词 - 深度优先搜索DFS(Java 2024 E卷 100分)
  • 苹果M4芯片Mac全面曝光 或10月发布
  • 说说重载(Overloading)与重写(Overriding)的区别
  • APP、小程序对接聚合广告平台需要提供哪些资料?
  • java中用雪花算法生成64位的长整数
  • 微深节能 卸料小车远程智能控制系统 格雷母线定位系统
  • 每日刷力扣SQL(九)
  • Qt详解QParallelAnimationGroup并行动画组
  • 后端面试真题整理
  • Day24 第11站 出发 c++!
  • 如何构建社区康养管理系统?实现老年人服务管理全攻略【Java SpringBoot】
  • 在Excel中使用VLOOKUP函数时避免显示NA和0
  • Linux调整SSH登录访问方式
  • 13.JS学习篇-ES6 React 项目模板
  • 论文速览【LLM】 —— 【ORLM】Training Large Language Models for Optimization Modeling
  • Html5—表单
  • 第2章-07-客户端IP与UserAgent