ice(Double d) {
price = d;
}
public Double getPrice() {
return price;
}
}
springapp/src/test/TestProductManagerDaoJdbc.java
package tests;
import java.util.List;
import java.util.ArrayList;
import junit.framework.TestCase;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import db.ProductManagerDaoJdbc;
import bus.Product;
public class TestProductManagerDaoJdbc extends TestCase {
private ProductManagerDaoJdbc pmdao;
public void setUp() {
pmdao = new ProductManagerDaoJdbc();
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:db/test");
ds.setUsername("sa");
ds.setPassword("");
pmdao.setDataSource(ds);
}
public void testGetProductList() {
List l = pmdao.getProductList();
Product p1 = (Product) l.get(0);
assertEquals("Lamp", p1.getDescription());
Product p2 = (Product) l.get(1);
assertEquals("Table", p2.getDescription());
}
public void testIncreasePrice() {
List l1 = pmdao.getProductList();
Product p1 = (Product) l1.get(0);
assertEquals(new Double("5.78"), p1.getPrice());
pmdao.increasePrice(p1, 10);
List l2 = pmdao.getProductList();
Product p2 = (Product) l2.get(0);
assertEquals(new Double("6.36"), p2.getPrice());
}
}
利用JBuilder 2005开发Spring实例(8)
时间:2010-12-04
23. 修改Web应用使其使用数据库持久层
springapp/src/bus/ProductManager.java
package bus;
import java.io.Serializable;
import java.util.ListIterator;
import java.util.List;
import db.ProductManagerDao;
public class ProductManager implements Serializable {
private ProductManagerDao pmd;
private List products;
public void setProductManagerDao(ProductManagerDao pmd) {
this.pmd = pmd;
}
/*
public void setProducts(List p) {
products = p;
}
*/
public List getProducts() {
products = pmd.getProductList();
return products;
}
public void increasePrice(int pct) {
ListIterator li = products.listIterator();
while (li.hasNext()) {
Product p = (Product) li.next();
/*
double newPrice = p.getPrice().doubleValue() * (100 + pct)/100;
p.setPrice(new Double(newPrice));
*/
pmd.increasePrice(p, pct);
}
}
}
springapp/war/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<!-- Controller for the initial "Hello" page -->
<bean id="springappController" class="web.SpringappController">
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<!-- Validator and Form Controller for the "Price Increase" page -->
<bean id="priceIncreaseValidator" class="bus.PriceIncreaseValidator"/>
<bean id="priceInc
|