SpringBoot 增删改查
最近刚开始学 SpringBoot,做了一个小项目来练手。刚好做一个记录,为没有接触过 SpringBoot 的同学提供一个参考。SpringBoot 小项目一、 创建项目使用 IDEA,新建 springboot 项目,可以使用 IDEA 生成,也可以在官网生成。JDK:1.8Java:8Spring 官网 https://start.spring.io/添加项目依赖Spring WebMyba
最近刚开始学 SpringBoot,做了一个小项目来练手。刚好做一个记录,为没有接触过 SpringBoot 的同学提供一个参考。超详细!!!有目录结构!!!
目录
一、 创建项目
使用 IDEA,新建 springboot 项目,可以使用 IDEA 生成,也可以在官网生成。
-
JDK:1.8
-
Java:8
Spring 官网 https://start.spring.io/
添加项目依赖
-
Spring Web
-
Mybatis Framework
-
MySQL Driver
-
Thymeleaf
二、 创建数据库
use `mall_smart`;
CREATE TABLE IF NOT EXISTS `wdl_user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(125) DEFAULT NULL,
`password` varchar(256) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这里使用的是 MySQL Workbench
三、 项目结构
main
java.com.example.demo
bean
User.java
contoller
UserController.java
mapper
UserMapper.java
server
UserService.java
Application.java
resources
mapping
UserMapper.xml
static
css 包
img 包
js 包
templates
insertPage.html
updatePage.html
userList.html
application.yml
四、 代码实现
4.1后端代码
1. User 类
在 java.com.example.demo.bean 包下新建 User 类
package com.example.demo.bean;
public class User {
private int id;
private String username;
private String password;
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
control + enter
快速添加构造函数和 get() set() 方法
2. UserController 类
在 java.com.example.demo.controller 包下新建 UserController 类
package com.example.demo.controller;
import com.example.demo.bean.User;
import com.example.demo.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/insertPage")
public String index(){
return "insertPage";
}
@RequestMapping("select/{id}")
@ResponseBody
public String save(@PathVariable int id){
return userService.select(id).toString();
}
@RequestMapping("/userList")
public String userList(Model model){
List<User> users = userService.userList();
model.addAttribute("users",users);
return "userList";
}
@RequestMapping("/insert")
public String save(User user){
userService.save(user);
System.out.println("新增了用户:" + user);
return "redirect:/userList";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable Integer id){
userService.delete(id);
return "redirect:/userList";
}
@GetMapping("/updatePage/{id}")
public String updatePage(Model model,@PathVariable int id){
User user = userService.findUserById(id);
model.addAttribute("user",user);
return "updatePage";
}
@PostMapping("/update")
public String updateUser(User user){
userService.update(user);
System.out.println("修改的用户为 : " + user.getUsername());
return "redirect:/userList";
}
}
3. UserMapper 接口
在 java.com.example.demo.mapper 包下新建 UserMapper 接口
package com.example.demo.mapper;
import com.example.demo.bean.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
User select(int id);
List<User> userList();
void save(User user);
int delete(Integer id);
User getById(int id);
int update(User user);
}
4. UserService 类
在 java.com.example.demo.server 包下新建 UserService 类
package com.example.demo.server;
import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User select(int id) {
return userMapper.select(id);
}
public List<User> userList() {
return userMapper.userList();
}
public void save(User user){
userMapper.save(user);
}
public int delete(Integer id){
return userMapper.delete(id);
}
public User findUserById(int id){
return userMapper.getById(id);
}
public int update(User user){
return userMapper.update(user);
}
}
5. UserMapper.xml
写完接口后在 resource.mapping 包下新建映射文件 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.example.demo.mapper.UserMapper">
<!--查询所有用户-->
<select id="select" resultType="com.example.demo.bean.User">
select * from db.user_t where id = #{id}
</select>
<select id="userList" resultType="com.example.demo.bean.User">
select * from db.user_t
</select>
<!--添加用户-->
<insert id="save" parameterType="string">
INSERT INTO db.user_t VALUES(null,#{username},#{password})
</insert>
<!--删除用户-->
<delete id="delete" parameterType="int">
delete from db.user_t where id = #{id}
</delete>
<!--根据id查询用户-->
<select id="getById" resultType="com.example.demo.bean.User">
select * from db.user_t where id = #{id}
</select>
<!--更改用户信息-->
<update id="update" parameterType="user">
update db.user_t set username=#{username},password=#{password} where id=#{id}
</update>
</mapper>
6. application.yml
在 resources 包下新建 application.yml,一定要注意yml的编写格式。url 写自己数据库的 IP 和端口,username 和 password 记得改成自己的。
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://127.0.0.0/db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 12345
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
content-type: text/html
cache: false
web:
resources:
static-locations: classpath:static/
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.example.demo.bean
logging:
level:
com:
example:
demo:
mapper : debug
4.2 前端代码
在 resources.templates 下创建 html 文件
1. insertPage.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
<link href="/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div style="width:800px;height:100%;margin-left:270px;">
<form action="/insert" method="post">
用户名:<input class="form-control" type="text" th:value="${username}" name="username"><br>
密码:<input class="form-control" type="text" th:value="${password}" name="password"><br>
<button class="btn btn-primary btn-lg btn-block">保存</button>
</form>
</div>
</body>
</html>
2. updatePage.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改用户</title>
<link href="/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div style="width:800px;height:100%;margin-left:270px;">
<form action="/update" method="post">
ID:<input class="form-control" name="id" type="text" th:value="${user.id}" readonly="readonly"><br>
用户名:<input class="form-control" type="text" th:value="${user.username}" name="username"><br>
密 码:<input class="form-control" type="text" th:value="${user.password}" name="password"><br>
<button class="btn btn-primary btn-lg btn-block" type="submit">提交</button>
</form>
</div>
</body>
</html>
3. userList.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
<link href="/css/bootstrap.css" rel="stylesheet">
</head>
<style>
a {
color:#fff;
}
th {
text-align: center;
}
</style>
<body>
<table class="table table-striped table-bordered table-hover text-center">
<thead>
<tr style="text-align:center">
<th>Id</th>
<th>Username</th>
<th>Password</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="user:${users}">
<!--/*@thymesVar id="id" type="com.example.demo.bean.User"*/-->
<td th:text="${user.id}"></td>
<!--/*@thymesVar id="username" type="com.example.demo.bean.User"*/-->
<td th:text="${user.username}"></td>
<!--/*@thymesVar id="password" type="com.example.demo.bean.User"*/-->
<td th:text="${user.password}"></td>
<td>
<a class="btn btn-primary" th:href="@{'/updatePage/'+${user.id}}">更改</a>
<a class="btn btn-danger" th:href="@{'/delete/'+${user.id}}">删除</a>
</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary form-control" style="height:50px"><a th:href="@{'/insertPage'}">添加用户</a></button>
</body>
</html>
4. static 包
static 包中的 css 为 Bootstrap v3.3.7;js 为 jQuery v2.1.4。可以到原文的最下面的 gitee 下载或者自己下载。
五、效果
http://localhost:8080/userList
http://localhost:8080/insertPage
http://localhost:8080/updatePage/1
更多推荐
所有评论(0)