Spring04-xml配置实现增删改查

大致结构

                          

加入需要的包

                               

 

建立实体类domain/Customer

package com.lixin.domain;

import java.io.Serializable;
/*
 * 客户的实体类
 * 用DButils操作,使用要求:实体类中的属性和数据库的字段必须一致
 */
public class Customer implements Serializable{
	
	private long id;
	private String name;
	private String source;//来源
	private String industry;//行业
	private String level;//等级
	private String address;
	private String phone;
	
	
	
	public long getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public String getSource() {
		return source;
	}
	public String getIndustry() {
		return industry;
	}
	public String getLevel() {
		return level;
	}
	public String getAddress() {
		return address;
	}
	public String getPhone() {
		return phone;
	}
	public void setId(long id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setSource(String source) {
		this.source = source;
	}
	public void setIndustry(String industry) {
		this.industry = industry;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", source=" + source + ", industry=" + industry + ", level="
				+ level + ", address=" + address + ", phone=" + phone + "]";
	}
	
}

配置:bean.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">
        
        <!-- 配置service -->
        <bean id="customerService" class="com.lixin.service.impl.CustomerServiceImpl">
             <property name="customerDao" ref="customerDao"></property>
        </bean>
        
        
        <!-- 配置Dao -->
        <bean id="customerDao" class="com.lixin.dao.impl.CustomerDaoImpl">
            <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>
        
        
        <!-- 配置c3p0连接池-->
        <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/test"></property>
               <property name="user" value="root"></property>
               <property name="password" value="115600LX"></property>
        </bean>
       
</beans>

 ICustomerDao:客户持久层接口

package com.lixin.dao;

import java.sql.SQLException;
import java.util.List;

import com.lixin.domain.Customer;
/*
 * 客户的持久层接口
 */

public interface ICustomerDao {

	/*
	 * 查询所有客户
	 */
	List<Customer> findAllCustomer() throws SQLException;
	/*
	 * 保存客户
	 */

	void saveCustomer(Customer customer) throws SQLException;
	/*
	 * 更新客户
	 */

	void updateCustomer(Customer customer) throws SQLException;
	/*
	 * 根据id号删除客户
	 */

	void deleteCustomer(long id) throws SQLException;
	/*
	 * 根据id查找客户
	 */

	Customer findCustomerById(long id) throws SQLException;

}

CustomerDaoImpl :客户的持久层实现类

package com.lixin.dao.impl;

import java.sql.SQLException;

import java.util.List;
/*
 * 客户的持久层实现类
 */

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.lixin.dao.ICustomerDao;
import com.lixin.domain.Customer;

public class CustomerDaoImpl implements ICustomerDao {
     private QueryRunner runner;
	public void setRunner(QueryRunner runner) {
		this.runner = runner;
	}

	@Override
	public List<Customer> findAllCustomer() throws SQLException {
		return runner.query("select * from customer", new BeanListHandler<Customer>(Customer.class));
		 
	}

	@Override
	public void saveCustomer(Customer customer) throws SQLException {
		runner.update("insert into customer(name,source,industry,level,address,phone)values(?,?,?,?,?,?)",
				customer.getName(),customer.getSource(),customer.getIndustry(),customer.getLevel(),
				customer.getLevel(),customer.getAddress(),customer.getPhone());


	}

	@Override
	public void updateCustomer(Customer customer) throws SQLException {
		runner.update("update customer set name=?,source=?,industry=?,level=?,address=?,phone=? where id=?",
				customer.getName(),customer.getSource(),customer.getIndustry(),customer.getLevel(),
				customer.getLevel(),customer.getAddress(),customer.getPhone(),customer.getId());

	}

	@Override
	public void deleteCustomer(long id) throws SQLException {
		runner.update("delete from customer where id=?", id);

	}

	@Override
	public Customer findCustomerById(long id) throws SQLException {
		return runner.query("select * from customer where id=?", new BeanHandler<Customer>(Customer.class),id);
	}

}

ICustomerService:客户的业务层接口

package com.lixin.service;

import java.sql.SQLException;
import java.util.List;

import com.lixin.domain.Customer;

/*
 * 客户的业务层接口
 */
public interface ICustomerService {
	/*
	 * 查询所有客户
	 */
     List<Customer>findAllCustomer() throws SQLException;
     /*
      * 
      * 保存客户
      */
     void saveCustomer(Customer customer) throws SQLException;
     /*
      * 更新客户
      */
     void updateCustomer(Customer customer) throws SQLException;
     /*
      * 根据id删除客户
      */
     void deleteCustomer(long id) throws SQLException;
     /*
      * 根据id查询客户
      */
     Customer findCustomerById(long id) throws SQLException;
}

CustomerServiceImpl:客户的业务层实现类

package com.lixin.service.impl;

import java.sql.SQLException;
import java.util.List;
/*
 * 客户的业务层实现类
 */

import com.lixin.dao.ICustomerDao;
import com.lixin.dao.impl.CustomerDaoImpl;
import com.lixin.domain.Customer;
import com.lixin.service.ICustomerService;

public class CustomerServiceImpl implements ICustomerService {
	//private ICustomerDao customerDao=new CustomerDaoImpl();配置了service,就不能使用此方法
	//需要使用set方法
	private ICustomerDao customerDao;

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

	@Override
	public List<Customer> findAllCustomer() throws SQLException {
		// TODO Auto-generated method stub
		return customerDao.findAllCustomer();
	}

	@Override
	public void saveCustomer(Customer customer) throws SQLException {
		customerDao.saveCustomer(customer);
	}

	@Override
	public void updateCustomer(Customer customer) throws SQLException {
		customerDao.updateCustomer(customer);

	}

	@Override
	public void deleteCustomer(long id) throws SQLException {
		customerDao.deleteCustomer(id);

	}

	@Override
	public Customer findCustomerById(long id) throws SQLException {
		// TODO Auto-generated method stub
		return customerDao.findCustomerById(id);
	}

}

测试类

package com.lixin.test;

import static org.junit.Assert.*;

import java.applet.AppletContext;
import java.sql.SQLException;
import java.util.List;

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

import com.lixin.domain.Customer;
import com.lixin.service.ICustomerService;
import com.lixin.service.impl.CustomerServiceImpl;

public class CustomerServiceTest {

	@Test
	public void testFindAllCustomer() throws SQLException {
		
		ApplicationContext ac=new ClassPathXmlApplicationContext(new String[] {"bean.xml"});
		ICustomerService cs=(ICustomerService) ac.getBean("customerService");
		List<Customer>customers=cs.findAllCustomer();
		
		for(Customer c:customers) {
            System.out.println(c);
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值