利用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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


第三步:进行配置

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mall?serverTimezone=UTC
    username: root
    password: root

第四步:添加分页配置

package com.example.test.config;
import	java.util.Properties;

import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PageHelperConfig {

    @Bean
    public PageHelper pageHelper(){
        PageHelper page = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable","true");
        page.setProperties( properties );
        return page;

    }
}

第五步:添加实体类

package com.example.test.pojo;

import lombok.Data;

@Data
public class User {
    private Long id;
    private String username;
}

第六步:添加mapper接口

package com.example.test.mapper;


import com.example.test.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface UserMapper {
    @Select( "select * from ums_admin" )
    List<User> list();

    @Delete( "delete from ums_admin where id=#{id}" )
    void delete(Long id);

    @Update( "update ums_admin set username=#{username} where id = #{id}" )
    int update(User user);

    @Insert( "insert ums_admin(username) values(#{username})" )
    int save(User user);

    @Select(" select * from ums_admin where id=#{id}")
    User edit(Long id);

}

第七步:编写Controller层

package com.example.test.web;

import com.example.test.mapper.UserMapper;
import com.example.test.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("list")
    public String List(Model model,@RequestParam(value = "start",defaultValue = "0")int start,
                       @RequestParam(value = "size",defaultValue = " 3")int size){
        PageHelper.startPage( start,size," id desc" );
        List<User> user=userMapper.list();
        PageInfo<User> page=new PageInfo<> (user);
        model.addAttribute( "page",page );
        return "list";
    }

    @RequestMapping("save")
    public String save(User user){
        userMapper.save(user);
        return "redirect:list";
    }

    @RequestMapping("update")
    public String update(User user){
        userMapper.update(user);
        return "redirect:list";
    }

    @RequestMapping("edit")
    public String edit(Long id,Model model){
        User user=userMapper.edit(id);
        model.addAttribute( "user",user );
        return "edit";
    }

    @RequestMapping("delete")
    public String delete(User user){
        userMapper.edit(user.getId());
        return "redirect:list";
    }
}

第八步:写页面

list页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <table>
        <tr>
            <td>id</td>
            <td>username</td>
            <td>编写</td>
            <td>删除</td>
        </tr>
        <tr th:each="c:${page.list}">
            <td th:text="${c.id}"></td>
            <td th:text="${c.username}"></td>
            <td> <a th:href="@{/edit(id=${c.id})}">编写</a></td>
            <td> <a th:href="@{/delete(id=${c.id})}">删除</a></td>
        </tr>
    </table>

    <div>
        <a th:href="@{/list(start=0)}">首页</a>
        <a th:href="@{/list(start=${page.pageNum-1})}">上一页</a>
        <a th:href="@{/list(start=${page.pageNum+1})}">下一页</a>
        <a th:href="@{/list(start=${page.pages})}">尾页</a>
    </div>

    <form action="/save" method="post">
        username:<input name="username"/>
        <button type="submit">提交</button>
    </form>

</div>


</body>
</html>

edit页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/update" method="post">
        <input name="username" th:value="${user.username}"/>
        <input name="id" th:value="${user.id}" type="hidden"/>
        <button type="submit">提交</button>
    </form>

</body>
</html>

第九步 结果

在这里插入图片描述

发布了29 篇原创文章 · 获赞 8 · 访问量 915

猜你喜欢

转载自blog.csdn.net/qq_38650808/article/details/102467867