Spring Boot 集成 JPA 的步骤
Spring Boot 集成 JPA 的步骤
配置依赖
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.5.2.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.41'
编写配置文件
1、JPA 的配置
@EnableJpaRepositories(basePackages = "com.liwei.dao")
@EntityScan(basePackages = "com.liwei.entity")
public class JpaConfig {
}
2、数据库连接的配置和其它配置
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/spring-boot-security?useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
创建数据库的 SQL 语句:
CREATE DATABASE `spring-boot-security` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
编写 dao 层和 entity 的代码
public interface UserRepository extends JpaRepository<User, Long> {
User findByUserName(String userName);
}
public interface UserRolesRepository extends JpaRepository<UserRole,Long> {
}
@Table(name = "t_user")
@Entity
public class User {
private Long userId;
private String userName;
private String password;
private String email;
private int enabled;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@Column(name = "user_name")
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getEnabled() {
return enabled;
}
public void setEnabled(int enabled) {
this.enabled = enabled;
}
public User() {
}
public User(Long userId, String userName, String password, String email, int enabled) {
this.userId = userId;
this.userName = userName;
this.password = password;
this.email = email;
this.enabled = enabled;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", enabled=" + enabled +
'}';
}
}
@Table(name = "t_user_roles")
@Entity
public class UserRole {
private Long userRoleId;
private Long userId;
private String role;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_role_id")
public Long getUserRoleId() {
return userRoleId;
}
public void setUserRoleId(Long userRoleId) {
this.userRoleId = userRoleId;
}
@Column(name = "user_id")
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
为了便于测试,我们还编写了服务层的方法:
public interface IUserService {
User findByUserName(String userName);
}
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserRepository userRepository;
@Override
public User findByUserName(String userName) {
return userRepository.findByUserName(userName);
}
}
编写测试代码
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.2.RELEASE'
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootSecurityApplication.class)
public class IUserServiceTest {
@Autowired
private IUserService userService;
@Autowired
private UserRepository userRepository;
@Test
public void initData() throws Exception {
User user = new User();
user.setUserName("liwei");
user.setPassword("123456");
user.setEmail("121088825");
user.setEnabled(1);
User user1 = userRepository.save(user);
System.out.println(user1);
}
@Test
public void findByUserName() throws Exception {
User user = userService.findByUserName("liwei");
System.out.println(user);
}
}
Spring Boot 集成 JPA 的步骤的更多相关文章
- Spring Boot集成JPA的Column注解命名字段无效的问题
偶然发现,Spring Boot集成jpa编写实体类的时候,默认使用的命名策略是下划线分隔的字段命名. Spring Boot版本:1.5.4.release 数据表: id int, userNam ...
- spring boot 集成 Mybatis,JPA
相对应MyBatis, JPA可能大家会比较陌生,它并不是一个框架,而是一组规范,其使用跟Hibernate 差不多,原理层面的东西就不多讲了,主要的是应用. Mybatis就不多说了,SSM这三个框 ...
- MyBatis初级实战之一:Spring Boot集成
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 【实验一 】Spring Boot 集成 hibernate & JPA
转眼间,2018年的十二分之一都快过完了,忙于各类事情,博客也都快一个月没更新了.今天我们继续来学习Springboot对象持久化. 首先JPA是Java持久化API,定义了一系列对象持久化的标准,而 ...
- (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...
- (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...
- Spring Boot 集成 FreeMarker 详解案例(十五)
一.Springboot 那些事 SpringBoot 很方便的集成 FreeMarker ,DAO 数据库操作层依旧用的是 Mybatis,本文将会一步一步到来如何集成 FreeMarker 以及配 ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- Spring Boot集成Spring Data Reids和Spring Session实现Session共享
首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...
随机推荐
- 第9周总结&实验报告7
完成火车站售票程序的模拟. 要求:(1)总票数1000张:(2)10个窗口同时开始卖票:(3)卖票过程延时1秒钟:(4)不能出现一票多卖或卖出负数号票的情况.一:实验代码 package first; ...
- openmvg中cmd模块解析
---恢复内容开始--- 在openmvg库中,定义了一个CmdLine类来定义例程的输入参数格式.源文件为.\openMVG\src\third_party\cmdLine\cmdLine.h. 先 ...
- Linux 環境下安裝swoole
一.先安装依赖 yum -y install gcc gcc-c++ autoconf automake yum -y install zlib zlib-devel openssl openssl- ...
- 用yum快速搭建LAMP平台与虚拟域名配置
实验环境: [root@nmserver-7 html]# cat /etc/redhat-release CentOS release 7.3.1611 (AltArch) [root@nmserv ...
- python是强语言还是弱语言?
python是强语言还是弱语言,没有一个具体官方的说法 数据类型也就是变量类型,一般编程语言的变量类型可以分成下面两类. 静态类型与动态类型 静态类型语言:一种在编译期间就确定数据类型的语言.大多数静 ...
- 使用SecureCRT 8.5快速打开sftp传输文件
一般使用Windows系统上安装的SecureCRT 8.5软件远程连接Linux服务器,通常给Linux系统传输文件或者使用FTP,或者使用SFTP等其他第三方软件,有时Linux系统上还需要做其他 ...
- 012-linux系统管理——进程管理与工作管理
linux系统管理——进程管理 top 命令是使用 top - :: up :, user, load average: 0.06, 0.60, 0.48 #五分钟钱,十分钟前,十五分钟前负载的值根据 ...
- Codeforces 957 水位标记思维题
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- Linux一键安装LNMP环境
Linux一键安装LNMP环境 官方地址:https://lnmp.org/. 参考安装步骤:https://lnmp.org/install.html. 一键安装可以选择mysql版本.php版本, ...
- Python核心技术与实战——九|面向对象
在搞清了各种数据类型.赋值判断.循环以后如果是从C++.Java语言入手的,就会有一个深坑要过:OOP(object oriented programming):公私有保护.多重继承.多态派生.纯函数 ...