使用spring容器配置c3p0连接池完成增删改查

上一篇详细写了c3p0连接池完成增删改查的全部过程

链接如下:

https://blog.csdn.net/qq_18725165/article/details/80725267

这篇是将c3p0的连接数据库的所有配置全部放到spring容器中完成。

一、导包:

    在之前4个包的基础上增加Spring的四个包和2个日志包(不过后来没有写日志文件)

       使用spring容器配置c3p0连接池完成增删改查_第1张图片

二、配置Bean.xml

    创建Bean.xml 先把xml的约束复制到开头,然后按照业务层-持久层-C3P0连接池的顺序开始编写bean

    也就是让之前需要在实现类里面New的对象放到spring容器里来,让spring创建对象,一层套一层。

    



		
		
		
			
		
		
		
		
			
		
		
		
		
					
		
		
		
		
			
			
			
			
		

三、调整

        既然customerDao、runner对象已经在Spring容器里了,那么之前类文件里面的new 可以不用,将其改为set方法 

==========================================================

        private ICustomerDao customerDao;


public void setCustomerDao(ICustomerDao customerDao) {
this.customerDao = customerDao;

}

 ===========================================================

       private QueryRunner runner;

public void setRunner(QueryRunner runner) {

this.runner = runner;

}

===========================================================

而customerService是接口的对象 ,那么在测试方法下面直接调用Spring容器创建的customerService对象:

        ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
      ICustomerService cs=(ICustomerService)ac.getBean("customerService");

 C3P0Util.java中的ds对象已经在Spring容器中直接创建并注入了c3p0连接数据库的dataSource,那么该类和c3p0-config.xml文件可以直接删除。

 四、测试

        

package test;

import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import domain.Customer;
import service.CustomerServiceImpl;
import service.ICustomerService;

public class CustomerServicerTest {

	@Test
	public void testFindAllCustomer() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		ICustomerService cs=(ICustomerService)ac.getBean("customerService");
		List list=cs.findAllCustomer();
		for(Customer c: list){
			System.out.println(c);
		}
	}

	@Test
	public void testSaveCustomer() {
//		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//		ICustomerService cs=(ICustomerService) ac.getBean("customerService");

//		Customer c=new Customer();
//		c.setCust_name("滴滴");
//		c.setCust_source("dache");
//		
//		cs.saveCustomer(c);
	}

	@Test
	public void testUpdateCustomer() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		ICustomerService cs=(ICustomerService) ac.getBean("customerService");
		Customer c=cs.findCustomerById(4);
		c.setCust_name("京东");
		cs.updateCustomer(c);
	}

	@Test
	public void testDeleteCustomer() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		ICustomerService cs=(ICustomerService) ac.getBean("customerService");

	}

	@Test
	public void testFindCustomerById() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		ICustomerService cs=(ICustomerService) ac.getBean("customerService");

	}

}

至此Spring容器的强大配置功能已经崭露头角,工程结构图如下:

使用spring容器配置c3p0连接池完成增删改查_第2张图片


你可能感兴趣的:(JAVA,Mysql,c3p0,spring)