1、導(dǎo)入jar包
spring-context
spring-beans
spring-core
spring-jdbc
spring-web
mybatis-spring
druid
struts2-spring-plugin
2属百、service的實現(xiàn)類,在類上加注解 @Service
屬性上加@Resource
如下所示:
@Service
public class RoleServiceImpl implements RoleService {
@Resource
private RoleMapper roleMapper;
}
3.控制器類加@Controller注解 ,注入的service加@Resource注解
如下所示:
@Controller
public class UserAction extends ActionSupport {
@Resource
private UserService userService;
@Resource
private RoleService roleService;
}
4.配置applicationContext.xml文件
創(chuàng)建 applicationContext.xml,放在web-info目錄下.
在applicationContext文件中配置數(shù)據(jù)源, 在mybatis.xml中把連接數(shù)據(jù)庫的environments刪除
在applicationContext.xml文件中配置會話工廠 ,配置掃描打了@Service和@Controller的包.
配置MapperScannerConfigurer生成mapper的bean
5.在web.xml中配置監(jiān)聽器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
1.使用聲明式事務(wù)導(dǎo)jar包
spring-aop
spring-tx
2.配置applicationContext文件
在文件中定義要切入的事務(wù)
示例如下:
<bean name="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="tm">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pointcut" expression="execution( com.szxs.service...(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>