Springboot快速入门教程(四)Spring Boot JPA-Hibernate介绍

版权声明:原创版权为博主所有,博主项目网址www.github.com/994683607,转载请注明出处。 https://blog.csdn.net/qq_35180973/article/details/82315973

#1.实验步骤:
这里写图片描述


#2.配置application.properties
1.在application.properties文件中配置mysql连接配置文件
########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
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

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

########################################################

Java Persistence Api

########################################################
1# Specify the DBMS
spring.jpa.database = MYSQL
1# Show or not log for each sql query
spring.jpa.show-sql = true
1# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
1# Naming strategy
[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
2#命名策略 自动创建表
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
3#stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
3.
(1) 创建实体类Demo,如果已经存在,可以忽略。
(2) 创建jpa repository类操作持久化(CrudRepository)。
(3) 创建service类。
(4) 创建restful请求类。
(5) 测试;


#3.Spring Boot Spring Data JPA接口介绍

##1.Repository接口
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 save(Iterable<? extends T> entities);//保存集合
    –T findOne(ID id);//根据id查找实体
    –boolean exists(ID id);//根据id判断实体是否存在
    –Iterable findAll();//查询所有实体,不用或慎用!
    –long count();//查询实体数量
    –void delete(ID id);//根据Id删除实体
    –void delete(T entity);//删除一个实体
    –void delete(Iterable<? extends T> entities);//删除一个实体的集合
    –void deleteAll();//删除所有实体,不用或慎用!

##3.PagingAndSortingRepository接口

该接口继承了Repository,还集成了提供了分页与排序功能
–Iterable findAll(Sort sort); //排序
–Page findAll(Pageable pageable); //分页查询(含排序功能)


##4.其他接口(不常用)

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

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

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


专栏目录:

Springboot快速入门教程(一) 搭建Springboot
Springboot快速入门教程(二) 返回JSON
Springboot快速入门教程(三) 热部署
Springboot快速入门教程(四) Spring Boot JPA-Hibernate介绍
Springboot快速入门教程(五) Spring Boot JPA-Hibernate实现
Springboot快速入门教程(六) Spring Boot JdbcTemplate
Springboot快速入门教程(七)全局异常捕捉
Springboot快速入门教程(八) Spring Boot 路径访问问题

猜你喜欢

转载自blog.csdn.net/qq_35180973/article/details/82315973
今日推荐