19. 增加message bundle
springapp/war/WEB-INF/classes/messages.properties
| title=SpringApp heading=Hello :: SpringApp greeting=Greetings, it is now |
20. 浏览改进后的结果http://localhost:8080/springapp
实现数据库持久层
21. 设置HSQL数据库,在JBuilder2005中增加HSQL库,选择菜单Tools-Configure-Libraries,在弹出的对话框中点击New按钮,输入HSQL库的名称:HSQL,点击Add按钮,将lib/hsqldb目录下的文件hsqldb.jar增加到HSQL库中。
选择菜单Enterprise-Enterprise Setup,在弹出的对话框中选择Database Drivers,按Add按钮增加HSQL库。
选择菜单Tools-Database Pilot,然后选择菜单Files-New输入Driver:org.hsqldb.jdbcDriver,URL:jdbc:hsqldb:db/test, 双击hsqldb:db/test,输入UserName:sa,不必输入Passsword。输入如下的SQL语句并执行
| CREATE TABLE products ( id INTEGER NOT NULL PRIMARY KEY, description varchar(255), price decimal(15,2) ); CREATE INDEX products_description ON products(description); INSERT INTO products (id, description, price) values(1, 'Lamp', 5.78); INSERT INTO products (id, description, price) values(2, 'Table', 75.29); INSERT INTO products (id, description, price) values(3, 'Chair', 22.81); |
这样就会在JBuilder_Home/bin目录下创建一个目录db,存放了数据库test的数据
22. 创建JDBC DAO (Data Access Object)实现
springapp/src/db/ProductManagerDao.java
| package db; import bus.Product; import java.util.List; public interface ProductManagerDao { public List getProductList(); public void increasePrice(Product prod, int pct); } |
springapp/src/db/ProductManagerDaoJdbc.java
| package db; import bus.Product; import java.util.List; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import javax.sql.DataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.jdbc.object.MappingSqlQuery; import org.springframework.jdbc.object.SqlUpdate; import org.springframework.jdbc.core.SqlParameter; public class ProductManagerDaoJdbc implements ProductManagerDao { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass()); private DataSource ds; public List getProductList() { logger.info("Getting products!"); ProductQuery pq = new ProductQuery(ds); return pq.execute(); } public void increasePrice(Product prod, int pct) { logger.info("Increasing price by " + pct + "%"); SqlUpdate su = new SqlUpdate(ds, "update products set price = price * (100 + ?) / 100 where id = ?"); su.declareParameter(new SqlParameter("increase", Types.INTEGER)); su.declareParameter(new SqlParameter("ID", Types.INTEGER)); su.compile(); Object[] oa = new Object[2]; oa[0] = new Integer(pct); oa[1] = new Integer(prod.getId()); int count = su.update(oa); logger.info("Rows affected: " + count); } public void setDataSource(DataSource ds) { this.ds = ds; } class ProductQuery extends MappingSqlQuery { ProductQuery(DataSource ds) { super(ds, "SELECT id, description, price from products"); compile(); } protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { Product prod = new Product(); prod.setId(rs.getInt("id")); prod.setDescription(rs.getString("description")); prod.setPrice(new Double(rs.getDouble("price"))); return prod; } } } |
springapp/src/bus/Product.java
| package bus; import java.io.Serializable; public class Product implements Serializable { private int id; private String description; private Double price; public void setId(int i) { id = i; } public int getId() { return id; } public void setDescription(String s) { description = s; } public String getDescription() { return description; } public void setPrice(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()); } } |
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="priceIncreaseForm" class="web.PriceIncreaseFormController"> <property name="sessionForm"><value>true</value></property> <property name="commandName"><value>priceIncrease</value></property> <property name="commandClass"><value>bus.PriceIncrease</value></property> <property name="validator"><ref bean="priceIncreaseValidator"/></property> <property name="formView"><value>priceincrease</value></property> <property name="successView"><value>hello.htm</value></property> <property name="productManager"> <ref bean="prodMan"/> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property> <property name="url"> <value>jdbc:hsqldb:/home/trisberg/workspace/springapp/db/test</value> </property> <property name="username"><value>sa</value></property> <property name="password"><value></value></property> </bean> <bean id="prodManDao" class="db.ProductManagerDaoJdbc"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <bean id="prodMan" class="bus.ProductManager"> <property name="productManagerDao"> <ref bean="prodManDao"/> </property> <!-- <property name="products"> <list> <ref bean="product1"/> <ref bean="product2"/> <ref bean="product3"/> </list> </property> --> </bean> <!-- <bean id="product1" class="bus.Product"> <property name="description"><value>Lamp</value></property> <property name="price"><value>5.75</value></property> </bean> <bean id="product2" class="bus.Product"> <property name="description"><value>Table</value></property> <property name="price"><value>75.25</value></property> </bean> <bean id="product3" class="bus.Product"> <property name="description"><value>Chair</value></property> <property name="price"><value>22.79</value></property> </bean> --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename"><value>messages</value></property> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hello.htm">springappController</prop> <prop key="/priceincrease.htm">priceIncreaseForm</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.JstlView</value> </property> <property name="prefix"><value>/WEB-INF/jsp/</value></property> <property name="suffix"><value>.jsp</value></property> </bean> </beans> |
springapp/src/tests/MockProductManagerDaoImpl.java
| package tests; import bus.Product; import java.util.List; import db.ProductManagerDao; import bus.Product; public class MockProductManagerDaoImpl implements ProductManagerDao { private List products; public void setProducts(List p) { products = p; } public List getProductList() { return products; } public void increasePrice(Product prod, int pct) { double newPrice = prod.getPrice().doubleValue() * (100 + pct)/100; prod.setPrice(new Double(newPrice)); } } |
springapp/src/tests/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> <bean id="springappController" class="web.SpringappController"> <property name="productManager"> <ref bean="prodMan"/> </property> </bean> <bean id="prodManDao" class="tests.MockProductManagerDaoImpl"> <property name="products"> <list> <ref bean="product1"/> <ref bean="product2"/> <ref bean="product3"/> </list> </property> </bean> <bean id="prodMan" class="bus.ProductManager"> <property name="productManagerDao"> <ref bean="prodManDao"/> </property> <!-- <property name="products"> <list> <ref bean="product1"/> <ref bean="product2"/> <ref bean="product3"/> </list> </property> --> </bean> <bean id="product1" class="bus.Product"> <property name="description"><value>Lamp</value></property> <property name="price"><value>5.75</value></property> </bean> <bean id="product2" class="bus.Product"> <property name="description"><value>Table</value></property> <property name="price"><value>75.25</value></property> </bean> <bean id="product3" class="bus.Product"> <property name="description"><value>Chair</value></property> <property name="price"><value>22.79</value></property> </bean> </beans> |
springapp/src/tests/TestProductManager .java
| package tests; import java.util.List; import java.util.ArrayList; import junit.framework.TestCase; import db.ProductManagerDao; import bus.ProductManager; import bus.Product; public class TestProductManager extends TestCase { private ProductManager pm; public void setUp() { pm = new ProductManager(); Product p = new Product(); p.setDescription("Chair"); p.setPrice(new Double("20.50")); ArrayList al = new ArrayList(); al.add(p); p = new Product(); p.setDescription("Table"); p.setPrice(new Double("150.10")); al.add(p); /* pm.setProducts(al); */ MockProductManagerDaoImpl pmdao = new MockProductManagerDaoImpl(); pmdao.setProducts(al); pm.setProductManagerDao(pmdao); pm.getProducts(); } public void testGetProducs() { List l = pm.getProducts(); Product p1 = (Product) l.get(0); assertEquals("Chair", p1.getDescription()); Product p2 = (Product) l.get(1); assertEquals("Table", p2.getDescription()); } public void testIncreasePrice() { pm.increasePrice(10); List l = pm.getProducts(); Product p = (Product) l.get(0); assertEquals(new Double("22.55"), p.getPrice()); p = (Product) l.get(1); assertEquals(new Double("165.11"), p.getPrice()); } } |
[首页] [上一页] [下一页] [末页]
