標籤:hibernate spring 單元測試 事務 版本jar包
巴巴運動網-整合hibernate4+spring4(2)
1、項目圖解
2、首先我們引入相應的jar包
這裡用的是oracle 11g,所以我們使用的資料庫連接jar包是ojdbc6,
實際上ojdbc5和6的差別就是支援的資料版本的問題,只要你安裝了相應的資料庫,對應的版本裡面就有相應的資料庫jar包,不行百度絕壁有!!!
3、我們配置一下資料庫中相應的實體物件
ProductType.java
/** * 功能:這是產品類別的 * 檔案:ProductType.java * 時間:2015年5月12日10:16:21 * cutter_point */package com.cutter_point.bean.product; publicclassProductType{ private Integer typeid; public Integer getTypeid() { returntypeid; } publicvoid setTypeid(Integertypeid) { this.typeid = typeid; }}
Product.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cutter_point.bean.product"> <class name="ProductType" table="ProductType"> <id name="typeid"column="typeid"> <generator class="sequence" /> </id> </class></hibernate-mapping>
4、我們配置一下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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- 掃描帶有spring特殊機制的類,這是把這些包下面所有的類都添加到spring中進行管理 --> <context:component-scan base-package="com.cutter_point" /> <!-- 屬性遍曆器 --> <!-- <context:property-placeholderlocation="classpath:jdbc.properties" /> --> <!-- 串連資料庫屬性配置,destroy-method="close"就是說在這個bean被摧毀的情況下可以調用這個bean預設的close方法--> <bean id="myDataSource" class="org.apache.commons.dbcp2.BasicDataSource"destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url"value="jdbc:oracle:thin:@localhost:1522:orcl"/> <property name="username"value="帳號"/> <property name="password"value="密碼"/> <!-- 串連池啟動時的初始值 --> <property name="initialSize" value="1"/> <!-- 串連池的最大值 dbcp2裡面似乎沒有--> <!-- <property name="maxActive"value="500"/> --> <!-- 最大空閑值.當經過一個高峰時間後,串連池可以慢慢將已經用不到的串連慢慢釋放一部分,一直減少到maxIdle為止 --> <property name="maxIdle" value="2"/> <!-- 最小空閑值.當閒置串連數少於閥值時,串連池就會預申請去一些串連,以免洪峰來時來不及申請 --> <property name="minIdle" value="1"/> </bean> <!-- hibernate二級緩衝的配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- configuration elided for brevity --> <property name="dataSource" ref="myDataSource" /> <property name="mappingResources"> <list> <!-- 對應檔 --> <value>com/cutter_point/bean/product/Product.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <!-- 用來配置hibernate的屬性配置 --> <value> org.hibernate.dialect.OracleDialect hibernate.hbm2ddl.auto=update <!--其他取值 create、create-drop、update、validate none--> hibernate.show_sql=true hibernate.format_sql=true <!-- 開啟二級緩衝功能 --> hibernate.cache.use_second_level_cache= true hibernate.cache.use_query_cache= false hibernate.cache.region.factory_class= org.hibernate.cache.ehcache.EhCacheRegionFactory <!-- hibernate3的二級緩衝配置 --> <!-- <propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>--> </value> </property> </bean> <!-- 交易管理員,吧上面配置的bean注入到這個裡面 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 我們採用註解的方式來使用這個事務,首先我們開啟事務 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
5、接下來我們測試一下hibernate+spring是否配置成功
/** * 功能:這是產品類別的單元測試 * 檔案:ProductTest.java * 時間:2015年5月12日10:27:24 * cutter_point */package junit.test; import javax.sql.DataSource; import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.BeforeClass;import org.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext; importcom.cutter_point.bean.product.ProductType; public class ProductTest{ @BeforeClass publicstatic void setUpBeforeClass() throws Exception { } @Test publicvoid test() { ProductTypept = new ProductType(); //new一個對象 pt.setTypeid(78); //設定id號碼 Configurationcfg = new Configuration(); //得到Configuration SessionFactorysf =cfg.configure("/config/hibernate/hibernate.cfg.xml").buildSessionFactory(); //取得session工廠 Sessionsession = sf.openSession(); session.beginTransaction(); session.save(pt); session.getTransaction().commit(); session.close(); sf.close(); } @Test publicvoid testSpring() { //測試spring是否可以運作 ApplicationContextcxt = new ClassPathXmlApplicationContext("config/spring/beans.xml"); DataSourcedatasource = (DataSource)cxt.getBean("myDataSource"); //取出一個對象 System.out.println(datasource); //判斷是不是為空白, } }
6、總結
這個spring裡面配置了資料來源是用的org.apache.commons.dbcp2.BasicDataSource
這個jar包是,當然你們可以用其他的,我這裡只是一個舉例,如果你報一個神秘version5.1的錯誤的話,請換一個資料來源jar包,比如commons-dbcp.jar
這個裡面就把org.apache.commons.dbcp2.BasicDataSource改為org.apache.commons.dbcp.BasicDataSource
這個其實就是版本問題,其他的地方應該是不用改了!!!!
【j2ee spring】27、巴巴運動網-整合hibernate4+spring4(2)