spring3+hibernate3+(dbcp+oracle+拦截器事务配置)整合(一)

1.applicationContext-base.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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
<!-- PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。
在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。
-->
 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:com/init.properties</value>
   </list>
  </property>
  <property name="fileEncoding">
   <value>UTF-8</value>
  </property>
 </bean>
 
<!-- dbcp数据库连接池-->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${classname}"></property>
  <property name="url" value="${url}"></property>
  <property name="username" value="${username}"></property>
  <property name="password" value="${password}"></property>

<property name="maxActive" value="${maxActive}"></property>
  <property name="maxIdle" value="${maxIdle}"></property>
  <property name="maxWait" value="${maxWait}"></property>
  <property name="minIdle" value="${minIdle}"></property>
 </bean>
<!-- 配置sessionFactory-->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="mappingResources">
   <list>
    <value>com/po/Area.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <!--    oracle数据库方言-->
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>
<!-- 配置事务-->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
<!-- 配置事务拦截器-->
 <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="transactionManager"></property>
  <!--  配置事务属性-->
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED</prop>
    <prop key="search*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
<!-- 通过spring中注入的bean的名称进行拦截-->
 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
   </list>
  </property>
  <property name="beanNames">
   <list>
    <value>*Service</value>
   </list>
  </property>
 </bean>
<!-- 配置dao层,注入sessionFactory,dao层直接继承HibernateDaoSupport类即可(HibernateDaoSupport类中已经实现了getHibernateTemplate())-->
 <bean id="areaDao" class="com.dao.AreaDaoImpl">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 
 <bean id="areaService" class="com.service.AreaServiceImpl">
  <property name="areaDao" ref="areaDao"></property>
 </bean>
 
</beans>

2.init.properties文件

classname=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
username=farm
password=farm
maxActive=1000
maxIdle=100
maxWait=200
minIdle=10
# Resources for parameter 'com.init'
# Project farm2
3.web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext-base.xml</param-value>
  </context-param>
  <context-param>
   <param-name>webAppRootKey</param-name>
   <param-value>web.root2</param-value>
  </context-param>
  <context-param>
   <param-name>log4jConfigLocation</param-name>
   <param-value>/WEB-INF/log4j.xml</param-value>
  </context-param>
  <listener>
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 4.AreaDaoImpl.java文件

package com.dao;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.po.Area;

public class AreaDaoImpl extends HibernateDaoSupport implements IAreaDao {

public void addArea(Area area) {
  // TODO Auto-generated method stub
  getHibernateTemplate().save(area);

}

public void deleteArea(int id) {
  // TODO Auto-generated method stub
  String hql ="delete from Area where id=?";
  getHibernateTemplate().bulkUpdate(hql, new Object[]{id});

}

public void updateArea(Area area) {
  // TODO Auto-generated method stub
  getHibernateTemplate().update(area);
 }

public Area findArea(int id) {
  // TODO Auto-generated method stub
  return getHibernateTemplate().load(Area.class, id);
 }

public List<Area> searchAreaList(Area area) {
  // TODO Auto-generated method stub
  String hql ="select * from Area where state=? and alevel=?";
  return getHibernateTemplate().find(hql, new Object[]{area.getState(),area.getAlevel()});
 }

}

5.测试类TestArea.java

package test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.po.Area;
import com.service.IAreaService;

public class TestArea{
 private IAreaService areaService;
 {
  BeanFactory factory = new ClassPathXmlApplicationContext("classpath:applicationContext-base.xml");
  areaService=(IAreaService) factory.getBean("areaService");
 }
 @Test
 public void addArea(){
  Area area = new Area();
  area.setAid("111");
  area.setAlevel(0);
  area.setAname("hy");
  area.setCode("000");
  area.setPid("000");
  area.setState(0);
  areaService.addArea(area);
 }
}

6.log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
  <param name="target" value="System.out" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d %t %-5p (%F:%L) - %m%n" />
  </layout>
  <filter class="org.apache.log4j.varia.LevelRangeFilter"> 
            <param name="levelMin" value="debug" /> 
            <param name="levelMax" value="warn" /> 
            <param name="AcceptOnMatch" value="true" /> 
        </filter>
 </appender>

<root>
  <priority value="info" />
 
  <appender-ref ref="STDOUT" />
 </root>
 <category name="com.opensymphony.xwork2.ognl.OgnlValueStack">
  <priority value="error" />
 </category>
</log4j:configuration>

上一篇:独家探寻阿里安全潘多拉实验室,完美越狱苹果iOS11.2.1


下一篇:JNDI和在tomcat中配置DBCP连接池 元数据的使用 DBUtils框架的使用 多表操作