SpringBoot集成JPA持久层框架,简化数据库操作
作者:知了一笑 发布时间:2024-01-16 11:42:38
标签:SpringBoot,JPA
目录
与SpringBoot2.0整合
1、核心依赖
2、配置文件
3、实体类对象
4、JPA框架的用法
5、封装一个服务层逻辑
测试代码块
源代码地址
与SpringBoot2.0整合
1、核心依赖
<!-- JPA框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2、配置文件
spring:
application:
name: node09-boot-jpa
datasource:
url: jdbc:mysql://localhost:3306/data_jpa?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
ddl-auto几种配置说明
1)create
每次加载hibernate时都删除上一次的生成的表,然后根据bean类重新来生成新表,容易导致数据丢失,(建议首次创建时使用)。
2)create-drop
每次加载hibernate时根据bean类生成表,但是sessionFactory一关闭,表就自动删除。
3)update
第一次加载hibernate时根据bean类会自动建立起表的结构,以后加载hibernate时根据bean类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。
4)validate
每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
3、实体类对象
就是根据这个对象生成的表结构。
@Table(name = "t_user")
@Entity
public class User {
@Id
@GeneratedValue
private Integer id;
@Column
private String name;
@Column
private Integer age;
// 省略 GET SET
}
4、JPA框架的用法
定义对象的操作的接口,继承JpaRepository核心接口。
import com.boot.jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
// 但条件查询
User findByAge(Integer age);
// 多条件查询
User findByNameAndAge(String name, Integer age);
// 自定义查询
@Query("from User u where u.name=:name")
User findSql(@Param("name") String name);
}
5、封装一个服务层逻辑
import com.boot.jpa.entity.User;
import com.boot.jpa.repository.UserRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
@Resource
private UserRepository userRepository ;
// 保存
public void addUser (User user){
userRepository.save(user) ;
}
// 根据年龄查询
public User findByAge (Integer age){
return userRepository.findByAge(age) ;
}
// 多条件查询
public User findByNameAndAge (String name, Integer age){
return userRepository.findByNameAndAge(name,age) ;
}
// 自定义SQL查询
public User findSql (String name){
return userRepository.findSql(name) ;
}
// 根据ID修改
public void update (User user){
userRepository.save(user) ;
}
//根据id删除一条数据
public void deleteStudentById(Integer id){
userRepository.deleteById(id);
}
}
测试代码块
import com.boot.jpa.JpaApplication;
import com.boot.jpa.entity.User;
import com.boot.jpa.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = JpaApplication.class)
public class UserJpaTest {
@Resource
private UserService userService ;
@Test
public void addUser (){
User user = new User() ;
user.setName("知了一笑");
user.setAge(22);
userService.addUser(user);
User user1 = new User() ;
user1.setName("cicada");
user1.setAge(23);
userService.addUser(user1);
}
@Test
public void findByAge (){
Integer age = 22 ;
// User{id=3, name='知了一笑', age=22}
System.out.println(userService.findByAge(age));
}
@Test
public void findByNameAndAge (){
System.out.println(userService.findByNameAndAge("cicada",23));
}
@Test
public void findSql (){
// User{id=4, name='cicada', age=23}
System.out.println(userService.findSql("cicada"));
}
@Test
public void update (){
User user = new User() ;
// 如果这个主键不存在,会以主键自增的方式新增入库
user.setId(3);
user.setName("哈哈一笑");
user.setAge(25);
userService.update(user) ;
}
@Test
public void deleteStudentById (){
userService.deleteStudentById(5) ;
}
}
源代码地址
GitHub地址:知了一笑
https://github.com/cicadasmile/spring-boot-base
码云地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base
来源:https://mp.weixin.qq.com/s/q4bG2ci4ww-ptaF8mN560Q
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- Mysql中'employee'表内容如下:# __Desc__ = 从数据库中导出数据到excel数据表中import x
- 示例可以附件中下载 1.加载xml文件 加载XML文件共有2种方法,一是加载指定字符串,二是加载指定文件 2.获取element的方法 a)
- Python配对函数zip()1、zip将列表、元组或其他序列的元素进行配对新建成一个元组构成的列表,它生成列表长度由最短的序列决定:#zi
- 目录一、前言二、Json.loads与eval 性能对比1. eval2. json.loads一、前言最近发现一些小伙伴使用eval来处理
- 数据库设计(Database Design)的概念:数据库设计是指对于一个给定的应用环境,构造最优的数据库模式,建立数据库及其应用系统,使之
- 使用go mod之后,想要在goland中有代码提示,有两种方式,一种是使用gopath下的goimport工具,另一种是使用gomod自身
- 连接mysql常用工具mysql作为数据库服务器来运行,任何满足mysql通信规范的软件都可以作为客户端来连接服务器。常用的客户端:navi
- 字典获取最大和最小value对应的keymy_dict = {'x':500, 'y':5874, '
- 链接的 target 属性怎么用 JS 来控制? 在HTML 4.0 Strict和XHTML 1.0 STRICT里不允许在<a&g
- 目录一、socketserver实现并发二、验证客户端合法性一、socketserver实现并发tcp协议的socket是只能和一个客户端通
- bootstrap中有alert组件,如果点击关闭按钮后该组件会被删除而不是被隐藏,想再显示怎么办呢?bootstrap-alert.js源
- 如下所示:#待处理列表A= [1,2,3,4,5]#移动次数a = 3右移比较简单for i in range(a): A.insert(0
- 增加操作:变量名[key] = value # 通过key添加value值,如果key存在则覆盖 &nbs
- 不知道大家有没有一种感觉,每次当使用numpy数组的时候坐标轴总是傻傻分不清楚,然后就会十分的困惑,每次运算都需要去尝试好久才能得出想要的结
- 虽然现在的GMail已经看不到这个效果,但之前那个可爱的深红色Loading仍然让许多人喜爱。我也将这款效果融入了我自己的WordPress
- B站原视频爬取,我就不多说直接上代码。直接运行就好。B站是把视频和音频分开。要把2个合并起来使用。这个需要分析才能看出来。然后就是登陆这块是
- 本文实例总结了Python正则表达式常用函数。分享给大家供大家参考,具体如下:re.match()函数原型:match(pattern, s
- 本文主要介绍了python 边缘扩充方式的实现示例,具体如下:import cv2# big_pad=True:当目标图像高和宽均大于原图时
- 首先来看实例代码:# -*- coding:utf-8 -*-import requestsimport datetimeimport ti
- 一、什么是星号变量最初,星号变量是用在函数的参数传递上的,在下面的实例中,单个星号代表这个位置接收任意多个非关键字参数,在函数的*b位置上将