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层的对象的更多相关文章

  1. 关于controller,service,dao层的问题记录

    出错写法: 1>.AlarmRecordController art=new AlarmRecordController(); 2>.private  static SystemServi ...

  2. 浅谈service、DAO层引入(转)

    转自 http://www.4u4v.net/mvc-simple-enough-on-the-introduction-of-service-dao-layer.html MVC是web开发中常见的 ...

  3. service和Dao层有什么关系

    Dao层:主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后就可在模块中调用此接口来进行数据业务的处理,而不用关心此接口的具体实现类是哪个类 ...

  4. 【service调用dao层传参的三种方式】

    第一种方案:默认数组角标: service Public User selectUser(String name,String area); mapper: <select id="s ...

  5. 深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存

    摘要: 主要针对Dao层的一些数据库查询的操作,数据实时性不强,直接加入缓存.当缓存中有的时候,就使用缓存中的数据.这样的方法,最终仅仅使用一个注解实现.对于之前的hibernate二级缓存使用,比较 ...

  6. model、dao、 service 和Comtroll层的关系

    首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操 ...

  7. 拦截器通过Spring获取工厂类,注入bean对象

    // 这里需要注意一点,我们在拦截器内无法通过SpringBean的方式注入LoggerJPA,我只能通过另外一种形式. /** * 根据传入的类型获取spring管理的对应dao * @param ...

  8. 利用工厂模式实现serviec层和dao层解耦

    利用工厂模式实现serveice和dao层的解耦,这样就可以不用在service层实例化dao层的对象,当dao层代码发生改变的时候(数据库实现发生改变)直接修改配置文件就不用改变service层的代 ...

  9. java service domain dao 分层思路

    今天在开发项目的时候,对项目的java后台的分层有一些看法: 首先,鼓励使用service domain dao 层分层设计概念. 其次,对几层作用的理解: 第一:dao层操作单表,不涉及复杂逻辑,主 ...

随机推荐

  1. XE下 SVG格式的图标使用方法

    下载一个SVG格式的图标,千图网,http://tool.58pic.com/tubiaobao/ 用txt文本打开SVG图标 拖一个PathLabel控件 在PathLabel控件的Data属性添加 ...

  2. PLSQL_Developer 连接win7_64位oracle11g

    window7系统 安装的64位 oracle11g,连接32位PLSQL_Developer 1 . 下载 PLSQL_Developer 9.0以上版本(绿色含汉化)   官方的 instantc ...

  3. Python中多使用迭代器

    英文原文出处:Use More Iterators 本文介绍将代码转换为使用迭代器的原因和实用技巧. 我最喜欢的Python语言的特色之一是生成器,它们是非常有用的,然而当阅读开源代码时,我很少遇到它 ...

  4. C# 中关于汉字与16进制转换的代码

    /// <summary> /// 从汉字转换到16进制 /// </summary> /// <param name="s"></par ...

  5. 没有为扩展名“.cshtml”注册的生成提供程序

    新建的mvc4 项目,然后从其他项目里拷贝shared文件夹和_ViewStart.cshtml文件过去,然后在@符号上出现“没有为扩展名“.cshtml”注册的生成提供程序” 解决方法: 需要在项目 ...

  6. Insus.NET最近想更换一部手机

    Insus.NET曾经使用过好几部手机.给Insus.NET工作与生活上带来了方便.最近想更换一部新手机,因此记念一下以前使用过的手机.当时Insus.NET没有相机,下面图片是网上找的(前四部): ...

  7. 【大数据之数据仓库】GreenPlum PK DeepGreen(TPCH)

    1.背景 一张UML类图可以简单的说明GreenPlum和DeepGreen之间的关系: GreenPlum: 主页:http://greenplum.org/ 源码:开源,https://githu ...

  8. LoadRunner--获取请求的返回结果函数

    注:内容来自网络 Action(){ web_set_max_html_param_len("262144"); // 默认最大长度为256 web_reg_save_param( ...

  9. Kotlin 区间的一些小注意

    1:步进 step 在kotlin 中区间通过循环可以实现每隔几个输出. 比如1..100,我每隔3个输出: fun main(args:Array<Stting>) { .. step) ...

  10. 关于windows上 web 和 ftp 站点的创建及使用

    关于windows上 web 和 ftp 站点的创建及使用 引言 其实这是我网络基础课上的一次作业,觉得挺实用的,遂写成博客分享,也算是对这次作业的一次总结. 实验目的 通过此实验掌握WEB和FTP站 ...