Spring3.0 + JPA(Hibernate3.6)

<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://192.168.1.96:3306/warehouse?useUnicode=true&amp;characterEncoding=UTF-8" />
    <property name="username" value="root" />
    <property name="password" value="lx" />
     <!-- 连接池启动时的初始值 -->
         <property name="initialSize" value="1"/>
         <!-- 连接池的最大值 -->
         <property name="maxActive" value="50"/>
         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
         <property name="maxIdle" value="10"/>
         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
         <property name="minIdle" value="1"/>
   
  </bean>
首先是jar准备
Spring3.0 + JPA(Hibernate3.6) - Ben - Ben
结构预览
Spring3.0 + JPA(Hibernate3.6) - Ben - Ben
jdbc:properties
driverClassName =org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:8899/ssh2?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
initialSize =1
maxActive=10
maxIdle=2
minIdle=1
----------------------------------------------------------------------------------------------------------------
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="ssh2"> <!-- 实体bean集合名字 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- JPA驱动提供者 -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
----------------------------------------------------------------------------------------------------------------
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<context:component-scan base-package="ben.s3h3.demo" />
<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}" />
<!-- 最小空闲值,当空闲的连接数少于最小值时,连接池就会去申请一些连接,以免高峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}" />

</bean>
<!-- 类工厂由spring管理 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" /><!-- 注入数据源bean到实体管理工厂bean -->
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver"><!-- 运行时植入 -->
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- 事务由spring管理 -->
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" /><!-- 注入实体管理工厂bean到事务管理bean -->
</bean>
  <tx:annotation-driven transaction-manager="txManager" /><!-- 事务声明方式是注解 -->
</beans>
----------------------------------------------------------------------------------------------------------------
IDType.java
package ben.s3h3.demo.bean.id;

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

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

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

}
----------------------------------------------------------------------------------------------------------------
IDService.java
package ben.s3h3.demo.service.id;

import ben.s3h3.demo.bean.id.IDType;

public interface IDService
{
void save(IDType type);
}
----------------------------------------------------------------------------------------------------------------
IDServiceBean.java
package ben.s3h3.demo.service.id.impl;

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

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ben.s3h3.demo.bean.id.IDType;
import ben.s3h3.demo.service.id.IDService;

@Service //标注为Spring的Servicebean,供自动扫描机制加载该bean,就不用再beans.xml中声明了
@Transactional
public class IDServiceBean implements IDService {

@PersistenceContext
EntityManager em;

public void save(IDType type) {
em.persist(type);
}
}
----------------------------------------------------------------------------------------------------------------
没用JUnit 写了个main方法
package main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import ben.s3h3.demo.bean.id.IDType;
import ben.s3h3.demo.service.id.IDService;

public class DemoRun
{
public static void main(String[] args)
{
System.out.println("测试Spring3.05+JPA(Hibernate3.6)环境");
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
IDService idService = (IDService) cxt
.getBean("IDServiceBean");
idService.save(new IDType());
System.out.println("测试完毕 ---> 查看数据库去 :-)");
}
}

你可能感兴趣的:(hibernate3)