`
woaiyingyu123
  • 浏览: 69712 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

(六)配置及用法之JPA

阅读更多
JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。本文简要介绍一下JPA的配置和用法。简要说明~如果不足之处,请留言,谢谢!
需要的包是什么,我也不大清楚,反正把SSH2整合在一起的包放进去就对啦~~呵呵~
本文有的xml使用了//注解方式,那样是错误的。在这里只是为了方便观看。。
(1)web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     <filter>
	<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
	<filter-class>
	org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
        </filter-class>
     </filter>
     <filter-mapping>
	<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
	<url-pattern>/*</url-pattern>
     </filter-mapping>
</web-app>

(2)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:context="http://www.springframework.org/schema/context" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<context:component-scan base-package="com.xxx" />
  <!--JPA管理工厂配置-->
    <bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="jpa"></property>//name是固定的,jpa是数据源的名字
    </bean>
  <!-- JPA事务管理 -->
   <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>
<!-- 注解方式声明注解,在需要注明事务的类名或方法加上 @Transactional注解 -->
<tx:annotation-driven transaction-manager="txManager" />
//JPA事务管理的做法和Spring的都一样,所以不做过多的概述。
//注意:如果用Spring管理JPA,那么事务配置是必要的(我们最好在业务层Service配置事务),否则JPA对数据库的操作是无效的,因为我们没有显式地提供事务的提交。
</beans>

(3)配置persistence.xml(在src下建立META-INF,放在其中):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">//数据源配置
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.connection.username" value="root"/>
      <property name="hibernate.connection.password" value="123"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa?useUnicode=true&amp;characterEncoding=UTF-8"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

(4)JPA的应用:
public class userDaoImpl{
   @PersistenceContext//自动匹配EntityManager
   protected EntityManager entityManager;
   //getter and setter方法略
   public void save(Object object)
 {
    entityManager.persist(object);
 }
   public void delete(Object object)
   {
    entityManager.remove(object);
   }
   public void update(Object object)
   {
    entityManager.merge(object);
   }
   public List<Object> findByHql(String hql)
   {
    entityManager.createQuery(hql);
   }
   //其他方法省略了~~方法的封装自己慢慢琢磨。这里只是简单介绍一个配置和基础的用法
}

(5)测试:
  //如果只是想简单的进行测试
   EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
   EntityManager entityManager= emf.createEntityManager();
   entityManager.getTransaction().begin();
   entityManager.persist(type);
   entityManager.getTransaction().commit();

  //如果要使用Spring管理的测试的话,老老实实这样做把~
  ApplicationContext act = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml");//applicationContext.xml在WEB-INF目录下
 // ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");applicationContext.xml在src目录下
UserServiceImpl userServiceImpl  =  (UserServiceImpl)act.getBean("userServiceImpl")// 括号内是Bean的ID名
//之后就是你的测试内容了~~ 
 

如果不足之处,请留言,谢谢。
总结:
(1)主要配置过程:web.xml+applicationContext.xml+persistence.xml
分享到:
评论

相关推荐

    优秀实践分享 Spring Data JPA2

    本文是介绍Spring-data-jpa的PPT的学习笔记,整理了Spring Data JPA相关知识配置和实践源码. 本文介绍知识点有: JPA与Spring的相关配置 JPA 方法名常用查询 JPA 使用@Query注解实现JPQL和本地自定义查询 JPA API 条件...

    Spring Data JPA从入门到精通

    'SpringDataJPA从入门到精通'分为12章 内容包括整体认识JPA、JPA基础查询方法、定义查询方法、注解式查询方法、@Entity实例里面常用注解详解、JpaRepository扩展详解、JPA的MVC扩展REST支持、DataSource的配置、乐观...

    JPA标注说明文档(带文档结构整理)

    使用 JPA 时,可以使用批注配置实体的 JPA 行为。批注是一种使用元数据修饰 Java 源代码的简单表达方法,它编译为相应的 Java 类文件,以便在运行时由 JPA 持续性提供程序解释以管理 JPA 行为。

    JPA注解参考文档.txt

    作. Java 企.版 5 (Java EE 5) ...使用 JPA .,可以使用批注配置.体的 JPA 行.。批注是一种使用元.据修. Java 源代.的..表. 方法,它...相.的 Java .文件,以便在.行.由 JPA 持.性提供程序解.以管理 JPA 行.。

    Spring jpa 中文参考文档

    使用 JPA 时,可以使用批注配置实体的 JPA 行为。批注是一种使用元数据修饰 Java 源代码的简单表达方法,它编译为相应的 Java 类文件,以便在运行时由 JPA 持续性提供程序解释以管理 JPA 行为。

    JPA 批注参考.pdf

    使用 JPA 时,可以使用批注配置实体的 JPA 行为。批注是一种使用元数据修饰 Java 源代码的简单表达方法,它编译为相应的 Java 类文件,以便在运行时由 JPA 持续性提供程序解释以管理 JPA 行为。

    Spring Data JPA 原理与实战2021年

    │ 开篇词 勇敢走出舒适区,突破自己的技术瓶颈.mp4 ...│ 20 Spring JPA 中的 Hibernate 加载过程与配置项是怎么回事?.mp4 │ 21 Peritence Context 所表达的核心概念是什么?.mp4 │ 22 Seio

    springmvc4+hibernate4 jpa实现整合(含jar包)

    通过参考和引用传智播客的免费教程,将springmvc4.1.6与hibernate4.3.10提供的JPA实现整合,使用mysql5.6.25,在MyEclipse2014中测试通过。可以作为web开发的基础框架使用。 使用说明: 1.需要安装mysql,并创建名为...

    JPA 批注参考 EJB3.0实体Bean注解详细解析

    在JPA 之前,Java EE 应用...使用JPA 时,可以使用批注配置实体的JPA 行为。批注是一种使用元数据修饰Java 源代码 的简单表达方法,它编译为相应的Java 类文件,以便在运行时由JPA 持续性提供程序解释以 管理JPA 行为。

    第25章_JPA概述.

    29.1主键生成策略 ...29.4 一对一关系的配置和使用 29.5 多对一和一对多关系的配置和使用 29.6 多对多的关系 29.7 把查询的多个值封装成对象 29.8 批量更新和删除 29.9 使用存储过程 29.10 实体生命周期回调方法

    Spring Boot中使用Spring-data-jpa的配置方法详解

    今天小编就为大家分享一篇关于Spring Boot中使用Spring-data-jpa的配置方法详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    第二部分详细讲解了jsf ri、jta、jndi、rmi、jms、javamail、ejb 3的session bean、message driven bean、jpa、jax-ws 2、jaas等java ee知识,这部分知识以jsf+ejb 3+jpa整合开发为重点,通过使用netbeans ide工具...

    Spring数据JPA - 中文参考文档

    使用 JPA 时,可以使用批注配置实体的 JPA 行为。批注是一种使用元数据修饰 Java 源代码的简单表达方法,它编译为相应的 Java 类文件,以便在运行时由 JPA 持续性提供程序解释以管理 JPA 行为。

    spring-boot-jpa-hibernate-demo:Spring Boot中的JPA + Hibernate + MySQL

    具体方法不再赘述项目通过配置DBConfig这个类来配置JPA到Hibernate的结合,读者可自行将DBConfig中有关Hibernate的相关配置提取到application.properties文件中框架SpringBootSpringMVCSpring data JPAMySQL概述一个...

    gradle-openjpa:使用 OpenJPA 代码增强实体类的 gradle-plugin

    使用 OpenJPA 代码增强实体类的 gradle-plugin 用法 buildscript { repositories { mavenCentral() } dependencies { classpath 'at.schmutterer.oss.gradle:gradle-openjpa:0.2.0' } } apply plugin: 'open...

    @ConfigurationProperties注解使用方法(源代码)

    @ConfigurationProperties注解使用方法(源代码) 目录 @ConfigurationProperties注解使用方法 前言 一、Spring配置方式 1.1 第一阶段:xml配置 1.2 第二阶段:注解配置 1.3 第三阶段:Java配置(java config) 二、@...

    毕业设计基于spring boot-jpa-thmleaf的旅游网站设计与实现【源码+lw+部署+讲解】

    使用Spring Data JPA进行数据库操作,Thymeleaf作为模板引擎生成动态页面,部署文档详细介绍了系统的部署步骤和环境配置要求,讲解内容涵盖了系统的功能模块、技术选型理由、设计思路以及使用方法。 适合人群:对...

    spring-boot-data-jpa-example:Spring Boot!学习之Spring-Data-Jpa

    spring.jpa.hibernate.ddl-auto: update 根据实体生成表结构,实体属性变动时,更新表结构,开发时建议使用这种策略 spring.jpa.hibernate.ddl-auto: validate 启动时校验实体和表结构是否一致, 数据结构稳定时采用...

    开发JPA应用

    13.3.4 使用JPA配置文件编辑器修改文件..................... 25 13.3.5 使用反向工程快速生成JPA实体类和DAO ............... 28 13.3.6 调整生成的实体类标注.............................. 42 13.3.7 编写...

    spring-data-jpa-demo:这是一个Spring Data JPA的demo,重点演示大多数常用的使用方法

    Spring Data Jpa常用功能演示配套说明请查看:项目简介本项目采用当前最新版本的2.1.4.RELEASE做基础架构支撑,请参考本项目建议有一定的基础及经验。教程主要针对中文用户,如果您英文良好,建议直接阅读官网帮助...

Global site tag (gtag.js) - Google Analytics