1.0 jdbc database operation Review
The introduction of 2.0 orm ideas
Implementation framework orm thought there mybatis (half-orm) and hibernate, mainly to build relationships with entities like tables, build relationships with the entity class attribute table fields
3.0 jpa specification introduced
Since there have been many like hibernate orm framework, a bit confusing, so the sun's proposed jpa specification, the internal interfaces and abstract classes
Basic Operations 4.0 jpa
4.1 engineering structures
1. Create a maven project into coordinates
2. jpa need to configure the core configuration file
* Location: next to the configuration called META-INF under the classpath folder
* name: the persistence.xml
3. Write the client entity class
4. Configuration mapping relationship table and entity class, and the class attribute field in a table
4.2 Procedure
1. load the configuration file to create the entity manager factory
2. The entity manager factory that creates the entity manager
3. Create a transaction object, open affairs
4. CRUD operations
5. commit the transaction
6. Release Resource
4.3 Basic Operations
@Test public void testSave(){ // 加载配置文件创建实体管理器工厂 EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myJpa"); // 通过实体管理器工厂获取实体管理器 EntityManager entityManager = entityManagerFactory.createEntityManager(); // 获取事务,开启事务 EntityTransaction tx = entityManager.getTransaction(); tx.begin(); // 完成jpa增删改查操作 Customer customer = new Customer(); customer.setCustName("传智播客"); customer.setCustIndustry("教育"); // 实体管理器保存对象 entityManager.persist(customer); // 事务提交 tx.commit(); // 释放资源 entityManager.close(); entityManagerFactory.close(); }
/** * 根据id查询 */ @Test public void testFind(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Customer customer = entityManager.find(Customer.class, 1L); System.out.println(customer); tx.commit(); entityManager.close(); }
/** * 根据id查询(延迟加载) */ @Test public void testGetReference(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Customer customer = entityManager.getReference(Customer.class,1L); System.out.println(customer); tx.commit(); entityManager.close(); }
/** * 删除客户的案例 */ @Test public void testDelete(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); // Customer customer = new Customer(); // customer.setCustId(2L); Customer customer = entityManager.find(Customer.class, 1L); entityManager.remove(customer); tx.commit(); entityManager.close(); }
/** * 更新客户的案例 */ @Test public void testUpdate(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Customer customer = entityManager.find(Customer.class, 2L); customer.setCustIndustry("it教育"); entityManager.merge(customer); tx.commit(); entityManager.close(); }
4.4 jpql操作
/** * 查询全部 */ @Test public void findAll(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Query query = entityManager.createQuery("from Customer "); List list = query.getResultList(); list.stream().forEach(customer-> System.out.println(customer)); tx.commit(); entityManager.close(); }
/** * 结果排序 */ @Test public void findOrder(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Query query = entityManager.createQuery("from Customer order by custId desc"); List list = query.getResultList(); list.stream().forEach(customer-> System.out.println(customer)); tx.commit(); entityManager.close(); }
/** * 结果统计 */ @Test public void findTotal(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Query query = entityManager.createQuery("select count(custId) from Customer"); Object result = query.getSingleResult(); System.out.println(result); tx.commit(); entityManager.close(); }
/** * 分页查询 */ @Test public void findPaged(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Query query = entityManager.createQuery("from Customer"); query.setFirstResult(0); query.setMaxResults(2); List list = query.getResultList(); list.stream().forEach(customer-> System.out.println(customer)); tx.commit(); entityManager.close(); }
/** * 条件查询 */ @Test public void findCondition(){ EntityManager entityManager = JpaUtils.getEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Query query = entityManager.createQuery("from Customer where custName like ?"); query.setParameter(1,"传智%"); List list = query.getResultList(); list.stream().forEach(customer-> System.out.println(customer)); tx.commit(); entityManager.close(); }
5.0 springdata-jpa
Spring Data JPA 是 Spring 基于 ORM 框架、JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和操作。
Spring Data JPA 让我们解脱了DAO层的操作,基本上所有CRUD都可以依赖于它来实现,在实际的工作工程中,推荐使用Spring Data JPA + ORM(如:hibernate)
完成操作,这样在切换不同的ORM框架时提供了极大的方便,同时也使数据库层操作更加简单,方便解耦。使用了SpringDataJpa,我们的dao层中只需要写接口,
就自动具有了增删改查、分页查询等方法
5.1 基本增删改查
i.搭建环境
创建工程导入坐标
配置spring的配置文件(配置spring Data jpa的整合)
编写实体类(Customer),使用jpa注解配置映射关系
ii.编写一个符合springDataJpa的dao层接口
* 只需要编写dao层接口,不需要编写dao层接口的实现类
* dao层接口规范
1.需要继承两个接口(JpaRepository,JpaSpecificationExecutor)
2.需要提供响应的泛型
findOne(id) :根据id查询
save(customer):保存或者更新(依据:传递的实体类对象中,是否包含id属性)
delete(id) :根据id删除
findAll() : 查询全部
5.2 原理分析
1.通过JdkDynamicAopProxy的invoke方法创建了一个动态代理对象
2.SimpleJpaRepository当中封装了JPA的操作(借助JPA的api完成数据库的CRUD)
3.通过hibernate完成数据库操作(封装了jdbc)
5.3 jpql方法
使用Spring Data JPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来说,我们还需要灵活的构造查询条件,这时就可以使用@Query注解,结合JPQL的语句方式完成查询
5.4 sql查询
1.特有的查询:需要在dao接口上配置方法
2.在新添加的方法上,使用注解的形式配置sql查询语句
3.注解 : @Query
value :jpql语句 | sql语句
nativeQuery :false(使用jpql查询) | true(使用本地查询:sql查询)
是否使用本地查询
5.5方法名称规则查询