Springboot 整合 Spingdata jpa

    技术2022-07-11  123

    Springboot 整合 Sping data jpa

    1.配置Maven依赖

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

    2.配置properties文件

    #配置数据源 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3308/springbootdata?serverTimezone=Asia/Shanghai&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=1310947308 #JPA配置 spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect #是否显示查询语句 spring.jpa.show-sql=true #每次运行程序,没有表时会创建表,如果对象发生改变会更新表结构,原有数据不会清空,只会更新 # create:每次运行程序时,都会重新创建表,故而数据会丢失 # create-drop:每次运行程序时会先创建表结构,然后待程序结束时清空表 # update:每次运行程序,没有表时会创建表,如果对象发生改变会更新表结构,原有数据不会清空,只会更新(推荐使用) # validate:运行程序会校验数据与数据库的字段类型是否相同,字段不同会报错 # none: 禁用DDL处理 spring.jpa.hibernate.ddl-auto=update

    3.常用注解

    @Entity :声明这个类是一个实体类,使用默认的 orm 规则,即 class 名即数据库表中表名,class 字段名即表中的字段名

    @Table:指定了 Entity 所要映射到数据库表,name属性用于指定映射的数据库表的表名

    @Id :映射到数据库表的主键属性,一个实体只能有一个属性被映射为主键

    @GeneratedValue:主键的生成策略,其中有个属性 strategy 其值代表的是:

    AUTO:主键由程序控制, 是默认选项 ,不设置就是这个IDENTITY: 主键由数据库生成, 采用数据库自增长,数据库底层必须支持自增, Oracle不支持这种方式SEQUENCE: 通过数据库的序列产生主键,数据库底层必须支持序列, MYSQL 不支持TABLE :提供特定的数据库产生主键, 该方式更有利于数据库的移植

    @Column:定义字段名,若省略该注解,则默认使用属性名当作字段名

    4.JpaRepository接口方法

    delete:删除或批量删除findAll:查找所有findOne:查找单个save:保存单个或批量保存saveAndFlush:保存并刷新到数据库

    5.使用JPQL或SQL的方式查询

    @Query: 注解的使用非常简单,只需在方法上面标注该注解,同时提供一个JPQL查询语句即可 nativeQuery (true): 使用本地sql的方式查询 @Modifying :来将该操作标识为修改查询,这样框架最终会生成一个更新的操作,而非查询 public interface CustomerDao extends JpaRepository<Customer, Long>,JpaSpecificationExecutor<Customer> { //@Query 使用jpql的方式查询。 @Query(value="from Customer") public List<Customer> findAllCustomer(); @Query(value="update Customer set custName = ?1 where custId = ?2") @Modifying public void updateCustomer(String custName,Long custId); /** * nativeQuery : 使用本地sql的方式查询 */ @Query(value="select * from cst_customer",nativeQuery=true) public void findSql(); }

    6.方法命名规则查询

    按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写。框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析。

    KeywordSampleJPQLAndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2Is,EqualsfindByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2LessThanfindByAgeLessThan… where x.age < ?1LessThanEqualfindByAgeLessThanEqual… where x.age ⇐ ?1GreaterThanfindByAgeGreaterThan… where x.age > ?1GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1AfterfindByStartDateAfter… where x.startDate > ?1BeforefindByStartDateBefore… where x.startDate < ?1IsNullfindByAgeIsNull… where x.age is nullIsNotNull,NotNullfindByAge(Is)NotNull… where x.age not nullLikefindByFirstnameLike… where x.firstname like ?1NotLikefindByFirstnameNotLike… where x.firstname not like ?1StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 (parameter bound with appended %)EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 (parameter bound with prepended %)ContainingfindByFirstnameContaining… where x.firstname like ?1 (parameter bound wrapped in %)OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname descNotfindByLastnameNot… where x.lastname <> ?1InfindByAgeIn(Collection ages)… where x.age in ?1NotInfindByAgeNotIn(Collection age)… where x.age not in ?1TRUEfindByActiveTrue()… where x.active = trueFALSEfindByActiveFalse()… where x.active = falseIgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)
    Processed: 0.014, SQL: 9