Spring06-基於註解注入例項
阿新 • • 發佈:2018-12-16
大體結構
需要匯入的包:
建立實體類:
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);
}
}
}