Springboot+Mybatis+jpa+thymeleaf
项目结构
pom文件
配置相关依赖:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
Application配置文件
这里需要根据自己的电脑上的数据库信息进行配置:
spring.datasource.url=jdbc:mysql://localhost/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=用户名
spring.datasource.password=密码
#连接数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/changgou?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#jpa 相关配置
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
#thymelea模板配置
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
实体类
类名必须与数据库表名相同,类中字段与表中字段相对应
package com.li.pojo;
import javax.persistence.*;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
controller层实现类
package com.li.controller;
import com.li.pojo.User;
import com.li.service.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;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
// 查询所有
@GetMapping("/list")
public String findUsers(Model model){
List<User> users = userService.findUsers();
model.addAttribute("users", users);
return "index";
}
// 根据id查询
@GetMapping("/edit")
public String findUserById(Integer id, Model model){
User user = userService.findUserById(id);
model.addAttribute("user",user);
return "update";
}
@RequestMapping("/show")
public String show(){
return "add";
}
// 添加
@PostMapping(value = "/add")
public String saveUser(User user){
userService.addUser(user);
return "redirect:/user/list";
}
// 更新
@RequestMapping("/update")
public String updateUser(User user){
userService.updateUser(user);
return "redirect:/user/list";
}
// 根据id删除
@RequestMapping("/delete")
public String deleteUserById(Integer id){
userService.deleteUserById(id);
return "redirect:/user/list";
}
}
service层接口
package com.li.service;
import com.li.pojo.User;
import java.util.List;
public interface UserService {
List<User> findUsers();
User findUserById(Integer id);
void addUser(User user);
void updateUser(User user);
void deleteUserById(Integer id);
}
service层实现类
package com.li.service.impl;
import com.li.dao.UserDao;
import com.li.pojo.User;
import com.li.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findUsers() {
List<User> users = userDao.findAll();
return users;
}
@Override
public User findUserById(Integer id) {
Optional<User> optional = userDao.findById(id);
User user = optional.get();
return user;
}
@Override
public void addUser(User user) {
userDao.save(user);
}
@Override
public void updateUser(User user) {
userDao.save(user);
}
@Override
public void deleteUserById(Integer id) {
userDao.deleteById(id);
}
}
dao层接口
package com.li.dao;
import com.li.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserDao extends JpaRepository<User,Integer> {
}
启动类
package com.li;
import org.springframework.boot.SpringApplication;
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
index.html首页
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a th:href="@{/user/show}">添加</a>
<div>
<table border="1">
<tr>
<th>ID</th>
<th>USERNAME</th>
<th>PASSWORD</th>
<th></th>
</tr>
<!-- 通过th命令使用一些操作 -->
<!-- 通过${} 使用变量 -->
<tr th:each="user:${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td>
<a th:href="@{/user/edit(id=${user.id})}">修改</a>
<a th:href="@{/user/delete(id=${user.id})}">删除</a>
</td>
</tr>
</table>
</div>
</body>
</html>
add.html添加页面
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/add" method="post" >
<input type="hidden" name="id">
username:<input type="text" name="username">
password:<input type="password" name="password">
<a href="/user/list">返回</a>
<input type="submit" value="提交">
</form>
</body>
</html>
update.html更新页面
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/update" th:object="${user}" method="post">
<input type="hidden" name="id" th:id="*{id}" th:value="*{id}">
用户名:<input type="text" id="username" name="username" th:value="*{username}">
密 码:<input type="password" id="password" name="password" th:value="*{password}">
<a href="/user/list">返回</a>
<input type="submit" value="提交">
</form>
</body>
</html>
创建user表
CREATE TABLE `user` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
添加表数据
INSERT INTO USER (username, PASSWORD) VALUES ('123','123');
首页展示
添加
添加用户名为111,密码为123456
添加后返回到首页:
修改
修改id为1的密码为123456:
修改后返回到首页:
删除
删除id为5的用户:
总结
通过springboot构建maven项目,结合Mybatis和jpa快速实现简单的增删改查,使用thymeleaf模板技术实现前端页面的显示。