SpringDataJPA    

JPA规范

概述:

JPA是ORM框架的一个规范,而ORM框架就是不注重SQL语句,注重对象的操作,把对象和数据库进行隐射从而到达操作对象已达到操作数据库表;

例如:

INSERT INTO *** VALUES( id , user , address )

大体的SQL语句不会更改,那么只要我们把需要替换的给做成映射替换成真正要执行的语句就可以了;

SpringDataJPA_SpringDataJPA




JPA入门案例

pom.xml文件

4.0.0com.javacat7jpa-day011.0-SNAPSHOTUTF-85.0.7.Finalmysqlmysql-connector-java8.0.16junitjunit4.12org.hibernatehibernate-entitymanager${project.hibernate.version}
		org.hibernatehibernate-c3p0${project.hibernate.version}log4jlog4j1.2.17


JPA核心文件

	
    	org.hibernate.jpa.HibernatePersistenceProvider
        	


JPA整合映射对象

import javax.persistence.*;

@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private Integer userId;
    @Column(name = "user_name")
    private String name;
    @Column(name = "user_sex")
    private String sex;
    @Column(name = "user_address")
    private String address;

    public Integer getUserId() {return userId;}
    public void setUserId(Integer userId) {this.userId = userId;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public String getSex() {return sex;}
    public void setSex(String sex) {this.sex = sex;}
    public String getAddress() {return address;}
    public void setAddress(String address) {this.address = address;}
}


测试JAP

public class TestJPA {
    @Test
    public void testSave(){
        //实体类管理工厂
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
        EntityManager em = factory.createEntityManager();//实体类
        EntityTransaction tx = em.getTransaction();//事务管理器
        
        tx.begin();//开启事务

        User user = new User();
        user.setName("hello");
        user.setAddress("jpa");
        
        em.persist(user);//增
        
        em.remove(user);//删
        
        em.merge(user);//改
        
        User userInfo1 = em.find(User.class, 1);//立即查询
        
        User userInfo2 = em.getReference(User.class, 1);//懒加载查询
        
        tx.commit();//提交事务
        em.close();//关闭连接
        factory.close();//关闭工厂
    }
}




JPQL查询

JPQL和SQL语句不同,JQPL是查询对象,语法和SQL类似

//查询全部
String jpql = "from User ";
Query query = em.createQuery(jpql);
List resultList = query.getResultList();

//倒序排序
String jpql = "from User order by userId desc ";
Query query = em.createQuery(jpql);
List resultList = query.getResultList();

//统计查询
String sql = "select count(userId) from User ";
Query query = em.createQuery(sql);
Object singleResult = query.getSingleResult();

//分页查询
String sql = "from User ";
Query query = em.createQuery(sql);
query.setFirstResult(0);
query.setMaxResults(2);
List list = query.getResultList();

//条件查询
String sql = "from User where name = ?";
Query query = em.createQuery(sql);
query.setParameter(1,"sss");
Object singleResult = query.getSingleResult();




Spring整合SpringDataJPA

pom文件

5.0.2.RELEASE5.0.7.Final1.6.61.2.120.9.1.25.1.6junitjunit4.12testorg.aspectjaspectjweaver1.6.8org.springframeworkspring-aop${spring.version}org.springframeworkspring-context${spring.version}org.springframeworkspring-context-support${spring.version}org.springframeworkspring-orm${spring.version}org.springframeworkspring-beans${spring.version}org.springframeworkspring-core${spring.version}

    org.hibernatehibernate-core${hibernate.version}org.hibernatehibernate-entitymanager${hibernate.version}org.hibernatehibernate-validator5.2.1.Final

    c3p0c3p0${c3p0.version}

    log4jlog4j${log4j.version}org.slf4jslf4j-api${slf4j.version}org.slf4jslf4j-log4j12${slf4j.version}mysqlmysql-connector-java${mysql.version}org.springframework.dataspring-data-jpa1.9.0.RELEASEorg.springframeworkspring-test${spring.version}javax.eljavax.el-api2.2.4org.glassfish.webjavax.el2.2.4


整合dao层[applicationContext-mapper.xml]



整合事务[applicationContext-tx.xml]


   
       


接口

public interface UserMapper extends JpaRepository, JpaSpecificationExecutor{
}




SpringBoot整合JPA

依赖包

 
 	org.springframework.boot
 	spring-boot-starter-data-jpa


yml配置文件

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mytest
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver #驱动
  jpa:
    hibernate:
      ddl-auto: update #自动更新
    show-sql: true  #日志中显示sql语句


对象映射

@Entity
@Table(name="user")
@Data
public class User{
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="user_id")
    private Integer userId;
    
    @Column(name="userName")
    private String userName;
    
    
}


dao接口

public interface UserMapper extends JpaRepository,JpaSpecificationExecutor{}