springmvc+mybatis整合service层

在整合完dao层后,下一步就是service层的整合。这里顺道说一下,一个框架的搭建,最好是从底层往上开始搭建,至于这样做的好处,就请在实战中慢慢体会吧。

一、编写service

首先需要创建service接口

package com.sky.ssm.service;
import java.util.List;
import com.sky.ssm.po.ItemsCustom;
import com.sky.ssm.po.QueryItemsCustomVo;
/**
 * 商品管理service
 * @author sk
 *
 */
public interface ItemsService {
	//商品查询列表
	public List<ItemsCustom> findItemsList(QueryItemsCustomVo queryItemsCustomVo) throws Exception;
}
然后编写service的实现类

package com.sky.ssm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.sky.ssm.mapper.ItemsCustomMapper;
import com.sky.ssm.po.ItemsCustom;
import com.sky.ssm.po.QueryItemsCustomVo;
import com.sky.ssm.service.ItemsService;
/**
 * 商品管理的实现类
 * @author sk
 *
 */
public class ItemsServiceImpl implements ItemsService {
	//注解mapper
	/** 因为在applicationContext-dao.xml中对mapper进行了扫描
	 * (
			<!-- 配置mapper扫描器 -->
			<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
				<!-- 扫描的包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
				<property name="basePackage" value="com.sky.ssm.mapper"/>
				<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
			</bean>
		)
	 * 因此,mapper已经存在到spring容器中,这里只需要获取即可
	 */
	@Autowired
	private ItemsCustomMapper itemsCustomMapper;

	public List<ItemsCustom> findItemsList(QueryItemsCustomVo queryItemsCustomVo)
			throws Exception {
		return itemsCustomMapper.queryItemsList(queryItemsCustomVo);
	}
}

二、service的配置

applicationContext-service.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	<!-- 商品管理的service -->
	<bean id="itemsService" class="com.sky.ssm.service.impl.ItemsServiceImpl"/>
</beans>
三、事务控制的配置

applicationContext-transaction.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	<!-- 使用spring的声明式事务控制方法 -->
	<!-- 一、声明一个spring的事务管理器 
		对mybatis操作数据库的事务控制,spring使用jdbc的事务控制类
	-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 配置数据源  引用applicationContext-dao.xml中的dataSource
			dataSource在applicationContext-dao.xml中配置了
		-->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 二、配置 通知 即 配置事务的传播特性  -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<!-- REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务;
				 SUPPORTS:支持当前事务,如果当前没有事务,就以非事务的方式执行
			 -->
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<!-- 三、配置aop -->
	<!--  在AOP中有几个概念:
			— 方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象。事务管理是J2EE应用中一个很好的横切关注点例子。方面用Spring的Advisor或拦截器实现。
			— 连接点(Joinpoint):程序执行过程中明确的点,如方法的调用或特定的异常被抛出。
			— 通知(Advice):在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。
			— 切入点(Pointcut):指定一个通知将被引发的一系列连接点的集合。AOP框架必须允许开发者指定切入点,例如,使用正则表达式。
			所以    “<aop:aspect>”实际上是定义横切逻辑,就是在连接点上做什么,
				“<aop:advisor>”则定义了在哪些连接点应用什么 <aop:aspect>。
			Spring这样做的好处就是可以让多个横切逻辑(即<aop:aspect>定义的)多次使用,提供可重用性。
	 -->
	<aop:config>
		<!-- (* com.sky.ssm.service.impl.*.*(..))中几个通配符的含义:
			 第一个 * —— 通配 任意返回值类型
			 第二个 * —— 通配 包com.sky.ssm.service.impl下的任意class
			第三个 * —— 通配 包com.sky.ssm.service.impl下的任意class的任意方法
			第四个 .. —— 通配 方法可以有0个或多个参数 -->
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.sky.ssm.service.impl.*.*(..))"/>
	</aop:config>
</beans>


项目结构目录:





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值