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

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

链接如下:

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

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

一、导包:

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

       

二、配置Bean.xml

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

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

    

<?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">
		
		<!-- 配置service -->
		<bean id="customerService" class="service.CustomerServiceImpl">
			<property name="customerDao" ref="customerDao" ></property>
		</bean>
		
		<!-- 配置Dao -->
		<bean id="customerDao" class="dao.impl.CustomerDao">
			<property name="runner" ref="runner"></property>
		</bean>
		
		<!-- 配置QueryRunner -->
		<bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
			<constructor-arg name="ds" ref="dataSource"></constructor-arg>		
		</bean>
		
		<!-- 配置dataSource -->
		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
			<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis"></property>
			<property name="user" value="root"></property>
			<property name="password" value="123"></property>
		</bean>
</beans>

三、调整

        既然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<Customer> 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容器的强大配置功能已经崭露头角,工程结构图如下:



猜你喜欢

转载自blog.csdn.net/qq_18725165/article/details/80725761
今日推荐