create database springbootjpa
创建项目时添加web、Thymeleaf模板
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
# 应用名称
spring.application.name=jpa
# 应用服务 WEB 访问端口
server.port=8080
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=true
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html
server.servlet.context-path=/
##数据源信息配置
#数据库地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?useSSL=false&characterEncoding=utf-8
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=123456
#数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#JPA持久化配置
#指定数据库类型
spring.jpa.database=MYSQL
#指定是否在日志中显示SQL语句
spring.jpa.show-sql=true
#指定自动创建、更新数据库表等配置,update表示如果数据库中存在持久化类对应的表就不创建,不存在就创建对应的表
spring.jpa.hibernate.ddl-auto=update
#让控制器输出的JSON字符串格式更美观
spring.jackson.serialization.indent-output=true
package com.yzh.jpa.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user_table")
/**
* 如果没写表名,默认为类名的词组
* (如MyUser类对应的表名为my_user)
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class MyUser implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;//主键
/**使用@Column注解,可以配置列相关属性(列名,长度等),
* 可以省略,默认为属性名小写
*/
private String uname;
private String usex;
private int age;
}
package com.yzh.jpa.repository;
import java.util.List;
import com.yzh.jpa.entity.MyUser;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* 这里不需要使用@Repository注解数据访问层,
* 因为Spring Boot自动配置了JpaRepository
*/
public interface UserRepository extends JpaRepository<MyUser, Integer>{
public MyUser findByUname(String uname);
public List<MyUser> findByUnameLike(String uname);
}
package com.yzh.jpa.service;
import java.util.List;
import com.yzh.jpa.entity.MyUser;
public interface UserService {
public void saveAll();
public List<MyUser> findAll();
public MyUser findByUname(String uname);
public List<MyUser> findByUnameLike(String uname);
public MyUser getOne(int id);
}
package com.yzh.jpa.service;
import java.util.ArrayList;
import java.util.List;
import com.yzh.jpa.entity.MyUser;
import com.yzh.jpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Autowired
private UserRepository userRepository;//依赖注入数据访问层
@Override
public void saveAll() {
MyUser mu1 = new MyUser();
mu1.setUname("叶子航");
mu1.setUsex("男");
mu1.setAge(20);
MyUser mu2 = new MyUser();
mu2.setUname("彭子健");
mu2.setUsex("男");
mu2.setAge(18);
MyUser mu3 = new MyUser();
mu3.setUname("单紫晨");
mu3.setUsex("男");
mu3.setAge(15);
List<MyUser> users = new ArrayList<MyUser>();
users.add(mu1);
users.add(mu2);
users.add(mu3);
//调用父接口中的方法saveAll
userRepository.saveAll(users);
}
@Override
public List<MyUser> findAll() {
//调用父接口中的方法findAll
return userRepository.findAll();
}
@Override
public MyUser findByUname(String uname) {
return userRepository.findByUname(uname);
}
@Override
public List<MyUser> findByUnameLike(String uname) {
return userRepository.findByUnameLike("%" + uname + "%");
}
@Override
public MyUser getOne(int id) {
//调用父接口中的方法getOne
return userRepository.getOne(id);
}
}
package com.yzh.jpa.controller;
import com.yzh.jpa.service.UserService;
import com.yzh.jpa.service.UserServiceImpl;
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.ResponseBody;
@Controller
public class UserTestController {
@Autowired
private UserService userService;
@RequestMapping("/save")
@ResponseBody
public String save() {
userService.saveAll();
return "保存用户成功!";
}
@RequestMapping("/findByUname")
public String findByUname(String uname, Model model) {
model.addAttribute("title", "根据用户名查询一个用户");
model.addAttribute("auser", userService.findByUname(uname));
return "showAuser";
}
@RequestMapping("/getOne")
public String getOne(int id, Model model) {
model.addAttribute("title", "根据用户id查询一个用户");
model.addAttribute("auser",userService.getOne(id));
return "showAuser";
}
@RequestMapping("/findAll")
public String findAll(Model model){
model.addAttribute("title", "查询所有用户");
model.addAttribute("allUsers",userService.findAll());
return "showAll";
}
@RequestMapping("/findByUnameLike")
public String findByUnameLike(String uname, Model model){
model.addAttribute("title", "根据用户名模糊查询所有用户");
model.addAttribute("allUsers",userService.findByUnameLike(uname));
return "showAll";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>显示查询结果</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" />
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Spring Data JPA简单查询</h3>
</div>
</div>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><span th:text="${title}"></span></h3>
</div>
<div class="panel-body">
<div class="table table-responsive">
<table class="table table-bordered table-hover">
<tbody class="text-center">
<tr th:each="user:${allUsers}">
<td>
<span th:text="${user.id}"></span>
</td>
<td>
<span th:text="${user.uname}"></span>
</td>
<td>
<span th:text="${user.usex}"></span>
</td>
<td>
<span th:text="${user.age}"></span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>显示查询结果</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" />
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Spring Data JPA简单查询</h3>
</div>
</div>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><span th:text="${title}"></span></h3>
</div>
<div class="panel-body">
<div class="table table-responsive">
<table class="table table-bordered table-hover">
<tbody class="text-center">
<tr>
<td>
<span th:text="${auser.id}"></span>
</td>
<td>
<span th:text="${auser.uname}"></span>
</td>
<td>
<span th:text="${auser.usex}"></span>
</td>
<td>
<span th:text="${auser.age}"></span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>