spring data jpa简单使用----逐渐完善

第一步:

  导入依赖

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


第二步:
  配置文件
   spring:
#数据库
datasource:
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: jiang1321
#jpa
jpa:
hibernate:
ddl-auto: update  //表策略-更新表
show-sql: true    //打印sql
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect  //指定数据库引擎
第三步:
  实体类:
    
@DynamicInsert   //sql语句无视为空的属性
@Entity      //表示为实体类
@Data
@Table(name = "usertable")  //数据库中表名称为usertable
public class UserTable {
    @Id  //主键
@GeneratedValue(strategy = GenerationType.IDENTITY)  //主键策略--自动增长
private Integer id;
@Column(nullable = false,unique = true)  //列 unique唯一
private String name;
@Column(nullable = false)          //列 非空
private String password;
@ManyToOne(fetch = FetchType.EAGER)    //表示为多对一关联
@JoinColumn(name = "roleid")//roleid user表中的外键名称  //外键  根据Role类型上的注解 @Table 找到指定表  name是表中外键的名称,该列指向Role表的id
Role role;
}
第四步:
  开发
    
public interface UserDao extends JpaRepository<UserTable,Integer>   //只需继承Repository
    void deleteById(Integer integer);                  //jpa根据方法名称自动生成sql
    UserTable findByName(String name);
    Page<UserTable> findAll(Pageable pageable);
    

查询关键字
逻辑关键字 关键字表达
AND                             And

OR                               Or

AFTER                         After, IsAfter

BEFORE                        Before, IsBefore

CONTAINING                     Containing,IsContaining,Contains

BETWEEN                     Between, IsBetween

ENDING_WITH                    EndingWith,IsEndingWith,EndsWith

EXISTS                        Exists

FALSE                        False, IsFalse

GREATER_THAN                  GreaterThan, IsGreaterThan

GREATER_THAN_EQUALS              GreaterThanEqual, IsGreaterThanEqual

IN                          In, IsIn

IS                          Is、、Equals(或没有关键字)

IS_EMPTY                      IsEmpty, Empty

IS_NOT_EMPTY                    IsNotEmpty, NotEmpty

IS_NOT_NULL                   NotNull, IsNotNull

IS_NULL                      Null, IsNull

LESS_THAN                    LessThan, IsLessThan

LESS_THAN_EQUAL                LessThanEqual, IsLessThanEqual

LIKE                        Like, IsLike

NEAR                        Near, IsNear

NOT                        Not, IsNot

NOT_IN                      NotIn, IsNotIn

NOT_LIKE                    NotLike, IsNotLike

REGEX                      Regex,MatchesRegex,Matches

STARTING_WITH                StartingWith,IsStartingWith,StartsWith

TRUE                      True, IsTrue

WITHIN                    Within, IsWithin

 

猜你喜欢

转载自www.cnblogs.com/jiangyukuan1996/p/13370417.html