Spring Boot(五):Spring Boot Jpa 的使用

1、Spring Boot Jpa简介java

Jpa(Java Persistence API)是sun官方提出的java持久化规范。它为java开发人员提供了一种对象/关联映射工具,来管理java应用中的关系数据。它的出现主要是为了简化现有的持久化开发工做和整合ORM技术,结束如今hibernate、toplink、jdo等ORM框架各自为营的局面。mysql

值得注意的是,Jpa是在充分吸取了现有ORM框架的基础上发展而来的,具备易于使用,伸缩性强等优势。从目前的开发社区的反应上看,Jpa受到了极大的支持和赞赏,其中就包括了spring和EJB的开发团队。spring

注意:Jpa 是一套规范,不是一套产品,那么像 Hibernate,TopLink,JDO 他们是一套产品,若是说这些产品实现了这个 Jpa 规范,那么咱们就能够叫他们为 Jpa 的实现产品。sql

Spring Boot Jpa是Spring基于ORM框架、JPA规范的基础上封装的一套Jpa应用框架,可以使开发者用极简的代码便可实现对数据库的访问和操做。它提供了包括增删改查等在内的经常使用功能,且易于扩展!学习并使用Spring Boot Jpa能够极大提升开发效率。数据库

2、基本查询app

基本查询也分为两种,一种是 Spring Data 默认已经实现,一种是根据查询的方法来自动解析成 SQL。框架

一、预先生成方法分布式

Spring Boot Jpa默认预先生成了一些基本的CRUD方法。工具

(1)继承 JpaRepository学习

public interface UserRepository extends JpaRepository<User, Long> {
}

(2)使用默认方法

@Test
public void testBaseQuery() throws Exception {
	User user=new User();
	userRepository.findAll();
	userRepository.findOne(1l);
	userRepository.save(user);
	userRepository.delete(user);
	userRepository.count();
	userRepository.exists(1l);
	// ...
}

就不解释了根据方法名就看出意思来

二、自定义简单查询

自定义的简单查询就是根据方法名来自动生成 SQL,主要的语法

是findXXBy,readAXXBy,queryXXBy,countXXBy,getXXBy后面跟属性名称:

User findByUserName(String userName);

也使用一些加一些关键字 And、Or

User findByUserNameOrEmail(String username, String email);

修改、删除、统计也是相似语法

Long deleteById(Long id);
Long countByUserName(String userName)

基本上 SQL 体系中的关键词均可以使用,例如: like、ignoreCase、OrderBy。

List<User> findByEmailLike(String email);
User findByUserNameIgnoreCase(String userName);
List<User> findByUserNameOrderByEmailDesc(String email);

具体的关键字,使用方法和生成sql以下

Keyword Sample JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstnameIs,findByFirstnameEquals … where x.firstname = ?1
Between findByStartDateBetween … where x.startDate between ?1 and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age ⇐ ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection age) … where x.age not in ?1
TRUE findByActiveTrue() … where x.active = true
FALSE findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

三、复杂查询

在实际的开发中咱们须要用到分页、删选、连表等查询的时候就须要特殊的方法或者自定义 SQL

四、分页查询

分页查询在实际使用中很是广泛了,Spring Boot Jpa 已经帮咱们实现了分页的功能,在查询的方法中,须要传入参Pageable,当查询中有多个参数的时候Pageable建议作为最后一个参数传入.

Page<User> findALL(Pageable pageable);
Page<User> findByUserName(String userName,Pageable pageable);

Pageable 是 Spring 封装的分页实现类,使用的时候须要传入页数、每页条数和排序规则

@Test
public void testPageQuery() throws Exception {
	int page=1,size=10;
	Sort sort = new Sort(Direction.DESC, "id");
    Pageable pageable = new PageRequest(page, size, sort);
    userRepository.findALL(pageable);
    userRepository.findByUserName("testName", pageable);
}

五、限制查询

有时候咱们只须要查询前N个元素,或者支取前一个实体。

User findFirstByOrderByLastnameAsc();
User findTopByOrderByAgeDesc();
Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
List<User> findFirst10ByLastname(String lastname, Sort sort);
List<User> findTop10ByLastname(String lastname, Pageable pageable);

六、自定义sql查询

