Spring-Data-Jpa的使用

在spring-boot中,关于spring-data-jpa的使用做一个记录: Spring Data JPA 是 Spring 基于 ORM 框架、JPA 规范的基础上封装的一套 JPA 应用框架,可以使开发者用极简的代码便可实现对数据的访问和操做。它提供了包括增删改查等在内的经常使用功能,且易于扩展! Spring Data JPA 让咱们解脱了 DAO 层的操做,基本上全部 CRUD 均可以依赖于它来实现。前端

依赖包:java

<dependency>
    <groupId>org.Springframework.boot</groupId>
    <artifactId>Spring-boot-starter-data-jpa</artifactId>
</dependency>
 <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

配置文件添加:mysql

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true

关于配置的说明:spring

spring.jpa.properties.hibernate.hbm2ddl.auto参数的做用主要用于:自动建立 | 更新 | 验证数据库表结构,总共有四个值:sql

create:每次加载 hibernate 时都会删除上一次的生成的表,而后根据 model 类再从新来生成新表,哪怕两次没有任何改变也要这样执行,这就是致使数据库表数据丢失的一个重要缘由。数据库

create-drop:每次加载 hibernate 时根据 model 类生成表,可是 sessionFactory 一关闭,表就自动删除。服务器

update:最经常使用的属性,第一次加载 hibernate 时根据 model 类会自动创建起表的结构(前提是先创建好数据库),之后加载 hibernate 时根据 model 类自动更新表结构,即便表结构改变了,但表中的行仍然存在,不会删除之前的行。要注意的是当部署到服务器后,表结构是不会被立刻创建起来的,是要等应用第一次运行起来后才会。session

validate:每次加载 hibernate 时,验证建立数据库表结构,只会和数据库中的表进行比较,不会建立新表,可是会插入新值。框架

spring.jpa.properties.hibernate.dialect:主要是指定生成表的存储引擎spring-boot

spring.jpa.show-sql:是否打印出自动生产的 SQL

添加实体类

@Entity
@Table(name = "t_people")
public class People implements Serializable{
    @Id
    @GeneratedValue
    private Long id;
    //添加人
    @Column(name = "add_user_name",nullable = false)
    private String addUserName;
    //修改人
    private String updateUserName;
    //添加时间
    @Temporal(TemporalType.TIMESTAMP)
    private Date addTime;
    //修改时间
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;
    //用户名
    private String userName;
    //密码
    private String password;
    //邮箱
    private String email;
    //电话号码
    private String telephone;
    @Transient
    private String testMsg;

使用spring-data-jpa不须要单独去数据库添加对应的表,只须要在实体类加上相应的注解,会自动生成咱们须要的表;

注解的说明:

@Entity :指定哪些实体类须要在数据库生成对应的表,不加将不生成表

@Table:能够不加,默认使用实体类名做为表名(默认按照驼峰命名的方式生成表名,例如:实体类名是SystemPeople,生成的表名是system_people),加注解后能够自定义表名

@Id和 @GeneratedValue:指定哪一个字段为主键id,而且每次新增id自动增加

@Column :能够不加,默认实体类属性生成表的列名,同表名的生成方式同样,name属性能够自定义列名,nullable设置字段是否能够为空,默认值为true,能够为空,为空时能够不加

@Temporal:设置实体类字段是Date类型时生成的表字段的类型;

@Transient:若是实体类的字段只是为了计算或者前端展现,不想生成的话加上该注解

添加dao:

public interface PeopleRepository extends JpaRepository<People,Long>, JpaSpecificationExecutor<People>{

    People findByUserName(String userName);

}

说明:JpaRepository封装好了不少增删改查,所以基本的数据库操做不用再定义查询方法

自定义简单查询

自定义的简单查询就是根据方法名来自动生成 SQL,主要的语法是 findXXBy、 findByXX、readAXXBy、queryXXBy、countXXBy、getXXBy 后面跟属性名称;如:findByUserName; 也能够加一些关键字 And、Or:

People findByUserNameOrPhone(String username, String phone);

** 删除或统计也是相似语法:**

Long deleteById(Long id);

Long countByUserName(String userName)

LIKE、IgnoreCase、OrderBy使用:

List<People > findByEmailLike(String email);

People findByUserNameIgnoreCase(String userName);

List<People > findByUserNameOrderByEmailDesc(String email);

** 自定义sql查询**

Spring Data支持大部分的SQL操做,但也支持咱们自定义sql,在 SQL 的查询方法上面使用 @Query 注解,如涉及到删除和修改须要加上 @Modifying,也能够根据须要添加 @Transactional 对事物的支持,查询超时的设置等。由于这里的SQL是HQL因此 sql的表名和字段名直接写实体类名和属性名,若是要写真实的表名,能够在sql后加上 nativeQuery = true

@Modifying
    @Query(value = "update People set userName = ?1 where id = ?2")
    void updateUserName(String userName,long id);
    @Modifying
    @Query(value = "update t_people set user_name = ?1 where id = ?2", nativeQuery = true)
    void updateUserName(String userName,long id);
相关文章
相关标签/搜索