1、先集成JPA+Spring
首先导入HibernateJPA的实现包,如图
2、再导入spring的jar包如下图,另外要加入数据库驱动ojdbc14.jar,这里我用的是Oracle数据库
3、创建spring配置文件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-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.jason"/>-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jason"/>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
4、连接数据库有三种方式:1.不使用数据源;2、JDBC数据源;3.dbcp连接池;4.c3p0连接池。(个人喜欢使用c3p0 哈哈)
第一种不使用数据源:在src根目录下创建META-INF文件夹并创建persistence.xml配置文件,JPA规范要求persistence.xml必须放在类路径的META-INF目录下,在应用部署的时候会被编译到WEB-INF的class目录下。
<?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="jason" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
<property name="hibernate.connection.username" value="jason"/>
<property name="hibernate.connection.password" value="jason"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
5、创建实体类Buyer.java
package com.jason.bean.user;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* 用户实体
* @Type_name: Buyer
* @author: JASON jason8812@yahoo.cn
*/
@Entity
public class Buyer implements Serializable{
private static final long serialVersionUID = -7273954519684644480L;
private String username;
private String password;
private String email;
public Buyer(){}
public Buyer(String username){
this.username = username;
}
public Buyer(String username, String password) {
this.username = username;
this.password = password;
}
public Buyer(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
@Id
@Column(length=18)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(length=32,nullable=false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(length=45,nullable=false)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Buyer other = (Buyer) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}
6、创建junit测试类BuyerTest.java
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BuyerTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void save(){
new ClassPathXmlApplicationContext("beans.xml");
}
}
右键save方法JUnit测试,如果数据库表里生成了buyer表就说明集成成功了!
第二种JDBC数据源,在beans.xml配置,也可以配置到jdbc.properties文件中
<bean id="JDBCdatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="skyadmin"></property>
<!-- 初始连接数量 -->
<property name="initialSize" value="50"></property>
<!-- 最大连接数量 -->
<property name="maxActive" value="80"></property>
</bean>
第三种dbcp连接池,使用数据源有助于应用的性能。
首先加入支持的dbcp连接池jar包commons-dbcp.jar和commons-pool.jar
在src根目录里创建jdbc.properties文件,内容如下:
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:ORCL
username=scott
password=tiger
initialSize=1
maxActive=100
maxIdle=8
minIdle=1
然后在beans.xml里注释掉
<!--<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jason"/>
</bean>-->
添加dbcp连接池配置
<!-- 2、使用dbcp数据源-->
<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>
<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}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<!--该类对JPA做一些额外工作-->
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
另外persistence.xml要修改如下:
<?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">
<!-- 2、dbcp数据源、c3p0数据源-->
<persistence-unit name="jason" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<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>
OK,运行JUnit测试SUCCESS!
第四种、c3p0连接池
需要加入支持的c3p0jar包c3p0-0.9.1.2.jar
在beans.xml里需要修改dbcp连接池配置如下
<!-- 3、c3p0数据源-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1"/>
<!-- 连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
jdbc.properties需改为
driverClass=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@localhost:1521:ORCL
user=scott
password=tiger
OKay!运行JUnit,SUCCESS!