其实 Spring Data 觉大部分的 SQL 均可以根据方法名定义的方式来实现,可是因为某些缘由咱们想使用自定义的 SQL 来查询,Spring Data 也是完美支持的;在 SQL 的查询方法上面使用@Query注解,如涉及到删除和修改在须要加上@Modifying也能够根据须要添加@Transactional对事物的支持,查询超时的设置等。

@Modifying
@Query("update User u set u.userName = ?1 where u.id = ?2")
int modifyByIdAndUserId(String  userName, Long id);
	
@Transactional
@Modifying
@Query("delete from User where id = ?1")
void deleteByUserId(Long id);
  
@Transactional(timeout = 10)
@Query("select u from User u where u.emailAddress = ?1")
User findByEmailAddress(String emailAddress);

七、多表查询

多表查询 Spring Boot Jpa 中有两种实现方式,第一种是利用 Hibernate 的级联查询来实现,第二种是建立一个结果集的接口来接收连表查询后的结果,这里主要第二种方式。

首先须要定义一个结果集的接口类。

public interface HotelSummary {

	City getCity();

	String getName();

	Double getAverageRating();

	default Integer getAverageRatingRounded() {
		return getAverageRating() == null ? null : (int) Math.round(getAverageRating());
	}

}

查询的方法返回类型设置为新建立的接口

@Query("select h.city as city, h.name as name, avg(r.rating) as averageRating "
		- "from Hotel h left outer join h.reviews r where h.city = ?1 group by h")
Page<HotelSummary> findByCity(City city, Pageable pageable);

@Query("select h.name as name, avg(r.rating) as averageRating "
		- "from Hotel h left outer join h.reviews r group by h")
Page<HotelSummary> findByCity(Pageable pageable);

使用

Page<HotelSummary> hotels = this.hotelRepository.findByCity(new PageRequest(0, 10, Direction.ASC, "name"));
for(HotelSummary summay:hotels){
		System.out.println("Name" +summay.getName());
	}

在运行中 Spring 会给接口(HotelSummary)自动生产一个代理类来接收返回的结果,代码汇总使用getxx的形式来获取。

3、多数据源的支持

一、同源数据库的多源支持

平常项目中由于使用的分布式开发模式,不一样的服务有不一样的数据源,经常须要在一个项目中使用多个数据源,所以须要配置Spring Data Jpa对多数据源的使用,通常分三步:

(1)配置多数据源

(2)不一样源的实体类放入不一样包路径

(3)声明不一样的包路径下使用不一样的数据源、事务支持

二、异构数据库多源支持

好比咱们的项目中,即须要对 mysql 的支持,也须要对 Mongodb 的查询等

实体类声明@Entity关系型数据库支持类型、声明@Document为 Mongodb 支持类型,不一样的数据源使用不一样的实体就能够了

interface PersonRepository extends Repository<Person, Long> {
 
}

@Entity
public class Person {
  
}

interface UserRepository extends Repository<User, Long> {
 
}

@Document
public class User {
  
}

可是,若是 User 用户既使用 Mysql 也使用 Mongodb 呢,也能够作混合使用

interface JpaPersonRepository extends Repository<Person, Long> {
 
}

interface MongoDBPersonRepository extends Repository<Person, Long> {
 
}

@Entity
@Document
public class Person {
  
}

也能够经过对不一样的包路径进行声明,好比 A 包路径下使用 mysql,B 包路径下使用 MongoDB

@EnableJpaRepositories(basePackages = "com.neo.repositories.jpa")
@EnableMongoRepositories(basePackages = "com.neo.repositories.mongo")
interface Configuration { }

4、使用枚举

使用枚举的时候,咱们但愿数据库中存储的是枚举对应的 String 类型,而不是枚举的索引值,须要在属性上面添加@Enumerated(EnumType.STRING)注解

@Enumerated(EnumType.STRING) 
@Column(nullable = true)
private UserType type;

5、不须要和数据库映射的属性

正常状况下咱们在实体类上加入注解@Entity,就会让实体类和表相关连若是其中某个属性咱们不须要和数据库来关联只是在展现的时候作计算,只须要加上@Transient属性既可。

@Transient
private String  userName;

 

一杯咖啡,一行代码,是一种境界,也是一种追求!

相关文章
相关标签/搜索