02_SpringData_JPA

一、.环境搭建

1.在pom.xml添加mysql,spring-data-jpa依赖

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
</dependency>


<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

 

2.在application.properties文件中配置mysql连接配置文件

########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

 

3.在application.properties文件中配置JPA配置信息

########################################################
### Java Persistence Api
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

4.编写测试例子

(1) 创建实体类Demo,如果已经存在,可以忽略。

(2) 创建jpa repository类操作持久化(CrudRepository)。

(3) 创建service类。

(4) 创建restful请求类。

(5) 测试;

二、springdata 接口

1.Repository 接口

是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法 : public interface Repository<T, ID extends Serializable> { }

有这么几点需要强调下:

(1). Repository是一个空接口,即是一个标记接口;

(2). 若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean纳入到IOC容器中,进而可以在该接口中定义满足一定规范的方法。

(3). 实际上也可以通过@RepositoryDefinition,注解来替代继承Repository接口。

(4). 查询方法以find | read | get开头;

(5). 涉及查询条件时,条件的属性用条件关键字连接,要注意的是条件属性以首字母大写。

(6).使用@Query注解可以自定义JPQL语句实现更灵活的查询。

2.CrudRepository接口

CrudRepository 接口提供了最基本的对实体类的添删改查操作

--T save(T entity);//保存单个实体   

--Iterable<T> save(Iterable<? extends T> entities);//保存集合         

--T findOne(ID id);//根据id查找实体          

--boolean exists(ID id);//根据id判断实体是否存在          

--Iterable<T> findAll();//查询所有实体,不用或慎用!          

--long count();//查询实体数量          

--void delete(ID id);//根据Id删除实体          

--void delete(T entity);//删除一个实体   

--void delete(Iterable<? extends T> entities);//删除一个实体的集合          

--void deleteAll();//删除所有实体,不用或慎用!   

3.PagingAndSortingRepository接口

该接口提供了分页与排序功能

--Iterable<T> findAll(Sort sort); //排序

--Page<T> findAll(Pageable pageable); //分页查询(含排序功能)

4.JpaRepository接口

查找所有实体,排序、查找所有实体,执行缓存与数据库同步

5.JpaSpecificationExecutor

不属于Repository体系,实现一组 JPA Criteria 查询相关的方法,封装 JPA Criteria 查询条件。通常使用匿名内部类的方式来创建该接口的对象。

6.自定义 Repository 

可以自己定义一个MyRepository接口。

转载于:https://www.cnblogs.com/ai1988/p/7836219.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值