springboot的restful风格的增删改查带页面小demo

springboot的restful风格的增删改查带页面小demo
面试的时候被要求写一个增删改查的demo,没有准备敲了好久。平时没有上传代码的习惯。后端的一下子就搞出来了,到了前端就卡住了。页面不想用JSP和thymeleaf这种模板引擎,想用ajax拼接html片段的生成页面。到了前端就有点搞不过来,还在继续摸索。最近算是把增删改查四个功能完成,写出来到时候。在网上一直找不到比较舒服的博客学习,只有后端没页面,有页面不是ajax这种,也么是ajax但是JS会失效的。写下这博客方便自己面试和后面需要学习springboot增删改查带页面的小demo的朋友。后续会把分页功能,图片上传下载和数据校验的功能加上慢慢完善。

构建springboot项目需要的依赖
构建项目的jar
系统架构图架构图

页面

实体类

@Data
public class User {
   
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public Integer getId() {
   
        return id;
    }

    public void setId(Integer id) {
   
        this.id = id;
    }

    public String getUsername() {
   
        return username;
    }

    public void setUsername(String username) {
   
        this.username = username;
    }

    public Date getBirthday() {
   
        return birthday;
    }

    public void setBirthday(Date birthday) {
   
        this.birthday = birthday;
    }

    public String getSex() {
   
        return sex;
    }

    public void setSex(String sex) {
   
        this.sex = sex;
    }

    public String getAddress() {
   
        return address;
    }

    public void setAddress(String address) {
   
        this.address = address;
    }
}

mapper层

@Mapper
public interface UserMapper {
   
    //查询所有User
    @Select("select * from user")
    public List<User> findAll();
    @Select("select * from user where id = #{id}")
    //查询一个User
    public User findUserById(Integer id);
    @Update("update user set username = '${username}',birthday = #{birthday},sex = #{sex},address = #{address} where id = #{id}")
    //修改User
    public int updateUser(User user);
    @Insert({
   "insert into user(username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})"})
    //添加一个User
    public int addUser(User user);
    //删除一个User
    @Delete("delete from user where id = #{id}")
    public int deleteUser(Integer id);
}

controller层

@RestController
public class UserController {
   
    @Autowired
    UserMapper userMapper;
    /**
     * 查询所有user
     * @return
     */
    @GetMapping("/user")
    public List<User> findAll(){
   
        return userMapper.findAll();
    }
    /**
     * 根据id查询一个user
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public User findUserById(@PathVariable("id") Integer id){
   
        return userMapper.findUserById(id);
    }
    /**
     * 修改user
     * @param id
     * @param username
     * @param birthday
     * @param sex
     * @param address
     * @return
     * @throws ParseException
     */
    @PutMapping("/user")
    public String update(@RequestParam("id") String id,
                         @RequestParam("username") String username,
                         @RequestParam("birthday") String birthday,
                         @RequestParam("sex") String sex,
                         @RequestParam("address") String address) throws ParseException {
   
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setUsername(username);
        //对日期进行格式转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(birthday);
        user.setBirthday(date);
        user.setSex(sex);
        user.setAddress(address);
        return userMapper.updateUser(user) == 1 ? "success" : "failed";
    }
    /**
     * 新增一个user
     * @param username
     * @param birthday
     * @param sex
     * @param address
     * @return
     */
    @PostMapping("/user")
    public String insertUser(@RequestParam("username") String username,
                             @RequestParam("birthday") String birthday,
                             @RequestParam("sex") String sex,
                             @RequestParam("address") String address) throws ParseException {
   
        User user = new User();
        user.setUsername(username);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值