1、实体类Customer
package vo;
public class Customer {
private String custname;
private String pwd;
private Integer age;
private String address;
public Customer() {
super();
}
public Customer(String custname, String pwd) {
super();
this.custname = custname;
this.pwd = pwd;
}
public Customer(String custname, String pwd, Integer age, String address) {
super();
this.custname = custname;
this.pwd = pwd;
this.age = age;
this.address = address;
}
public String getCustname() {
return custname;
}
public void setCustname(String custname) {
this.custname = custname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2、数据持久层接口CustomerDAO以及实现类CustomerDAOImpl
package dao;
import java.util.List;
import vo.Customer;
public interface CustomerDAO {
public List<Customer> selectAll();
public Customer selectByName(String custname);
public Customer selectByNamePwd(String custname,String pwd);
public void insert(Customer cust);
}
package dao.Impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import vo.Customer;
import dao.CustomerDAO;
import dao.JDBCConnectionFactory;
public class CustomerDAOImpl implements CustomerDAO{
<span style="white-space:pre"> </span>private DataSource dataSource;
<span style="white-space:pre"> </span>public void setDataSource(DataSource dataSource) {
<span style="white-space:pre"> </span>this.dataSource = dataSource;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public List<Customer> selectAll() {
<span style="white-space:pre"> </span>List<Customer>list =new ArrayList<Customer>();
<span style="white-space:pre"> </span>Connection conn = JDBCConnectionFactory.getConnection();
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>Statement stmt = conn.createStatement();
<span style="white-space:pre"> </span>String sql ="select custname,age,address from customer";
<span style="white-space:pre"> </span>ResultSet rs = stmt.executeQuery(sql);
<span style="white-space:pre"> </span>while(rs.next()){
<span style="white-space:pre"> </span>list.add(new Customer(rs.getString(1),null,rs.getInt(2),rs.getString(3)));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>} catch (SQLException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}finally{
<span style="white-space:pre"> </span>if(conn!=null){
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>conn.close();
<span style="white-space:pre"> </span>} catch (SQLException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return list;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public Customer selectByName(String custname) {
<span style="white-space:pre"> </span>Customer cust = null;
<span style="white-space:pre"> </span>Connection conn = JDBCConnectionFactory.getConnection();
<span style="white-space:pre"> </span>String sql = "select * from customer where custname=?";
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>PreparedStatement pstmt = conn.prepareStatement(sql);
<span style="white-space:pre"> </span>pstmt.setString(1, custname);
<span style="white-space:pre"> </span>ResultSet rs = pstmt.executeQuery();
<span style="white-space:pre"> </span>if(rs.next()){
<span style="white-space:pre"> </span>cust=new Customer(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>} catch (SQLException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}finally{
<span style="white-space:pre"> </span>if(conn!=null){
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>conn.close();
<span style="white-space:pre"> </span>} catch (SQLException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return cust;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public Customer selectByNamePwd(String custname, String pwd) {
<span style="white-space:pre"> </span>Customer cust = null;
<span style="white-space:pre"> </span>Connection conn = JDBCConnectionFactory.getConnection();
<span style="white-space:pre"> </span>String sql = "select * from customer where custname=? and pwd=?";
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>PreparedStatement pstmt = conn.prepareStatement(sql);
<span style="white-space:pre"> </span>pstmt.setString(1, custname);
<span style="white-space:pre"> </span>pstmt.setString(2, pwd);
<span style="white-space:pre"> </span>ResultSet rs = pstmt.executeQuery();
<span style="white-space:pre"> </span>if(rs.next()){
<span style="white-space:pre"> </span>cust=new Customer(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>} catch (SQLException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}finally{
<span style="white-space:pre"> </span>if(conn!=null){
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>conn.close();
<span style="white-space:pre"> </span>} catch (SQLException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return cust;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void insert(Customer cust) {
<span style="white-space:pre"> </span>Connection conn = JDBCConnectionFactory.getConnection();
<span style="white-space:pre"> </span>String sql = "insert into customer values(?,?,?,?)";
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>PreparedStatement pstmt = conn.prepareStatement(sql);
<span style="white-space:pre"> </span>pstmt.setString(1, cust.getCustname());
<span style="white-space:pre"> </span>pstmt.setString(2, cust.getPwd());
<span style="white-space:pre"> </span>pstmt.setInt(3, cust.getAge());
<span style="white-space:pre"> </span>pstmt.setString(4, cust.getAddress());
<span style="white-space:pre"> </span>pstmt.executeUpdate();
<span style="white-space:pre"> </span>} catch (SQLException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}finally{
<span style="white-space:pre"> </span>if(conn!=null){
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>conn.close();
<span style="white-space:pre"> </span>} catch (SQLException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}
上述代码中的CustomerDAOImpl类关联了DataSource类型的属性dataSource,并为属性dataSource提供了setter方法进行注入。
3、服务层接口CustomerService及实现类CustomerServiceImplpackage service;
import java.util.List;
import exception.RegisterException;
import vo.Customer;
public interface CustomerService {
public boolean login(String custname,String pwd);
public void register(Customer cust) throws RegisterException;
public Customer viewPersonal(String custname);
public List<Customer>viewAll();
}
package service;
import java.util.List;
import exception.RegisterException;
import vo.Customer;
import dao.CustomerDAO;
public class CustomerServiceImpl implements CustomerService{
private CustomerDAO dao;
public void setDao(CustomerDAO dao){
this.dao=dao;
}
public boolean login(String custname,String pwd){
Customer cust = dao.selectByNamePwd(custname, pwd);
if(cust!=null){
return true;
}else{
return false;
}
}
public void register(Customer cust)throws RegisterException{
Customer c = dao.selectByName(cust.getCustname());
if(c==null){
dao.insert(cust);
}else{
throw new RegisterException();
}
}
public Customer viewPersonal(String custname){
return dao.selectByName(custname);
}
public List<Customer> viewAll(){
return dao.selectAll();
}
}
所有服务都使用CustomerServiceImpl类进行提供。根据上述分析可见,CustomerServiceImpl类关联了CustomerDAO作为属性,
而CustomerDAO关联了DataSource作为属性。接下来,就可以使用IoC容器管理CustomerServiceImpl实例,
同时为其注入CustomerDAO属性,为CustomerDAO注入DataSource属性。
在applicationContext.xml中配置DataSource属性:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" scope="prototype">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mldn</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>password</value>
</property>
<property name="maxActive">
<value>10</value>
</property>
<property name="initialSize">
<value>2</value>
</property>
<bean id="dao" class="dao.Impl.CustomerDAOImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
上述配置中,CustomerDAOImpl类使用ref引用了已经配置好的dataSource实例,注入到属性dataSource中,
配置好CustomerDAOImpl实例后,就可以开始配置CustomerServiceImpl类的实例,如下所示:
<bean id="service" class="service.CustomerServiceImpl">
<property name="dao">
<ref bean="dao"/>
</property>
</bean>
上述配置中,使用ref引用了已经配置好的dao实例,将其注入到CustomerServiceImpl类的属性dao中。至此Ioc管理的bean都成功装配。
测试:
public static void main(String[] args) {
ApplicationContext ctxt=new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService service = (CustomerService)ctxt.getBean("service");
System.out.println(service.login("tom", "tom"));
}
运行显示结果:
true