案例48-crm练习利用spring管理service和dao层的对象
1 导包


2 将 Service 对象以及 Dao 对象配置到 spring 容器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!--注册dao层对象到容器中 -->
<bean name="userDao" class="www.test.dao.impl.UserDaoImpl"></bean>
<bean name="linkManDao" class="www.test.dao.impl.LinkManDaoImpl"></bean>
<bean name="customerDao" class="www.test.dao.impl.CustomerDaoImpl"></bean>
<!--注册service层对象到容器中 -->
<bean name="userService" class="www.test.service.impl.UserServiceImpl">
<property name="ud" ref="userDao"></property>
</bean>
<bean name="linkManService" class="www.test.service.impl.LinkManServiceImpl">
<property name="cd" ref="customerDao"></property>
<property name="lmd" ref="linkManDao"></property>
</bean>
<bean name="customerService" class="www.test.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
</beans>
注意对象中需要有对应的属性才能注入。
package www.test.service.impl; import www.test.dao.CustomerDao;
import www.test.dao.LinkManDao;
import www.test.dao.impl.CustomerDaoImpl;
import www.test.dao.impl.LinkManDaoImpl;
import www.test.domain.Customer;
import www.test.domain.LinkMan;
import www.test.service.LinkManService;
import www.test.utils.HibernateUtils; public class LinkManServiceImpl implements LinkManService { private CustomerDao cd;
private LinkManDao lmd;
public void save(LinkMan lm) {
//打开事务
HibernateUtils.getCurrentSession().beginTransaction(); try {
//1 根据客户id获得客户对象
Long cust_id = lm.getCust_id();
Customer c = cd.getById(cust_id);
//2 将客户放入LinkMan中表达关系
lm.setCustomer(c);
//3 保存LinkMan
lmd.save(lm);
} catch (Exception e) {
e.printStackTrace();
//回滚事务
HibernateUtils.getCurrentSession().getTransaction().rollback();
}
//提交事务
HibernateUtils.getCurrentSession().getTransaction().commit(); }
public CustomerDao getCd() {
return cd;
}
public void setCd(CustomerDao cd) {
this.cd = cd;
}
public LinkManDao getLmd() {
return lmd;
}
public void setLmd(LinkManDao lmd) {
this.lmd = lmd;
} }
3 web.xml配置容器随项目的启动而启动
<!-- 可以让spring容器随项目的启动而创建,随项目的关闭而销毁 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 指定加载spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
4 在 Action 中获得容器中的对象
//=============================================================
//获得spring容器=>从Application域获得即可 //1 获得servletContext对象
ServletContext sc = ServletActionContext.getServletContext();
//2.从servletContext中获得ApplicationContext容器
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc); //3.从容器中获得CustomerService
CustomerService cs = (CustomerService) ac.getBean("customerService"); //============================================================
案例48-crm练习利用spring管理service和dao层的对象的更多相关文章
- 关于controller,service,dao层的问题记录
出错写法: 1>.AlarmRecordController art=new AlarmRecordController(); 2>.private static SystemServi ...
- 浅谈service、DAO层引入(转)
转自 http://www.4u4v.net/mvc-simple-enough-on-the-introduction-of-service-dao-layer.html MVC是web开发中常见的 ...
- service和Dao层有什么关系
Dao层:主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后就可在模块中调用此接口来进行数据业务的处理,而不用关心此接口的具体实现类是哪个类 ...
- 【service调用dao层传参的三种方式】
第一种方案:默认数组角标: service Public User selectUser(String name,String area); mapper: <select id="s ...
- 深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存
摘要: 主要针对Dao层的一些数据库查询的操作,数据实时性不强,直接加入缓存.当缓存中有的时候,就使用缓存中的数据.这样的方法,最终仅仅使用一个注解实现.对于之前的hibernate二级缓存使用,比较 ...
- model、dao、 service 和Comtroll层的关系
首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操 ...
- 拦截器通过Spring获取工厂类,注入bean对象
// 这里需要注意一点,我们在拦截器内无法通过SpringBean的方式注入LoggerJPA,我只能通过另外一种形式. /** * 根据传入的类型获取spring管理的对应dao * @param ...
- 利用工厂模式实现serviec层和dao层解耦
利用工厂模式实现serveice和dao层的解耦,这样就可以不用在service层实例化dao层的对象,当dao层代码发生改变的时候(数据库实现发生改变)直接修改配置文件就不用改变service层的代 ...
- java service domain dao 分层思路
今天在开发项目的时候,对项目的java后台的分层有一些看法: 首先,鼓励使用service domain dao 层分层设计概念. 其次,对几层作用的理解: 第一:dao层操作单表,不涉及复杂逻辑,主 ...
随机推荐
- RegExp正则表达式对象
JavaScript的RegExp对象有两种创建方式,一种是字面量,一种是对象. var r = /pattern/attributes或者new RegExp(pattern, attributes ...
- HTML5 Canvas核心技术图形动画与游戏开发 ((美)David Geary) 中文PDF扫描版
<html5 canvas核心技术:图形.动画与游戏开发>是html5 canvas领域的标杆之作,也是迄今为止该领域内容最为全面和深入的著作之一,是公认的权威经典.amazon五星级超级 ...
- Jenkins 自动化部署asp.net
使用步骤 1.安装jenkins.git和vs,并确保机器上安装了.net framework 4.5和.net framework4.0 ,完成后访问http://localhost:8080. 2 ...
- VS Code 运行html文件
用VS Code编写html文件,想在VS Code中直接打开运行,配置如下: 配置tasks.json 打开VS Code,点击"终端",选择"配置任务". ...
- cdq分治略解
前言 陌上花开,可缓缓归矣 --吴越王 寓意:意思是:田间阡陌上的花开了,你可以一边赏花,一边慢慢回来. 隐意:春天都到了,你怎么还没有回来.形容吴越王 ...
- 函数形参为基类数组,实参为继承类数组,下存在的问题------c++程序设计原理与实践(进阶篇)
示例: #include<iostream> using namespace std; class A { public: int a; int b; A(int aa=1, int bb ...
- C语言条件编译(#if,#ifdef,#ifndef,#endif,#else,#elif)
1.条件编译介绍 条件编译(conditional compiling)命令指定预处理器依据特定的条件来判断保留或删除某段源代码.例如,可以使用条件编译让源代码适用于不同的目标系统,而不需要管理该源代 ...
- [agc014d] Black and White Tree
Description 有一颗n个点的树,刚开始每个点都没有颜色. Alice和Bob会轮流对这棵树的一个点涂色,Alice涂白,Bob涂黑,Alice先手. 若最后存在一个白点,使得这个 ...
- luogu5212/bzoj2555 substring(后缀自动机+动态树)
对字符串构建一个后缀自动机. 每次查询的就是在转移边上得到节点的parent树中后缀节点数量. 由于强制在线,可以用动态树维护后缀自动机parent树的子树和. 注意一个玄学的优化:每次在执行连边操作 ...
- springcloud微服务架构的思考
在网上找到一张关于微服务体系架构的图 应用组件: 首先对于整个程序的入口应该是网关,zuul部分 这个组件在springcloud中的gateway服务之后,zuul可以进行网关分配,根据想应的路劲进 ...