1.spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2011 Alibaba Group.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-->
<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:util="http://www.springframework.org/schema/util"
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-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<!-- 自动加载SERVICE DAO ACTION -->
<context:component-scan base-package="xxx.dao.impl" />
<context:component-scan base-package="xxx.provider" />
<!-- 加载properties配置文件
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
-->
<!-- 配置mysql数据源 -->
<!--
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx:3306/o2o?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="xxx" />
<property name="password" value = "xxx" />
</bean>
-->
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://xxx.xxx.xxx.xxx:3306/o2o?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="xxx"/>
<property name="password" value="xxx"/>
<property name="maxPoolSize" value="50"/>
<property name="minPoolSize" value="10"/>
<property name="initialPoolSize" value="10"/>
<property name="maxIdleTime" value="15"/>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 依赖注入数据源,注入正是上面定义的dataSource -->
<property name="dataSource" ref="dataSource" />
<!-- mappingResouces属性用来列出全部映射文件 -->
<property name="packagesToScan">
<list>
<value>xxx.entity</value>
</list>
</property>
<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 采用@Transaction注解的方式使用事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="hibernateTempate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--配置事务的传播特性 -->
<!--
<tx:advice id="txAdvice" transaction-manager="transactionManager">
用于配置详细的事务语义
<tx:attributes>
对增、删、改方法进行事务支持
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
对查找方法进行只读事务
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
对其它方法进行只读事务
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
-->
<!--那些类的哪些方法参与事务 -->
<!--
<aop:config>
<aop:advisor
pointcut="execution(* cn.com.company.service..*Service.*(..))"
advice-ref="txAdvice" />
<aop:advisor
pointcut="execution(* cn.com.company.service..*ServiceImpl.*(..))"
advice-ref="txAdvice" />
</aop:config>
-->
<!--
<bean id="xxxxDaoImpl" class="xxx.impl.xxxDaoImpl">
<property name="hibernateTempate" ref="hibernateTempate"/>
</bean>
<bean id="xxxxServiceImpl" class="xxx.impl.xxxServiceImpl">
<property name="xxxxDAO" ref="xxxxDaoImpl"/>
</bean>
-->
</beans>
2.Dao实现类
@Repository("xxxxDAO")
public class XXXDaoImpl extends AbstractBaseDao<XXXTableEntity> implements XXXDaoIF {
}
3.service实现类
@Service("xxxServiceImpl")
@Transactional(readOnly=true)
public class xxxxServiceImpl implements xxxServiceIF {
@Autowired
@Qualifier("xxxDAO")
private xxxDaoIF xxxDAO;
public xxxDaoIF getxxxDAO() {
return xxxDAO;
}
public void setxxxDAO(xxxDaoIF xxxDAO) {
this.xxxDAO = xxxDAO;
}
public xxxDto findCustomById(String id) {
return null;
}
@Transactional(propagation=Propagation.REQUIRED)
public void save(xxxTableEntity entity) {
xxxDAO.insert(entity);
}
public String findUserNameById(String id) {
// TODO Auto-generated method stub
return null;
}
public void save(CustomerDto entity) {
// TODO Auto-generated method stub
}
}