Spring04-xml配置实现增删改查

大致结构

                          Spring04-xml配置实现增删改查_第1张图片

加入需要的包

                               Spring04-xml配置实现增删改查_第2张图片

 

建立实体类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文件



        
        
        
             
        
        
        
        
        
            
        
        
        
        
        
               
        
        
        
        
        
               
               
               
               
        
       

 ICustomerDao:客户持久层接口

package com.lixin.dao;

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

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

public interface ICustomerDao {

	/*
	 * 查询所有客户
	 */
	List 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 findAllCustomer() throws SQLException {
		return runner.query("select * from customer", new BeanListHandler(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.class),id);
	}

}

ICustomerService:客户的业务层接口

package com.lixin.service;

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

import com.lixin.domain.Customer;

/*
 * 客户的业务层接口
 */
public interface ICustomerService {
	/*
	 * 查询所有客户
	 */
     ListfindAllCustomer() 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 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");
		Listcustomers=cs.findAllCustomer();
		
		for(Customer c:customers) {
            System.out.println(c);
		}
	}
}

 

你可能感兴趣的:(Spring04-xml配置实现增删改查)