Spring案例CRM系统

1.用xml文件保存客户信息(只把较重要的代码上传)

//客户bean
package com.feng.domain;
public class Customer {
	private String cust_name;

	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
}
//service层接口
package com.feng.service;
import com.feng.domain.Customer;
public interface CustomerService {
	public void save(Customer customer);

}
//service层具体实现类
package com.feng.service.impl;
import com.feng.dao.CustomerDao;
import com.feng.domain.Customer;
import com.feng.service.CustomerService;
public class CustomerServiceImpl implements CustomerService{
	private CustomerDao customerDao;
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	public CustomerServiceImpl() {
		// TODO Auto-generated constructor stub
		System.out.println("CustomerServiceImpl对象被创建");
	}
	@Override
	public void save(Customer customer) {
		// TODO Auto-generated method stub
		System.out.println("执行了CustomerServiceImpl--save方法");
		customerDao.save(customer);
	}
}

//Dao层接口
package com.feng.dao;
import com.feng.domain.Customer;
public interface CustomerDao {
	public void save(Customer customer);
}
//Dao层具体实现类
package com.feng.dao.impl;
import com.feng.dao.CustomerDao;
import com.feng.domain.Customer;
public class CustomerDaoImpl implements CustomerDao{
	public CustomerDaoImpl() {
		// TODO Auto-generated constructor stub
		System.out.println("CustomerDaoImpl对象被创建");
	}
	@Override
	public void save(Customer customer) {
		// TODO Auto-generated method stub
		System.out.println("执行了CustomerDaoImpl--save方法");
		//操作数据库
	}
}
//Action类
package com.feng.web.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.feng.domain.Customer;
import com.feng.service.CustomerService;
import com.feng.service.impl.CustomerServiceImpl;
/**
 * Servlet implementation class CustomerAction
 */
@WebServlet("/customerServlet")
public class CustomerAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private Customer customer=new Customer();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerAction() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接受参数
		String method=request.getParameter("method");
		if(method.equals("addsubmit")) {
			String cust_name=request.getParameter("cust_name");
			customer.setCust_name(cust_name);
			//调用service层
			System.out.println("Action中的save方法");
			System.out.println(customer.getCust_name());
			//传统方式
//			CustomerService customerService=new CustomerServiceImpl();
//			customerService.save(customer);
			//spring的方式进行操作
			//ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
			//使用web监听器,不用没有保存都要new,节省资源开销
			WebApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
			CustomerService customerService=(CustomerService) ac.getBean("customerService");
			customerService.save(customer);
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
//applicationContex.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="customerDao" class="com.feng.dao.impl.CustomerDaoImpl"></bean>
	<bean id="customerService" class="com.feng.service.impl.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"></property>
	</bean>
</beans>

2.用注解的方式保存客户信息

//service层
package com.feng.ioc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/*<bean id="customerService" class="com.feng.ioc.CustomerService">
 * 		<property name="customerDao" ref="customerDao"></property>
 *  </bean>

*/
@Service(value = "customerService")
public class CustomerService {
	@Autowired//复杂类型的属性注入
	private CustomerDao customerDao;	
	@Value("Rose")//简单类型的属性注入
	private String name="jack";
	public void save() {
		System.out.println("CustomerService业务层被调用了。。。");
		customerDao.save();
		System.out.println(name);
	}
}

 

//Dao层
package com.feng.ioc;
import org.springframework.stereotype.Repository;
@Repository("customerDao")
public class CustomerDao {
	public void save() {
		System.out.println("CustomerDao层被调用了");
	}
}
//测试类
package com.feng.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.feng.ioc.mixed.ProductService;
public class SpringText {
	@Test
	public void testIoc() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		//获取bean
		CustomerService customerService=(CustomerService) ac.getBean("customerService");
		customerService.save();
	}
}
//applicationContex.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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- 扫描包下的类,查看哪个类上有@Component注解,把这个类的对象new出来 -->
	<context:component-scan base-package="com.feng.ioc"></context:component-scan>
</beans>

3.xml与注解混合配置

//Service层
package com.feng.ioc.mixed;
import org.springframework.beans.factory.annotation.Autowired;
public class ProductService {
	@Autowired
	private ProductDao productDao;
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	public void save() {
		System.out.println("ProductService--save");
		productDao.save();
	}
}
//Dao层
package com.feng.ioc.mixed;
public class ProductDao {
	public void  save() {
		System.out.println("ProductDao--save");
	}
}
//测试类
package com.feng.ioc.mixed;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.feng.ioc.mixed.ProductService;
public class SpringText {
	@Test
	public void testIoc1() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext-mixed.xml");
		//获取bean
		ProductService productService=(ProductService) ac.getBean("productService");
		productService.save();
		
	}
}
//applicationContex-mixed.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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<context:component-scan base-package="com.feng.ioc.mixed"></context:component-scan>
	<bean id="productDao" class="com.feng.ioc.mixed.ProductDao"></bean>
	<bean id="productService" class="com.feng.ioc.mixed.ProductService"></bean>
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值