五)service

  • (1)service依赖dao
  • (2)编写测试
  • (3)如何读取另一个工程的spring的配置

classpath: 加载当前maven工程的resources目录下的配置文件
classpath*: 加载当前maven工程及其依赖工程的resources目录下的配置文件
applicationContext-*.xml: 读取所有符合规则的文件

TestCompanyService

/*** Created by 李柏霖* 2020/10/22 20:33*/package com.lbl.service;import com.lbl.domain.Company;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;//2:添加spring的单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")  //3:创建 spring/applicationContext-tx.xml
public class TestCompanyService {@AutowiredICompanyService  companyService;@Testpublic void test01(){//1:编写了业务逻辑的测试//等号 左边是接口 右边是实现类//ICompanyService  service = new CompanyServiceImpl();List<Company> list = companyService.findAll();System.out.println(list);}
}

spring/applicationContext-tx.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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--扫描Service实现类--><context:component-scan base-package="com.lbl.service"/><!--Spring声明式事务(底层就是AOP): 三步曲--><!--1.配置事务管理器:管理事务:DataSource.Connection.commit() rollback()方法  --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--注入数据源--><property name="dataSource" ref="dataSource"/></bean><!--2.配置事务通知--><tx:advice id="txAdvice" transaction-manager="transactionManager" ><!--配置事务细节特征--><tx:attributes><!--查询方法,使用默认的隔离级别 及 SUPPORTS传播行为--><tx:method name="find*" isolation="DEFAULT" propagation="SUPPORTS"/><tx:method name="query*" isolation="DEFAULT" propagation="SUPPORTS"/><tx:method name="select*" isolation="DEFAULT" propagation="SUPPORTS"/><tx:method name="get*" isolation="DEFAULT" propagation="SUPPORTS"/><!--增删改方法,使用默认的隔离级别 及 REQUIRED传播行为--><tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/></tx:attributes></tx:advice><!--3.配置事务切面: 切面=通知+切入点--><aop:config><!--配置切入点--><aop:pointcut id="pt" expression="execution(* com.lbl.service.*.impl.*.*(..))"/><!--切面=通知+切入点--><aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/></aop:config></beans>

如果出现utf-8异常

去父工程的pom.xml

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target>

如果出现applicationContext异常

@ContextConfiguration("classpath*:spring/applicationContext-*.xml")

classpath: 加载当前maven工程的resources目录下的配置文件
classpath*: 加载当前maven工程及其依赖工程的resources目录下的配置文
applicationContext-*.xml: 读取所有符合规则的文件

如果出现Company找不到类异常

<packaging>pom</packaging>
export_parent<packaging>jar</packaging>
export_domain
export_dao
export_system_service<packaging>war</packaging>
export_web_manager

运行结果 :

saas-export项目service层相关推荐

  1. 【SaaS - Export项目】18 - 用户登录、注销登录

    文章目录 1. export_system_service子工程编写测试类测试登录功能 2. export_system_service子工程的service接口和实现类 3. export_dao子 ...

  2. 在 Java Web 项目中,Service 层和 Dao 层真的有必要每个类都加上接口吗

    作者 l 会点代码的大叔(CodeDaShu) 很多程序员在刚开始工作的时候,接触到的项目都是这样做的:项目的代码被分成 Controller.Service.Dao 层,一个接口对应一个实现类,然后 ...

  3. ##管家婆项目(service层)

    管家婆项目(service层) package cn.kgc.gjp.service;import cn.kgc.gjp.dao.ZhangWuDao; import cn.kgc.gjp.domai ...

  4. dao层和service层和control_maven分模块构建SSM普通web项目:service层代码编写

    接着编写service层的代码. 1.service接口及其实现类 新建service包用于放service接口,还有,新建service.impl包放接口的实现类.如下图,BookService和其 ...

  5. 039医疗项目-模块四:采购单模块—采购单的创建-----------Dao层,service层,Acion层...

    我们根据数据库里面的表格:生成对应的Mapper接口文件和...Mapperxml文件: YYCGD:采购单模板表. YYCGD2014:采购单动态生成表,由存储过程生成. YYCGDMX:采购单明细 ...

  6. 对SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层等层的理解

    SSM 是 Spring + SpringMVC + Mybatis集成的框架. MVC即model view controller.(模型,视图,控制器) 一.entity层(model层,doma ...

  7. Service 层和 Dao 层有必要为每个类都加上接口吗?

    以下文章来源方志朋的博客,回复"666"获面试宝典  作者:架构思维 toutiao.com/i6882356844245975563 前几天刷头条又刷到了「Service层和Da ...

  8. CTO说:Service层的接口是不是多此一举

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者:架构思维 来源:toutiao.com/i68823 ...

  9. 讨论:Service层需要接口吗?

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 链接:toutiao.com/i6882356844245 ...

最新文章

  1. 利用exchangelib快速上手使用python发邮件
  2. html5指南针源码,《绝秘奉献》——最新超短线指南针!源码已放!(贴图 原码)...
  3. UVa11022 String Factoring(kmp+dp)
  4. 关于企业的信息化建设
  5. MySQL常见备份与恢复方案
  6. 顺序栈实现括号匹配的检验(C语言实现)【栈】
  7. linux内核中的循环缓冲区
  8. 树莓派静态IP配置方法
  9. python中可以使用变量来引用函数吗_如何在python语言中使用函数变量并调用函数...
  10. java项目启动后运行方法_spring boot在启动项目之后执行的实现方法
  11. ASP中巧用Response属性
  12. ubuntu18安装vnpyv1.9.2之二
  13. win11任务栏怎么设置大小
  14. 云栖科技评论世界杯特刊:俄罗斯世界杯,新技术再一次升级内容产业
  15. 新概念英语第一册1-144课(转)
  16. 人工智能在肿瘤成像中的临床挑战和应用
  17. 如何将YouTube视频插入PowerPoint演示文稿
  18. 关于结构光问题的总结
  19. Cordova 拨打电话_Cordova 项目开启拨打电话
  20. Unity隐藏目录和隐藏文件

热门文章

  1. Oracle一备份内存就占满卡死,rman备份占用内存问题
  2. 如何获取AK/SK?(IAM用户)
  3. thinkphp图片集合
  4. h5游戏引流推广方法:电商平台如何引流推广?
  5. Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, E
  6. 华为路由器联动_华为a1路由器怎么联动Yeelight智能设备
  7. 定时任务实现的几种方式
  8. java实现lbs_Java总结篇系列:Java泛型
  9. 恒流恒压线性充电芯片MST4210
  10. 芒果改进YOLOv7系列:超越ConvNeXt结构,原创结合Conv2Former改进结构,Transformer 风格的卷积网络视觉基线模型,高效涨点