巴巴运动网学习笔记二之spring3.1+jpa的环境配置

a.导入spring所用jar包

b.新建src/beans.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-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<aop:aspectj-autoproxy/>
	<context:component-scan base-package="com"/>	
    <!--  使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现,
    需要在tomcat作一些特殊配置.具体参考手册
    注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
   -->
    
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/eshop?useUnicode=true&characterEncoding=UTF-8"/>
		<property name="username" value="root"/>
		<property name="password" value="123"/>
		<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3  -->
		
	</bean>

	<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		 <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
		<property name="loadTimeWeaver">
	          <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
	    </property>
	</bean> 
	
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf"/>
   </bean>
   
   <tx:annotation-driven transaction-manager="transactionManager"/>
   
</beans>


 

<?xml version="1.0"?>
<persistence 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" version="1.0">
   <persistence-unit name="zsm" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
         <!--  
         <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/eshop?useUnicode=true&characterEncoding=UTF-8"/>
         -->
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.show_sql" value="true"/>
	     <property name="hibernate.format_sql" value="false"/>
	     
      </properties>
   </persistence-unit>
</persistence>


 

package com.bean.product;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class ProductType {
	
	private int id;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	

}


 

package com.test;

import javax.sql.DataSource;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProdutctTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test
	public void test() {
		ApplicationContext acx = new ClassPathXmlApplicationContext("beans.xml");
		DataSource dataSource = (DataSource)acx.getBean("dataSource");
		System.out.println(dataSource);
	}

}


这里,列一些使用JPA常见的问题,算是自己的笔记,希望可以帮到你的小忙。
  1、jpa包的冲突
  2、事务不正常执行
  3、Cannot proxy target class because CGLIB2 is not available
  1、jpa包的冲突
  运行 jpa 程序,却出现:
  Caused by: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
  这个是由于jar包的冲突了,    因为使用的是新版本的 Hibernate(这里我使用的是 Hibernate 3.6),跟  javaee.jar 里面的jpa接口冲突了
  解决办法
  使用 tomcat 中带的 servlet-api.jar 替换 javaee.jar
  去掉 Java EE 5 Libraries
  新建 user libraries 包含 jsf-api.jar jsf-impl.jar jstl-1.2.jar servlet-api.jar
  就是将 MyEclipse 里面的 Java EE 5 Libraries  中的 javaee.jar 换成 servlet-api.jar
  然后在工程里加入刚刚的user libraries,OK

 

org.gjt.mm.mysql.Driver是早期的驱动名称,后来就改名为com.mysql.jdbc.Driver,现在一般都推荐使用com.mysql.jdbc.Driver。在最新版本的mysql jdbc驱动中,为了保持对老版本的兼容,仍然保留了org.gjt.mm.mysql.Driver,但是实际上org.gjt.mm.mysql.Driver中调用了com.mysql.jdbc.Driver,因此现在这两个驱动没有什么区别。



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值