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