SpringData入门(一)JPA与Hibernate JPA

前言

     本章讲解Hibernate JPA的相关知识,用来作为Spring Data的准备章节

方法

1.概念

Spring Data 是持久层通用解决方案,支持 关系型数据库 Oracle、MySQL、非关系型数据库NoSQL、Map-Reduce 框架、云基础数据服务 、搜索服务

Spring Data 包含多个子项目,其中Spring Data JPA (简化创建 JPA 数据访问层和跨存储的持久层功能)正式我们本次需要重点关注的内容。其底层采用的就是Hibernate JPA技术,这也是为什么先讲Hibernate JPA的主要原因!

2.Spring整合Hibernate基于JPA规范

之前说了那么多,...JPA,那么什么是JPA呢?

JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。Sun引入新的JPA ORM规范出于两个原因:其一,简化现有Java EE和Java SE应用开发工作;其二,Sun希望整合ORM技术,实现天下归一。

说白了目前市场上的ORM框架(Hibernate,Mybatis,EclipseLink,JFinal...)各为其主,刚开始做的时候都有他们自己的一套对象/关系映射技术,JPA的出现为了规约各方,使得大家按照JPA的标准管理对象/关系映射,真正做到天下统一!

其中,Hibernate JPA就是实现了其所谓的JPA规范来简化DAO层开发!

1)准备好Spring+Hibernate整合的Maven项目

这个需要大家提前准备好!不会配置的同学请自行复习。

2)修改pom文件,加入Hibernate JPA相关jar坐标

<!-- 引入Hibernate JPA的相关jar -->
<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-entitymanager</artifactId>
	<version>${hibernate.version}</version>
</dependency>

3)修改applicationContext.xml文件

整体文件详情如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 配置组件扫描,以便于告诉Spring哪里有注解 -->
    <context:component-scan base-package="cn.edu.ccut"></context:component-scan>
    <!-- 引入jdbc配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!-- 配置dataSource数据源 -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!-- 配置Hibernate JPA EntityManagerFactory -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    	<property name="dataSource" ref="dataSource" />
    	<property name="jpaVendorAdapter">
    		<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    			<!-- 配置Hibernate相关的属性 -->
    			<property name="database" value="MYSQL"/>
    			<property name="generateDdl" value="true"/>
    			<property name="showSql" value="true"/>
    		</bean>
    	</property>
    	<property name="packagesToScan" value="cn.edu.ccut.bo"></property>
    </bean>
    <!-- 配置Hibernate JPA 事务管理器 -->
    <bean id="tx" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>
    <!-- 没有Hibernate JPA 配置sessionFactory -->
    <!--<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
        	<props>
        		<prop key="hibernate.show_sql">true</prop>
        		<prop key="hibernate.format_sql">true</prop>
        		<prop key="hibernate.hbm2ddl.auto">update</prop>
        	</props>
        </property>
        <property name="packagesToScan" value="cn.edu.ccut.bo"></property>
    </bean>-->
    <!-- 没有Hibernate JPA 配置hibernateTemplate -->
    <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
       <property name="sessionFactory" ref="sessionFactory"></property>
    </bean> -->
    <!-- 没有Hibernate JPA 配置事务管理器 -->
    <!-- <bean id="tx" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean> -->
    <!-- 配置注解管理事务 -->
    <tx:annotation-driven transaction-manager="tx" proxy-target-class="true"></tx:annotation-driven>
</beans>

3)使用Hibernate JPA后的DAO实现类CRUD

package cn.edu.ccut.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import cn.edu.ccut.bo.Users;
import cn.edu.ccut.dao.UserDAO;

@Repository
public class UserDAOImpl implements UserDAO {
	//没有Hibernate JPA配置
	/*@Autowired
	private HibernateTemplate hibernateTemplate;*/

	/*@Override
	public Users findById(Integer id) {
		return hibernateTemplate.get(Users.class, id);
	}*/
	
	//Hibernate JPA
	@PersistenceContext(name="entityManagerFactory")//该注解表示从工厂中拿出对象
	private EntityManager entityManager;

	@Override
	public void doCreate(Users user) {
		entityManager.persist(user);
	}

	@Override
	public Users findById(Integer id) {
		return entityManager.find(Users.class, id);
	}

	@Override
	public void doRemove(Integer id) {
		Users user = this.findById(id);
		entityManager.remove(user);
	}

	@Override
	public void doUpdate(Users user) {
		entityManager.merge(user);
	}
	
}

以上呢就是我们使用Spring和Hibernate整合时基于JPA规范实现的增删改查,权当是SpringData JPA的知识铺垫!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值