实现的功能:显示全部数据、添加数据、修改数据、删除数据、根据id搜索和分页,下面是项目演示截图:
选择Maven Project,填写包名、项目名称,finish
<?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.2.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.tedu.springboot</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- maven的版本问题 编辑器的兼容问题 -->
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- 阿里巴巴连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如果项目有报错,选中项目右键找到Maven选择Update Project…,把Force Update of Snapshots/Releases选中,点击OK,等他更新完就好了
application.yml
server:
port: 8888
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
thymeleaf:
cache: false #关闭缓存
mode: HTML5 #设置模板类型
encoding: utf-8 #设置编码
suffix: .html
pagehelper: #pagehelper分页插件配置
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSqll
UserBean.java
package com.kang.bean;
import java.io.Serializable;
//@AllArgsConstructor//全参数构造
public class UserBean implements Serializable{
private int id;//主键是不参与构造的
private String name;
private String pwd;
private String email;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "UserBean [id=" + id + ", name=" + name + ", pwd=" + pwd + ", email=" + email + ", age=" + age + "]";
}
}
UserMapper.java
package com.kang.mapper;
import java.util.List;
import com.kang.bean.UserBean;
public interface UserMapper {
public int addUser(UserBean userBean);
public int updateUser(UserBean userBean);
public int deleteUser(int id);
public List<UserBean> findAll();
public UserBean findOneByid(int id);
}
UserMapper.xml
<mapper namespace="com.kang.mapper.UserMapper">
<select id="findAll" resultType="com.kang.bean.UserBean">
select * from user
select>
<select id="findOneByid" resultType="com.kang.bean.UserBean" parameterType="int">
select * from user where id=#{id}
select>
<insert id="addUser" parameterType="com.kang.bean.UserBean" >
insert into user() value(#{id},#{name},#{pwd},#{email},#{age})
insert>
<update id="updateUser" parameterType="com.kang.bean.UserBean">
update user set name=#{name},pwd=#{pwd},email=#{email},age=#{age} where id=#{id}
update>
<delete id="deleteUser" parameterType="com.kang.bean.UserBean">
delete from user where id=#{id}
delete>
mapper>
UserService.java
package com.kang.service;
import java.util.List;
import com.kang.bean.UserBean;
public interface UserService {
// 查询全部
List<UserBean> findAll();
// 添加数据
int addUser(UserBean userBean);
// 删除数据
Integer deleteUser(int id);
// 修改需要先查询出来对象数据,显示然后进行数据提交,依据id唯一进行修改
int updateUser(UserBean userBean);
// 查询对象
UserBean fingOne(int id);
}
UserServiceImpl.java
package com.kang.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.kang.bean.UserBean;
import com.kang.mapper.UserMapper;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<UserBean> findAll() {
return userMapper.findAll();
}
@Override
public int addUser(UserBean userBean) {
return userMapper.addUser(userBean);
}
@Override
public Integer deleteUser(int id) {
return userMapper.deleteUser(id);
}
@Override
public int updateUser(UserBean userBean) {
return userMapper.updateUser(userBean);
}
@Override
public UserBean fingOne(int id) {
return userMapper.findOneByid(id);
}
}
UserController.java
package com.kang.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kang.bean.UserBean;
import com.kang.service.UserService;
@Controller // +@ResponseBody=@RestController
// @RestController//这个类下所有的都返回json格式
public class UserController {
// 注入
// @Autowired
@Resource
private UserService userService;
@RequestMapping("/user/login")
public ModelAndView login(ModelAndView mv, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
if (pageNum < 1 || pageNum == null) {
pageNum = 1;
}
PageHelper.startPage(pageNum, 5);
List<UserBean> list = userService.findAll();
PageInfo<UserBean> pageInfo = new PageInfo<UserBean>(list);
mv.addObject("pageInfo", pageInfo);
mv.setViewName("list");
return mv;
}
@PostMapping("/user/add")
// @ResponseBody
public String add(UserBean userBean, HttpServletResponse servletResponse) throws IOException {
int addUser = userService.addUser(userBean);
if (addUser == 1) {
servletResponse.sendRedirect("/user/login");
}
return "error";
}
@RequestMapping("/user/useradd")
public String add() {
return "add";
}
// 删除数据
@RequestMapping("/user/deleteUser")
public String deleteUser(@RequestParam Integer id, HttpServletResponse servletResponse) throws IOException {
int count = userService.deleteUser(id);
System.out.println("1111111");
if (count == 1) {
servletResponse.sendRedirect("/user/login");
}
return "error";
}
// 更新数据
@RequestMapping("/user/updateName")
public ModelAndView updateName(ModelAndView mv, int id) {
UserBean findOne = userService.fingOne(id);
mv.addObject("users", findOne);
mv.setViewName("update");
return mv;
}
@PostMapping("/user/userUpdate")
public String updateUser(UserBean userBean, HttpServletResponse servletResponse) throws IOException {
int userUpdate = userService.updateUser(userBean);
if (userUpdate == 1) {
servletResponse.sendRedirect("/user/login");
}
return "error";
}
// 根据id查找
@PostMapping("/user/updatePage")
public ModelAndView updatePage(int id, ModelAndView mv,
@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
UserBean users = userService.fingOne(id);
if (pageNum < 1 || pageNum == null) {
pageNum = 1;
}
PageHelper.startPage(pageNum, 5);
List<UserBean> list = new ArrayList<>();
list.add(users);
PageInfo<UserBean> pageInfo = new PageInfo<UserBean>(list);
mv.addObject("pageInfo", pageInfo);
mv.setViewName("list");
return mv;
}
}
在src/main/resources下创建文件夹template,以存放页面
list.html
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
<link
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet">
head>
<style>
p a {
background-color: white;
}
h2 {
text-align: center;
}
.search_form {
width: 602px;
height: 42px;
}
/*左边输入框设置样式*/
.input_text {
width: 200px;
height: 30px;
border: 1px solid green;
/*清除掉默认的padding*/
padding: 0px;
/*提示字首行缩进*/
text-indent: 10px;
/*去掉蓝色高亮框*/
outline: none;
/*用浮动解决内联元素错位及小间距的问题*/
float: left;
}
.input_sub {
width: 70px;
height: 30px;
background: green;
/*去掉submit按钮默认边框*/
border: 0px;
float: left;
color: white; /*搜索的字体颜色为白色*/
cursor: pointer; /*鼠标变为小手*/
}
style>
<body>
<h2>SpringBoot-CURDh2>
<form class="search_form" th:action="@{/user/updatePage}" method="post">
<input type="text" class="input_text" placeholder="请输入id搜索" name="id">
<input type="submit" value="搜索" class="input_sub">
form>
<table
class="table table-striped table-bordered table-hover text-center">
<thead>
<tr style="text-align: center">
<th>idth>
<th>nameth>
<th>pwdth>
<th>emailth>
<th>ageth>
<th>operationth>
tr>
thead>
<tr th:each="user:${pageInfo.list}">
<td th:text="${user.id}">td>
<td th:text="${user.name}">td>
<td th:text="${user.pwd}">td>
<td th:text="${user.email}">td>
<td th:text="${user.age}">td>
<td><a class="btn btn-primary"
th:href="@{/user/updateName(id=${user.id})}">更改a> <a
class="btn btn-danger"
th:href="@{'/user/deleteUser?id='+${user.id}}">删除a>td>
tr>
table>
<p>
当前 <span th:text="${pageInfo.pageNum}">span> 页,总 <span
th:text="${pageInfo.pages}">span> 页,共 <span
th:text="${pageInfo.total}">span> 条记录
<a th:href="@{/user/login(pageNum=1)}">首页a>
<a th:href="@{/user/login(pageNum=${pageInfo.pageNum}-1)}">上一页a>
<a th:href="@{/user/login(pageNum=${pageInfo.pageNum}+1)}">下一页a>
<a th:href="@{/user/login(pageNum=${pageInfo.pages})}">尾页a>
p>
<button class="btn btn-block">
<a href="/user/useradd">添加用户a>
button>
body>
html>
update.html
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改用户title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
head>
<body>
<div style="width:600px;height:100%;margin-left:270px;">
<form action="/user/userUpdate" method="post">
ID:<input class="form-control" name="id" type="text" th:value="${users.id}" readonly="readonly"><br>
用户名:<input class="form-control" type="text" th:value="${users.name}" name="name"><br>
密 码:<input class="form-control" type="text" th:value="${users.pwd}" name="pwd"><br>
email:<input class="form-control" type="text" th:value="${users.email}" name="email"><br>
年龄:<input class="form-control" type="text" th:value="${users.age}" name="age"><br>
<button class="btn btn-primary btn-lg btn-block" type="submit">提交button>
form>
div>
body>
html>
add.xml
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加用户title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
head>
<body>
<div style="width:600px;height:100%;margin-left:270px;">
<form action="/user/add" method="post">
用户名:<input class="form-control" type="text" th:value="${name}" name="name"><br>
密 码:<input class="form-control" type="text" th:value="${pwd}" name="pwd"><br>
email:<input class="form-control" type="text" th:value="${email}" name="email"><br>
年龄:<input class="form-control" type="text" th:value="${age}" name="age"><br>
<button class="btn btn-primary btn-lg btn-block">保存button>
form>
div>
body>
html>
SpringBootMain.java 启动类
package com.kang;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.kang.mapper")
public class SpringBootMain {
//启动类
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class, args);
}
}
浏览器中输入http://localhost:8888/user/login