码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringBoot--列表添加新增功能


    1、IDEA进行相关的配置,并且创建SpringBoot项目,目录如下:
    在这里插入图片描述
    2、domain中创建实体类,用于和数据库中的数据表关联起来,User。

    package com.example.demo.domain;
    
    public class User {
        // 对应数据表user中的各字段
        private String uuid;
        private String username;
        private String gender;
        private int age;
        private String phone;
        private String address;
        private String email;
        private float salary;
    
        // 鼠标右键选择generate,接着选择setter and getter,选中所有字段,然后点击Ok,便会自动为我们自动生成get和set方法。
        public String getUuid() {
            return uuid;
        }
        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
    
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
    
        public float getSalary() {
            return salary;
        }
        public void setSalary(float salary) {
            this.salary = salary;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    3、mapper中创建接口(interface),如:UserMapper。

    package com.example.demo.mapper;
    
    import com.example.demo.domain.User;
    import org.apache.ibatis.annotations.*;
    import java.util.List;
    
    public interface UserMapper {
        // 与数据库相关的操作,通过注解来实现
        @Insert({"insert into user(uuid,username,gender,age,phone,address,email,salary) values(#{uuid},#{username},#{gender},#{age},#{phone},#{address},#{email},#{salary})"})
        @Options(useGeneratedKeys = true,keyProperty = "uuid",keyColumn = "uuid")  // useGeneratedKeys=true表示使用数据库自动增长的主键;keyColumn用于指定数据库table中的主键;keyProperty用于指定传入对象的成员变量。
        int insert(User user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4、service中创建服务(对应着mapper中的接口),如:UserService。

    package com.example.demo.service;
    
    import com.example.demo.domain.User;
    import com.github.pagehelper.PageInfo;
    import java.util.List;
    
    public interface UserService {
        // 增
        int save(User user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5、service-impl中创建接口服务的实现(对应着service中的接口),如:UserServiceImpl。

    package com.example.demo.service.impl;
    
    import com.example.demo.domain.User;
    import com.example.demo.mapper.UserMapper;
    import com.example.demo.service.UserService;
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @Service("UserService")
    public class UserServiceImpl implements UserService {
        @Resource  // 接口只能有一个实现类(@Resource是JDK提供的,通过name名字查找;而Autowired是Spring提供的,通过type类型查找。)
        private UserMapper userMapper;
    
        // 鼠标放在public class UserServiceImpl implements UserService的红线处,点击implement methods,全部选中,则创建好了所有的方法
        // 增加
        @Override
        public int save(User user) {
            return userMapper.insert(user);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    6、controller中创建控制器,如:UserController。

    package com.example.demo.controller;
    
    import com.alibaba.fastjson.JSONObject;
    import com.example.demo.domain.User;
    import com.example.demo.service.UserService;
    import com.github.pagehelper.PageInfo;
    import netscape.javascript.JSObject;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.*;
    import javax.annotation.Resource;
    import java.util.*;
    
    @Controller  // ①返回JSON或XML数据;②页面模板
    @RequestMapping("/user")  // 这个类的路径,定义为/user
    public class UserController {
        @Resource(name = "UserService")
        private UserService csi;
    
        // 添加功能
        @RequestMapping("/save")  // 可用于Get或Post请求
        @ResponseBody  // 将java对象转为json格式的数据
        public void save(User user){
            String uuid = UUID.randomUUID().toString().replace("","");  // UUID.randomUUID().toString()是javaJDK提供的一个自动生成主键的方法。
            user.setUuid(uuid);
            csi.save(user);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    相关链接:
    IntelliJ IDEA中构建Spring Boot的项目
    Spring Boot中的配置文件(application.properties、application.yml与pom.xml)
    ORM、JPA、Spring Data JPA和常用的五种访问数据库方式

    SpringBoot–页面显示列表功能
    SpringBoot–列表添加详情功能
    SpringBoot–列表添加新增功能
    SpringBoot–列表更新功能
    SpringBoot–列表删除功能

    SpringBoot的控制层注解(@Controller与@RestController)
    SpringBoot的业务层注解(@Resource、@Autowired与@Qualifier)
    SpringBoot的映射(@RequestMapping、@GetMapping与@PostMapping)
    SpringBoot的函数参数(JavaBean对象、@RequestParam与@RequestBody)
    SpringBoot的返回数据(@ResponseBody、返回templates下的网页 与 返回公共网页)

    利用postman完成JSON串的发送功能(springboot)
    利用postman完成数据的分页查询功能(springboot)
    利用postman完成向数据库中添加数据的功能(springboot)

    Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
    Preparing: insert into user(uuid,username,gender,age,phone,address) values(?,?,?,?,?,?,?)
    Could not autowire. No beans of ‘UserMapper‘ type found.
    Error starting ApplicationContext. To display the conditions report re-run your application with
    Loading class com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is com.mysql.cj.jdb
    Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found
    Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    BootStrap-SpringBoot的使用过程中前端table表格无法加载出数据。

  • 相关阅读:
    C#实现二叉树的最大深度
    Flutter 2.8 正式发布
    思科网络中如何配置扩展ACL协议
    Java语言高级-02继承与多态-第三节接口
    Linux 多线程多进程
    LeetCode 46 全排列 - Java 实现
    运动耳机品牌排行榜前十名有哪些,2022年六款运动耳机值得入手
    简单实现spring的set依赖注入
    CleanClip for Mac 剪切板 粘贴工具 历史记录 安装(保姆级教程,新手小白轻松上手)
    【PostgreSQL】用pgAdmin轻松管理PostgreSQL
  • 原文地址:https://blog.csdn.net/blbyu/article/details/126749722
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号