大体结构
需要导入的包:
建立实体类:
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 + "]";
}
}
创建客户的持久层接口:
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;
}
客户的持久层实现类:
package com.lixin.dao.impl;
import java.sql.SQLException;
import java.util.List;
/*
* 客户的持久层实现类
*/
import javax.annotation.Resource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;
import com.lixin.dao.ICustomerDao;
import com.lixin.domain.Customer;
@Repository("customerDao")
public class CustomerDaoImpl implements ICustomerDao {
@Resource(name="runner")
private QueryRunner 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);
}
}
客户的业务层接口:
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;
}
客户的业务层实现类:
package com.lixin.service.impl;
import java.sql.SQLException;
import java.util.List;
/*
* 客户的业务层实现类
*/
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lixin.dao.ICustomerDao;
import com.lixin.dao.impl.CustomerDaoImpl;
import com.lixin.domain.Customer;
import com.lixin.service.ICustomerService;
@Service("customerService")
public class CustomerServiceImpl implements ICustomerService {
//private ICustomerDao customerDao=new CustomerDaoImpl();配置了service,就不能使用此方法
//需要使用set方法
@Resource(name="customerDao")
private ICustomerDao 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);
}
}
spring的配置类(config):相当于bean.xml
package config;
import java.beans.PropertyVetoException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/*
* 一个spring的配置类
* 相当于bean.xml
*/
@ComponentScan("com.lixin")//自动扫描的包
@Import({JdbcConfiguration.class})//导入其他配置类
public class SpringConfiguration {
}
package config;
import java.beans.PropertyVetoException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcConfiguration {
@Bean(name="runner")//它是把方法的返回值存入spring容器中,该注解有一个属性,name:用于指定bean的id.当不指定时它有默认值,默认值是方法的名称
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
@Bean(name="dataSource")
public DataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource ds=new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
ds.setUser("root");
ds.setPassword("115600LX");
return ds;
}
}
测试类:
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.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lixin.domain.Customer;
import com.lixin.service.ICustomerService;
import com.lixin.service.impl.CustomerServiceImpl;
import config.SpringConfiguration;
public class CustomerServiceTest {
@Test
public void testFindAllCustomer() throws SQLException {
ApplicationContext ac=new AnnotationConfigApplicationContext(SpringConfiguration.class);
ICustomerService cs=(ICustomerService) ac.getBean("customerService");
List<Customer>customers=cs.findAllCustomer();
for(Customer c:customers) {
System.out.println(c);
}
}
}