Develop Spring MVC Applications (4-2)

xiaoxiao2021-03-06  117

l Create a TestProductManagerDaoJDBC class to make unit tests on DAO

Package tests;

Import java.util.list;

Import junit.framework.testcase;

Import org.springframework.jdbc.datasource.drivermanagerDataSource;

Import db.productmanagerdaojdbc;

Import bus.product;

Public class testproductmanagerdaojdbc extends testcase {

Private productmanagerdaojdbc pmda;

Public void setup () {

PMDAO = New ProductManagerDaoJDBC ();

DrivermanagerDataSource DS = New DriverManagerDataSource ();

DS.SetDriverClassName ("com.mysql.jdbc.driver);

DS.SETURL ("JDBC: MySQL: // LocalHost / Test");

DS.SETUSERNAME ("Test");

DS.SETPASSWORD ("Test");

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 ());

}

}

l Create DataSource in the setup () method for testing

(25) Modify web applications to use databases

l We only need to modify the business class ProductManager to use the database, and no need to modify the code of the view and controller part.

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.ListItIn ();

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);

}

}

}

l Here you need a reference for a productManagerDao interface and its setter method, which sets in the bean configuration

L This, in the ProductManager class, use the DAO method to implement the database lasting

转载请注明原文地址:https://www.9cbs.com/read-126205.html

New Post(0)