当前位置:编程学习 > JAVA >>

Spring通用basedao

clip_image002

clip_image004

clip_image006

clip_image008

clip_image010clip_image012

clip_image014

2.0
clip_image016

Spring applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<!-- 建立连接池 -->
<bean name="datasouce" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property>
<property name="username" value="sa"></property>
<property name="password" value="sa123456"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="100000"></property>
<property name="defaultAutoCommit" value="false"></property>
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datasouce"></property>
<property name="hibernateProperties">
<props>
<!-- 加载hibernate配置属性dialect(方言)show_sql(显示在数据库执行的sql语句) -->
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 加载hibernate的类关于数据库的映射文件 -->
<property name="mappingResources">
<list>
<value>com/skeyedu/crm/entity/CrmUserandcustomer.hbm.xml</value>
<value>com/skeyedu/crm/entity/User.hbm.xml</value>
<value>com/skeyedu/crm/entity/Customer.hbm.xml</value>
<value>com/skeyedu/crm/entity/CrmCustomerandlinkman.hbm.xml</value>
<value>com/skeyedu/crm/entity/LinkMan.hbm.xml</value>
<value>com/skeyedu/crm/entity/Crmfollow.hbm.xml</value>
</list>
</property>
</bean>
<!--所有dao实现类的继承了HibernateDaoSupport必实现里面的sessionFactory属性-->
<bean name="userdao" class="com.skeyedu.crm.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="customerdao" class="com.skeyedu.crm.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="linkmandao" class="com.skeyedu.crm.dao.impl.LinkManDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- service层调用dao方法,配置dao接口属性 -->
<bean name="userservice" class="com.skeyedu.crm.service.impl.UserServiceImpl">
<property name="UDao" ref="userdao"></property>
</bean>
<bean name="customerservice" class="com.skeyedu.crm.service.impl.CustomerServiceImpl">
<property name="CDao" ref="customerdao"></property>
</bean>
<bean name="linkmanservice" class="com.skeyedu.crm.service.impl.linkManServiceImpl">
<property name="CDao" ref="customerdao"></property>
<property name="LDao" ref="linkmandao"></property>
</bean>
<!--scope="prototype",表示每调用action时从新new个对象,这样排除出现错误在input的时值依然存在,导致actin报错,其他操作都无法使用,因使用的均为同一个action(struts2底层机制是使用时就new个对象)或者这样说scope="prototype" 会在该类型的对象被请求时创建一个新的action对象。如果没有配置scope=prototype则添加的时候不会新建一个action,他任然会保留上次访问的过记录的信息。 -->
<bean name="douser" class="com.skeyedu.crm.struts.DoUserAction" scope="prototype">
<property name="UService" ref="userservice"></property>
</bean>
<bean name="docustomer" class="com.skeyedu.crm.struts.DoCustmoerAction" scope="prototype">
<property name="CService" ref="customerservice"></property>
<property name="UService" ref="userservice"></property>
</bean>
<bean name="dolinkman" class="com.skeyedu.crm.struts.DoLinkmanAction" scope="prototype">
<property name="CService" ref="customerservice"></property>
<property name="LService" ref="linkmanservice"></property>
</bean>
<!-- 声明事务,仅管理service层,在调用dao层方法时异常,捕获并回滚(例:Add*表示以事务环境管理Add开头的方法),对不合以下规则的其他方法不做事务提交或回滚处理,是只读的,一般是指不涉及到数据库更新的方法-->
<bean name="trasactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="myadvice" transaction-manager="trasactionManager">
<tx:attributes>
<tx:method name="Add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Edit*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Del*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Drop*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Regst*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="shareuser*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:metho

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,