数据访问层采用JPA集成MySQL
简介
JPA采用仓库模式实现数据持久化处理,它屏蔽底层处理细节,简化开发应用程序时对数据源处理的过程。数据访问层采用JPA,主要以Java实体类为核心,实体类封装数据字典映射数据表,结合仓库类实现数据的CRUD. 需要知道的是JPA是Java Persistence API的缩写。
MySQL是常见的开源免费关系型数据库,应用广泛支持良好,一般作为项目开发的首选。此次学习我们采用IDEA搭建Spring Data JPA + MySQL项目。
项目搭建
- 创建新项目
- 初始化项目,设置Group、Artifact、Package,项目采用Maven构建,Java版本采用v8
- 项目依赖选择:Spring Boot DevTools、Lombok、Spring Web、Thymeleaf、Spring Data JPA、MySQL Driver.
- 完成项目创建
项目配置
- 修改application.properties为application.yml
设置配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| server: port: 10016 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test_db?useTimezone=true&serverTimezone=GMT%2b8&characterEncoding=utf8 username: testUser password: testPwdxxxxxx jpa: database: mysql show-sql: true generate-ddl: true
|
pom依赖配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
|
项目编码
项目分层
在项目正式编码前,我们简单的定义下各个package,用于项目分层化分解。
config包:存放项目启动相关配置类;
controller包:存放spring mvc Controller类;
domain.entity包:存放数据表相关实体类,实体类用于映射数据表;
domain.enums包:存放枚举类型;
domain.model包:存放自定义模型,用于封装数据表以外的数据结构;
repository包:存放仓库类,提供数据的CRUD功能;
定义实体类
使用JPA作为数据访问层的第一步是封装定义数据实体类。在UserEntity实体类中,封装映射t_user表相关字段。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| package org.banana.authserver.domain.entity;
import javax.persistence.*; import java.util.Date;
@Table(name = "t_user") @Entity public class UserEntity { private Integer id; private String name; private String password; private Boolean disabled; private Boolean expired;
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", nullable = false) public Integer getId() { return this.id; }
public void setId(Integer id) { this.id = id; }
@Basic @Column(name = "name", nullable = true, length=255) public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
@Basic @Column(name = "password", nullable = true, length=255) public String getPassword() { return this.password; }
public void setPassword(String password) { this.password = password; }
@Basic @Column(name = "disabled", nullable = false) public Boolean getDisabled() { return this.disabled; }
public void setDisabled(Boolean disabled) { this.disabled = disabled; }
@Basic @Column(name = "expired", nullable = false) public Boolean getExpired() { return this.expired; }
public void setExpired(Boolean expired) { this.expired = expired; }
}
|
创建仓库接口
创建UserRepository接口,使用@Repository注解,继承CrudRepository、JpaRepository、JpaSpecificationExecutor,实现对数据表t_user的CRUD.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package org.banana.authserver.repository;
import org.banana.authserver.domain.entity.UserEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository;
@Repository public interface UserRepository extends CrudRepository<UserEntity, Integer>, JpaRepository<UserEntity, Integer>, JpaSpecificationExecutor<UserEntity> {
}
|
创建控制器类
创建HomeController,使用@Controller注解,创建一个action方法: index. index方法内部调用仓库接口,获取所有用户列表,并将模型数据传递到页面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package org.banana.authserver.controller;
import org.banana.authserver.domain.entity.UserEntity; import org.banana.authserver.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList; import java.util.List;
/** * @author yuanrui@live.cn * @since 2020/12/16 15:47 */ @Controller public class HomeController {
@Autowired UserRepository userRepository;
@GetMapping( value = { "/", "/index"}) public String index(Model model) { List<UserEntity> all = userRepository.findAll();
List list = new ArrayList<String>(); for (UserEntity ent : all){ list.add(ent.getName()); } String users = String.join(",", list);
model.addAttribute("users", users);
return "index"; } }
|
在资源文件夹resources的templates中创建index.html文件,编辑代码:
1 2 3 4 5 6 7 8 9 10
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>系统首页</title> </head> <body> 系统用户:[[${users}]] </body> </html>
|
Shift+F9调试项目,在浏览器中打开:http://localhost:10016/
MySQL脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '编号', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名', `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码', `disabled` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否禁用', `expired` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否过期', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `t_user` VALUES (1, 'test', '123456', b'0', b'0'); INSERT INTO `t_user` VALUES (2, 'abc', '123456', b'0', b'0');
|