springData jpa 入门案例

1.导入maven坐标

 
    4.2.4.RELEASE
    5.0.7.Final
    1.6.6
    1.2.12
    0.9.1.2
    5.1.6
  

  
    
    
      junit
      junit
      4.9
      test
    

    
    
      org.aspectj
      aspectjweaver
      1.6.8
    

    
      org.springframework
      spring-aop
      ${spring.version}
    

    
      org.springframework
      spring-context
      ${spring.version}
    

    
      org.springframework
      spring-context-support
      ${spring.version}
    

    
      org.springframework
      spring-orm
      ${spring.version}
    

    
      org.springframework
      spring-beans
      ${spring.version}
    

    
      org.springframework
      spring-core
      ${spring.version}
    

    

    
    
      org.hibernate
      hibernate-core
      ${hibernate.version}
    
    
      org.hibernate
      hibernate-entitymanager
      ${hibernate.version}
    
    
      org.hibernate
      hibernate-validator
      5.2.1.Final
    
    

    
    
      c3p0
      c3p0
      ${c3p0.version}
    
    

    
    
      log4j
      log4j
      ${log4j.version}
    

    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    

    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    


    
      mysql
      mysql-connector-java
      ${mysql.version}
    

    
      org.springframework.data
      spring-data-jpa
      1.9.0.RELEASE
    

    
      org.springframework
      spring-test
      ${spring.version}
    

    
    
      javax.el
      javax.el-api
      2.2.4
    

    
      org.glassfish.web
      javax.el
      2.2.4
    
    
  

2.将springData 与 spring整合




    
        
        
        
        
    
    
    
        
        
        
        
        
            
        
        
        
            
                
                
                
                
                
                
                
                
            
        
        
        
            
        
    
    
    
    
    
        
    
    
    
    
        
            
            
            
            
            
            
            
        
    

    
    
        
        
    
    
    

3.将实体类与表映射起来

@Entity
@Table(name = "tab_student")
public class Student {

    @Column(name = "id")
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name = "birthday")
    private LocalDateTime birthday;

4.定义dao接口,实现JpaRepository和JpaSpecificationExecutor二个接口
springData jpa 入门案例_第1张图片

5.简单的crud
springData jpa 入门案例_第2张图片: count
查看数据时否存在:
二种情况:一种通过select * from table where id = ?
第二种:通过select count(1) where id = ?
exists (判断数据是否存在):springDataKJpa走的是查询count
Hibernate: select count(*) as col_0_0_ from tab_student student0_ where student0_.id=? and 1=1
在这里插入图片描述
springData jpa 入门案例_第3张图片
springDataJpa执行原理:
1.通过JdkDynamicAopProxy的invoke方法创建了一个动态代理对象
springData jpa 入门案例_第4张图片
2.SimpleJpaRepository当中封装了JPA的操作(借助JPA的api完成数据库的CRUD)
springData jpa 入门案例_第5张图片
3.通过hibernate完成数据库操作(封装了jdbc)

你可能感兴趣的:(springData jpa 入门案例)