目录
`Spring Data JPA` 简述`Spring Data JPA` 使用示例`Maven` 依赖配置文件 `application.properties`实体类`springboot` 的启动类数据持久化的方式`Spring Data JPA` 提供的接口默认实现自定义符合 `Spring Data JPA` 规则的查询`jpql` 查询与 `sql` 查询`jpql` 查询示例`sql` 查询示例自定义查询方法示例(使用 `sql` 查询)Spring Data JPA
简述
Spring Data JPA
是Spring
基于Spring Data
框架、在JPA
规范的基础上开发的一个框架,使用Spring Data JPA
可以极大地简化JPA
的写法,可以在几乎不用写实现的情况下实现对数据库的访问和操作,除了CRUD
外,还包括分页和排序等一些常用的功能
Spring Data JPA
使用示例
Maven
依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>
配置文件application.properties
spring.jpa.show-sql=truespring.jpa.database=MYSQL# 可以查看 sql 语句spring.jpa.properties.hibernate.format_sql=true
实体类
@Entity@Table(name = "user")// jpa插入数据时,自动插入当前的时间戳@EntityListeners(AuditingEntityListener.class)public class User {@Id// 声明主键列@GeneratedValue(strategy = GenerationType.IDENTITY)// 主键的生成策略private Integer id;private String phone;private String email;private Byte isActive;@CreatedDate// jpa插入数据时,自动插入当前的时间戳private Date createTime;@LastModifiedDate// jpa插入数据时,自动插入当前的时间戳private Date updateTime;......}
其中,注解@Entity
用来标记该类是一个JPA
实体类,并使用了注解@Table
指定关联的数据库的表名;注解@Id
用来定义记录的唯一标识,并结合注解@GeneratedValue
将其设置为自动生成
springboot
的启动类
@SpringBootApplication@EnableJpaAuditing// jpa插入数据时,自动插入当前的时间戳public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args); }}
@EnableJpaAuditing
:jpa
插入数据时,自动插入当前的时间戳
数据持久化的方式
使用Spring Data JPA
提供的接口默认实现自定义符合Spring Data JPA
规则的查询方法,由框架将其自动解析为SQL
Spring Data JPA
提供的接口默认实现
Spring Data JPA
提供了一些实现了基本的数据库操作的接口类,如下图所示
自定义符合Spring Data JPA
规则的查询
除了可以直接使用Spring Data JPA
接口提供的基础功能外,Spring Data JPA
还允许开发者自定义查询方法,对于符合以下命名规则的方法,Spring Data JPA
能够根据其方法名为其自动生成SQL
,除了使用示例中的find
关键字,还支持的关键字有:query、get、read、count、delete
等
查询时,很多时候需要同时使用多个属性进行查询,而且查询的条件也各不相同,Spring Data JPA
为此提供了一些条件查询的关键字,我把常用的都整理了一下,如下表
jpql
查询与sql
查询
jpql
语法和sql
其实大同小异,jpql
是针对实体类进行的操作,sql
是直接对数据库表的操作,所以jpql
里只是将sql
里数据库表名、列名等信息替换为实体类属性而已
jpql
查询示例
想要使用jpql
的前提是你已经使用注解配置好了实体类以及参数
/** * User: 是实体类* userId 和 username: 是实体类的属性*/@Query(value = "from User where userId = ?2 and userName = ?1")User findUserByIdAndName(String name, int id);
sql
查询示例
/*** tbl_user: 是数据库表名* user_name:是表的字段*/@Query(value = "select * from tbl_user where user_name like ?",nativeQuery = true)List<User> sqlFindByName(String name);
自定义查询方法示例(使用sql
查询)
@Repository// User:当前需要映射的实体// Long:当前映射的实体中的ID的类型public interface UserRepository extends JpaRepository<User, Long> {User findByUsername(String username);@Transactional@Modifying@Query(value = "update jsh_user set tenant_id=?1 where username=?2", nativeQuery = true)void updateTenantUserName(Long tenantId, String username);Optional<User> findById(Long id);@Transactional@Modifying@Query(value = "update jsh_user set password=?1 where id=?2", nativeQuery = true)Integer updateUser(String pwd, Long id);// 模糊查询并分页Page<User> findByUsernameContaining(String username, Pageable pageable);Page<User> findByUsernameContainingAndLoginameContaining(String username, String loginame, Pageable pageable);// 批量删除@Transactional@Modifying@Query(value = "delete from jsh_user where id in(?1)", nativeQuery = true)void deleteUser(List<Long> ids);}
nativeQuery
:是否使用sql
查询,默认nativeQuery
为false
(即jpql
查询)@Modifying
:当使用@Query
写jpql
时,如果是删除或修改操作,需要加入此注解,同时也要加上@Transactional
进行修饰以通知SpringData
这是一个UPDATE
或DELETE
操作;默认情况下是只读,所以查询不需要?1
与?2
对应方法中的两个参数Pageable
:可以封装分页,排序等参数sql
查询语句:写的是表名与字段名