springmvc+mybatis整合service层

来源:互联网 发布:淄博网站排名优化软件 编辑:程序博客网 时间:2024/05/22 00:08

在整合完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容器中,这里只需要获取即可 */@Autowiredprivate 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/contexthttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/contexthttp://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中的dataSourcedataSource在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>


项目结构目录:





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果爱派密码忘了怎么办 鼠标无法识别的usb设备怎么办 电脑鼠标无法识别usb设备怎么办 win7电脑用户密码忘了怎么办 联想win7旗舰版开不了机怎么办 驱动都被卸载了怎么办 电脑密码忘了怎么办w7旗舰版 笔记本电脑密码忘了怎么办w7 windows一键还原了怎么办 戴尔笔记本电脑键盘没反应怎么办 win10电脑系统盘满了怎么办 win7玩dnf卡死怎么办 cf老是卡住闪退怎么办 w7系统帐户被停用怎么办 海康硬盘录像机密码忘了怎么办 电信合约套餐到期后怎么办 电脑更新系统卡住了怎么办 格力空调显示e1怎么办 ae崩溃了没保存怎么办 电脑下面的状态栏没了怎么办 游戏32位不兼容怎么办 电脑开机dos红屏怎么办 win7进入dos红屏怎么办 手机玩游戏屏幕卡住不动怎么办 魔域英文版换中文版怎么办 党员培养期不足一年怎么办 出生证明日期错了怎么办 毕业生登记表写错了怎么办 高等学校毕业生登记表写错了怎么办 眼睛里迷了东西怎么办 眼睛迷了怎么办小绝招 isf货物离港申报怎么办 非农户口没住房怎么办 在工厂上班很累怎么办 退货运费太贵了怎么办 悬肘写字手抖怎么办 护士成绩合格证明丢了怎么办 找工作真难找好烦怎么办 大夫说身体不适合怀孕有了怎么办 不知道要做什么工作怎么办 红米note4x闪退怎么办