spring3.0 MVC筆記3--從spring2.0轉向spring3.0-集成Hibernate3及聲明式事務

spring3.0 MVC筆記3

12、集成Hibernate3及聲明式事務

hibernate3提供contextual sessions,可直接在dao中使用

sessionFactory,不推薦以前的HibernateTemplate

a、聲明dataSource

 <bean id="dataSource"

class="org.logicalcobwebs.proxool.ProxoolDataSource">
     <property name="driver">
        

<value>net.sourceforge.jtds.jdbc.Driver</value>
     </property>
     <property name="driverUrl">
        

<value>jdbc:jtds:sqlserver://t18:1433;DatabaseName=tdrcc

ms;autoReconnect=true;</value>   
     </property>
     <property name="user" value="zbtbwusr" />
     <property name="password" value="aaa" />
     <property name="alias" value="tdrcpool" />
     <property name="prototypeCount" value="5" />
     <property name="maximumConnectionCount"

value="100" />
     <property name="minimumConnectionCount"

value="5" />
     <property name="simultaneousBuildThrottle"

value="50" />
  </bean>

b、聲明sessionFactory

 <bean id="sessionFactory"
  

class="org.springframework.orm.hibernate3.LocalSessionFa

ctoryBean">
  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
  <property name="configLocation">
   

<value>classpath:hibernate.cfg.xml</value>
  </property>
 </bean>

c、編寫dao

@Repository
public class UserDaoImpl implements IUserDao {
 @Autowired
 public UserDaoImpl(SessionFactory sf){
  this.sessionFactory = sf;
 }
 
 private Session getCurrentSession(){
  return sessionFactory.getCurrentSession

();
 }
 public Object addObject(Object o){
  getCurrentSession().save(o);
  return o;
 }
 public List findTopXByHQL(final String hql,

final short x)throws HibernateException,SQLException{
        Query query = getCurrentSession().createQuery

(hql);
        query.setFirstResult(0);
        query.setMaxResults(x);
        List list = query.list();
        return list;
     }
...
}

d、編寫service
@Service("helloService")
public class HelloServiceImpl implements HelloService{
 @Autowired
 public HelloServiceImpl(IUserDao dao){
  this.dao = dao;
 }
 public List<Member> listAll(){
  return dao.findListByHQL("from Member

m");
 }
...
}

e、配置事務
---
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"

http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-

3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-

context-3.0.xsd">

---
 <bean id="txManager"

class="org.springframework.orm.hibernate3.HibernateTrans

actionManager">
     <property name="sessionFactory"

ref="sessionFactory"/>
   </bean>
  <tx:advice id="txAdvice" transaction-

manager="txManager">
           <tx:attributes>
                               <tx:method name="add*"

propagation="REQUIRED" />
                               <tx:method name="delete*"

propagation="REQUIRED" />
                               <tx:method name="update*"

propagation="REQUIRED" />
                               <tx:method name="list*"

propagation="SUPPORTS" read-only="true" />
                               <tx:method name="get*"

propagation="SUPPORTS" read-only="true" />
                               <tx:method name="*" read

-only="true" />
            </tx:attributes>
 </tx:advice>
 <aop:config>
  <aop:advisor
   pointcut="execution(*

*..HelloService.*(..))"
   advice-ref="txAdvice"/>
 </aop:config>

13、spring3.0應用應包含的jar

1、基本:
org.springframework.context-3.1.2.RELEASE.jar
org.springframework.core-3.1.2.RELEASE.jar
org.springframework.expression-3.1.2.RELEASE.jar
org.springframework.asm-3.1.2.RELEASE.jar
org.springframework.beans-3.1.2.RELEASE.jar
2、依賴:
log4j-1.2.15.jar
commons-collections-3.1.jar
commons-logging-1.1.1.jar
3、spring單元測試:
org.springframework.test-3.1.2.RELEASE.jar
4、spring mvc
org.springframework.web.servlet-3.1.2.RELEASE.jar
org.springframework.web-3.1.2.RELEASE.jar
5、jstl
standard.jar
jstl.jar
6、控制器註解注入JSR-330支持,用於@Inject註解
weld-osgi-bundle.jar
7、文件處理支持FileUtils
commons-io-2.0.1.jar
8、文件上傳
commons-fileupload-1.2.2.jar
9、POJO註解驗證JSR-303支持,用於@Valid、@Size等註解的編


validation-api-1.0.0.GA.jar
10、註解驗證默認實現
hibernate-validator-4.0.2.GA.jar
11、數據庫驅動及連接池
jtds-1.2.jar
proxool-0.9.1.jar
proxool-cglib.jar
cglib-2.2.2.jar
12、集成Hibernate3及聲明式事務
hibernate3.jar
org.springframework.orm-3.1.2.RELEASE.jar
jta.jar
org.springframework.jdbc-3.1.2.RELEASE.jar
dom4j-1.6.1.jar
asm-3.3.jar
org.springframework.transaction-3.1.2.RELEASE.jar
antlr-2.7.2.jar
aopalliance.jar
org.springframework.aop-3.1.2.RELEASE.jar
aspectjweaver.jar

發佈了34 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章