当前位置:   article > 正文

SpringBoot项目简单的后台增删查改_springboot实现前后端的增删改查

springboot实现前后端的增删改查

配置pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xyc</groupId>
    <artifactId>gdd_exam</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gdd_exam</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.xyc.gdd_exam.GddExamApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 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
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

配置properties文件

# 应用服务 WEB 访问端口
server.port=8080
#配置mysql数据库
spring.datasource.url=jdbc:mysql://localhost:3306/gdd_exam
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#mybatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.xyc.gdd_exam.po
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

代码实现

po包

User类

package com.xyc.gdd_exam.po;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Date;
@Data//lombok注解,不需要写get,set方法
@AllArgsConstructor//所有的有参构造
@NoArgsConstructor//无参构造
public class User {
    private Integer id;//id
    private String username;//用户名
    private String password;//密码
    private Integer status;//用户状态
    private Integer phone;//电话
    private String address;//地址
    private String pic;//头像
    private Integer purview;//权限
    private Date createtime;//日期
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

vo包

ResponseVo

package com.xyc.gdd_exam.vo;
import com.sun.istack.internal.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseVo implements Serializable {
    private Integer code;
    private String msg;
    private Object data;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

mapper包

UserMapper类

package com.xyc.gdd_exam.mapper;
import com.xyc.gdd_exam.po.User;
import java.util.List;
public interface UserMapper {
    //查询所有用户
    List<User> selectAll();
    //根据id查询个人信息
    User select(int id);
    //修改用户信息
    boolean update(User user);
    //添加用户
    boolean add(User user);
    //根据id删除用户信息
    boolean delete(int id);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

mappers包

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.xyc.gdd_exam.mapper.UserMapper">
    <resultMap id="baseMap" type="com.xyc.gdd_exam.po.User">
        <id column="user_id" property="id"></id>
        <result column="user_name" property="username"></result>
        <result column="user_pass" property="password"></result>
        <result column="user_status" property="status"></result>
        <result column="user_phone" property="phone"></result>
        <result column="user_addr" property="address"></result>
        <result column="user_pic" property="pic"></result>
        <result column="user_purview" property="purview"></result>
        <result column="createtime" property="createtime"></result>
    </resultMap>
    <select id="selectAll" resultMap="baseMap">
        select * from gdd_user
    </select>
    <select id="select" resultMap="baseMap" parameterType="java.lang.Integer">
        select * from gdd_user where user_id=#{id}
    </select>
    <insert id="add" parameterType="com.xyc.gdd_exam.po.User">
        insert into
            gdd_user(user_name,user_pass,user_status,user_phone,user_addr,user_pic,user_purview,createtime)
            values (#{username},#{password},#{status},#{phone},#{address},#{pic},#{purview},#{createtime})
    </insert>
    <update id="update" parameterType="com.xyc.gdd_exam.po.User">
        update gdd_user set
                            user_name=#{username},
                            user_pass=#{password},
                            user_status=#{status},
                            user_phone=#{phone},
                            user_addr=#{address},
                            user_pic=#{pic}
            where user_id=#{id}
    </update>
    <delete id="delete" parameterType="java.lang.Integer">
        delete from gdd_user where user_id=#{id}
    </delete>
</mapper>
  • 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

service包

UserServiceImpl

package com.xyc.gdd_exam.service.impl;
import com.xyc.gdd_exam.mapper.UserMapper;
import com.xyc.gdd_exam.po.User;
import com.xyc.gdd_exam.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<User> selectAll(){
        List<User> users = userMapper.selectAll();
        System.out.println("service users = " + users);
        if (users!=null){
            return users;
        }else{
            return null;
        }
    }
    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Override
    public User select(int id){
        System.out.println("service id = " + id);
        if (id>0){
            User select = userMapper.select(id);
            System.out.println("service select = " + select);
            if (select!=null){
                return select;
            }else{
                return null;
            }
        }else {
            return null;
        }
    }
    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public boolean add(User user){
        System.out.println("service user = " + user);
        if (user!=null){
            boolean add = userMapper.add(user);
            return add;
        }else {
            return false;
        }
    }
    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public boolean update(User user){
        System.out.println("service user = " + user);
        if (user!=null){
            boolean update = userMapper.update(user);
            return update;
        }else {
            return false;
        }
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @Override
    public boolean delete(int id){
        System.out.println("service id = " + id);
        if (id>0){
            boolean delete = userMapper.delete(id);
            return delete;
        }else {
            return false;
        }
    }
}
  • 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
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

controller包

UserController

package com.xyc.gdd_exam.controller;
import com.sun.istack.internal.NotNull;
import com.xyc.gdd_exam.po.User;
import com.xyc.gdd_exam.service.UserService;
import com.xyc.gdd_exam.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/selectAll")
    public ResponseVo selectAll(){
        List<User> users = userService.selectAll();
        if (users!=null){
            return new ResponseVo(200,"查询成功",users);
        }
        return new ResponseVo(-200,"查询失败",null);
    }
    @RequestMapping("/select")
    public ResponseVo select(@NotNull Integer id){
        User user = userService.select(id);
        if (user!=null){
            return new ResponseVo(200,"查询成功",user);
        }else {
            return new ResponseVo(-200,"查询失败",null);
        }
    }
    @PostMapping("/add")
    public ResponseVo add(@RequestBody User user){
        boolean add = userService.add(user);
        if (add==true){
            return new ResponseVo(200,"添加成功",add);
        }else{
            return new ResponseVo(-200,"添加失败",null);
        }
    }
    @RequestMapping("/update")
    public ResponseVo update(@RequestBody User user){
        boolean update = userService.update(user);
        if (update==true){
            return new ResponseVo(200,"修改成功",update);
        }else{
            return new ResponseVo(-200,"修改失败",null);
        }
    }
    @RequestMapping("/delete")
    public ResponseVo delete(@NotNull Integer id){
        boolean delete = userService.delete(id);
        if (delete==true){
            return new ResponseVo(200,"删除成功",delete);
        }else{
            return new ResponseVo(-200,"删除失败",null);
        }
    }
}
  • 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

启动类

package com.xyc.gdd_exam;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = {"com.xyc.gdd_exam.mapper"})
public class GddExamApplication {

    public static void main(String[] args) {
        SpringApplication.run(GddExamApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/576711
推荐阅读
相关标签