- 浏览: 72029 次
- 性别:
- 来自: 东京
最新评论
-
JavaStudyEye:
讲解的很详细,呵呵
Ruby on Rails入门例子 -
greatwqs:
提供一个下载的例子岂不是更好?
Struts2.1+Spring3.0+JPA1.0(Hibernate3.3实现)例子 -
einsteinm:
老套路!
Struts+Spring+Hibernate添加用户例子 -
processlife:
个人感觉有些陈旧...
现在比较流行 appfuse 的ss ...
Struts+Spring+Hibernate添加用户例子 -
dhxyu:
看看你是怎么使用hibernate的
Struts+Spring+Hibernate添加用户例子
BookStore
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>bookstore</groupId> <artifactId>BookStore</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>BookStore Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.1.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.4.0.GA</version> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>8.3-603.jdbc3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-struts</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-core</artifactId> <version>1.3.9</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> </dependencies> <build> <finalName>BookStore</finalName> </build> </project>
BookStore\init
message.txt
errors.header=<font color="red"> errors.footer=</font> error.login.pwmismatch=ログイン名とパスワードが一致しません。 error.createuser.pass2inmatch=確認用パスワードが一致しません。 error.createuser.useralreadyexist=このアカウントはすでに利用されています。 error.createuser.cannotcreate=ユーザが作成できませんでした。 error.createuser.hasempty=入力欄に空欄がありました。 error.addtocart.notselected=商品が選択されていません。 error.checkout.noselected=商品が選択されていません。 error.search.notfound=見つかりませんでした。
BookStore\src\main\java\bookstore\action\bean
AddToCartActionFormBean.java
package bookstore.action.bean; import org.apache.struts.action.ActionForm; public class AddToCartActionFormBean extends ActionForm{ private String[] selecteditems; public String[] getSelecteditems(){ return( this.selecteditems ); } public void setSelecteditems( String[] inSelecteditems ){ this.selecteditems = inSelecteditems; } }
CreateUserActionFormBean.java
package bookstore.action.bean; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; public class CreateUserActionFormBean extends ActionForm{ private String account; private String name; private String email; private String passwd; private String passwd2; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getPasswd2() { return passwd2; } public void setPasswd2(String passwd2) { this.passwd2 = passwd2; } public ActionErrors validate( ActionMapping mapping, HttpServletRequest req ){ ActionErrors errors = null; if( getName().equals( "" ) || getName() == null || getEmail().equals( "" ) || getEmail() == null || getAccount().equals( "" ) || getAccount() == null || getPasswd().equals( "" ) || getPasswd() == null || getPasswd2().equals( "" ) || getPasswd2() == null ){ errors = new ActionErrors(); errors.add( "illegalcreateuser", new ActionMessage( "error.createuser.hasempty" ) ); } return( errors ); } }
LoginActionFormBean.java
package bookstore.action.bean; import org.apache.struts.action.ActionForm; public class LoginActionFormBean extends ActionForm{ private String account; private String passwd; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } }
SearchActionFormBean.java
package bookstore.action.bean; import org.apache.struts.action.ActionForm; public class SearchActionFormBean extends ActionForm{ private String keyword; public String getKeyword() { return keyword; } public void setKeyword(String keyword) { this.keyword = keyword; } }
BookStore\src\main\java\bookstore\action
AddToCartAction.java
package bookstore.action; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import bookstore.action.bean.AddToCartActionFormBean; import bookstore.logic.BookLogic; import bookstore.vbean.VBook; public class AddToCartAction extends Action { BookLogic bookLogic; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ){ HttpSession httpSession = req.getSession( false ); if( httpSession == null ){ return( mapping.findForward( "illegalSession" ) ); } List<String> cart = (List<String>) httpSession.getAttribute( "Cart" ); if( cart == null ){ cart = new ArrayList<String>(); } AddToCartActionFormBean atcafb = (AddToCartActionFormBean)form; List<String> productList = (List<String>)httpSession.getAttribute( "ProductList" ); String[] selecteItemsArray = atcafb.getSelecteditems(); List<String> selectedItems = null; if( selecteItemsArray != null && selecteItemsArray.length != 0 ){ selectedItems = Arrays.asList( atcafb.getSelecteditems() ); } List<String> newCart = bookLogic.createCart( productList, selectedItems, cart ); httpSession.setAttribute( "Cart", newCart ); List<String> productListAll = bookLogic.getAllBookISBNs(); List<VBook> vProductList = bookLogic.createVBookList( productListAll, newCart ); httpSession.setAttribute( "ProductList", productListAll ); httpSession.setAttribute( "ProductListView", vProductList ); return( mapping.findForward( "Continue" ) ); } public void setBookLogic( BookLogic bookLogic ){ this.bookLogic = bookLogic; } }
CheckoutAction.java
package bookstore.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import bookstore.logic.BookLogic; public class CheckoutAction extends Action { BookLogic bookLogic; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ){ HttpSession httpSession = req.getSession( false ); if( httpSession == null ){ return( mapping.findForward( "illegalSession" ) ); } List<String> selectedItems = (List<String>)httpSession.getAttribute( "Cart" ); if( selectedItems == null || selectedItems.size() == 0 ){ ActionMessages errors = new ActionMessages(); errors.add( "productalart", new ActionMessage( "error.checkout.noselected" ) ); saveMessages( req, errors ); return( mapping.findForward( "illegalCheckout" ) ); } httpSession.setAttribute( "ItemsToBuy", bookLogic.createVCheckout( selectedItems ) ); return( mapping.findForward( "ToCheck" ) ); } public void setBookLogic( BookLogic bookLogic ){ this.bookLogic = bookLogic; } }
CreateUserAction.java
package bookstore.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import bookstore.action.bean.CreateUserActionFormBean; import bookstore.logic.CustomerLogic; public class CreateUserAction extends Action{ CustomerLogic customerLogic; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ){ CreateUserActionFormBean cuafb = (CreateUserActionFormBean)form; String passwd = cuafb.getPasswd(); String passwd2 = cuafb.getPasswd2(); ActionMessages errors; if( passwd.equals( passwd2 ) == false ){ // passwd and passwd2 not matched errors = new ActionMessages(); errors.add( "illegalcreateuser", new ActionMessage( "error.createuser.pass2inmatch" ) ); saveMessages( req, errors ); return( mapping.findForward( "illegalCreateUser" ) ); } String account = cuafb.getAccount(); if( customerLogic.isAlreadyExsited( account ) ){ // user has already exsited errors = new ActionMessages(); errors.add( "illegalcreateuser", new ActionMessage( "error.createuser.useralreadyexist" ) ); saveMessages( req, errors ); return( mapping.findForward( "illegalCreateUser" ) ); } if( !customerLogic.createCustomer( account, passwd, cuafb.getName(), cuafb.getEmail() ) ){ // user was not created errors = new ActionMessages(); errors.add( "illegalcreateuser", new ActionMessage( "error.createuser.cannotcreate" ) ); saveMessages( req, errors ); return( mapping.findForward( "illegalCreateUser" ) ); } return( mapping.findForward( "UserCreated" ) ); } public void setCustomerLogic(CustomerLogic inCustomerLogic) { this.customerLogic = inCustomerLogic; } }
LoginAction.java
package bookstore.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import bookstore.action.bean.LoginActionFormBean; import bookstore.logic.BookLogic; import bookstore.logic.CustomerLogic; import bookstore.vbean.VBook; public class LoginAction extends Action{ CustomerLogic customerLogic; BookLogic bookLogic; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ){ LoginActionFormBean lafb = (LoginActionFormBean)form; // password match if( !customerLogic.isPasswordMatched( lafb.getAccount(), lafb.getPasswd() ) ){ // Account Mismached ActionMessages errors = new ActionMessages(); errors.add( "illegallogin", new ActionMessage( "error.login.pwmismatch" ) ); saveMessages( req, errors ); return( mapping.findForward( "illegalLogin" ) ); } // getSession() HttpSession httpSession = req.getSession( false ); if( httpSession != null ){ httpSession.invalidate(); } httpSession = req.getSession(); httpSession.setAttribute( "Login", lafb.getAccount() ); List<String> productListAll = bookLogic.getAllBookISBNs(); List<VBook> vProductList = bookLogic.createVBookList( productListAll, null ); httpSession.setAttribute( "ProductList", productListAll ); httpSession.setAttribute( "ProductListView", vProductList ); return( mapping.findForward( "LoginSuccess" ) ); } public void setCustomerLogic(CustomerLogic customerLogic) { this.customerLogic = customerLogic; } public void setBookLogic(BookLogic bookLogic) { this.bookLogic = bookLogic; } }
OrderAction.java
package bookstore.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import bookstore.logic.CustomerLogic; import bookstore.logic.OrderLogic; public class OrderAction extends Action{ OrderLogic orderLogic; CustomerLogic customerLogic; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ){ HttpSession httpSession = req.getSession( false ); if( httpSession == null ){ return( mapping.findForward( "illegalSession" ) ); } String uid = (String)httpSession.getAttribute( "Login" ); List<String> cart = (List<String>)httpSession.getAttribute( "Cart" ); orderLogic.orderBooks( uid, cart ); req.setAttribute( "Customer", customerLogic.createVCustomer( uid ) ); return( mapping.findForward( "OrderSuccess" ) ); } public void setOrderLogic(OrderLogic orderLogic) { this.orderLogic = orderLogic; } public void setCustomerLogic(CustomerLogic customerLogic) { this.customerLogic = customerLogic; } }
SearchAction.java
package bookstore.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import bookstore.action.bean.SearchActionFormBean; import bookstore.logic.BookLogic; import bookstore.vbean.VBook; public class SearchAction extends Action{ BookLogic bookLogic; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res ){ HttpSession httpSession = req.getSession( false ); if( httpSession == null ){ return( mapping.findForward( "illegalSession" ) ); } ActionMessages errors; List<String> cart = (List<String>) httpSession.getAttribute( "Cart" ); SearchActionFormBean safb = (SearchActionFormBean)form; List<String> foundBooks = bookLogic.retrieveBookISBNsByKeyword( safb.getKeyword() ); if( foundBooks == null || foundBooks.size() == 0 ){ foundBooks = bookLogic.getAllBookISBNs(); errors = new ActionMessages(); errors.add( "productalart", new ActionMessage( "error.search.notfound" ) ); saveMessages( req, errors ); } List<VBook> vProductList = bookLogic.createVBookList( foundBooks, cart ); httpSession.setAttribute( "ProductList", foundBooks ); httpSession.setAttribute( "ProductListView", vProductList ); return( mapping.findForward( "SearchSuccess" ) ); } public void setBookLogic(BookLogic bookLogic) { this.bookLogic = bookLogic; } }
BookStore\src\main\java\bookstore\dao\hibernate
BookDAOImpl.java
package bookstore.dao.hibernate; import java.util.List; import java.util.regex.Pattern; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; import bookstore.dao.BookDAO; import bookstore.pbean.TBook; @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public class BookDAOImpl extends HibernateDaoSupport implements BookDAO{ public int getPriceByISBNs(final List<String> inISBNList) { HibernateTemplate ht = getHibernateTemplate(); return( ((Long)ht.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query priceQuery = session .createQuery("select sum( book.price ) from TBook book where book.isbn in ( :SELECTED_ITEMS )"); priceQuery.setParameterList("SELECTED_ITEMS", inISBNList); return( (Long)priceQuery.uniqueResult() ); } } )).intValue() ); } public List<TBook> retrieveBooksByKeyword(String inKeyword) { String escapedKeyword = Pattern.compile( "([%_])" ) .matcher( inKeyword ) .replaceAll( "\\\\$1" ); String[] keywords = { "%" + escapedKeyword + "%", "%" + escapedKeyword + "%", "%" + escapedKeyword + "%" }; List<TBook> booksList = getHibernateTemplate().find( "from TBook book where book.author like ?" + "or book.title like ? or book.publisher like ?" , keywords ); return( booksList ); } public List<TBook> retrieveBooksByISBNs( final List<String> inISBNList ){ HibernateTemplate ht = getHibernateTemplate(); if( inISBNList == null ){ return( ht.find( "from TBook book" ) ); }else{ return( ((List<TBook>)ht.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query retrieveQuery = session .createQuery("from TBook book where book.isbn in ( :ISBNS )"); retrieveQuery.setParameterList("ISBNS", inISBNList); return( retrieveQuery.list() ); } } ))); } } }
CustomerDAOImpl.java
package bookstore.dao.hibernate; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; import bookstore.dao.CustomerDAO; import bookstore.pbean.TCustomer; @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public class CustomerDAOImpl extends HibernateDaoSupport implements CustomerDAO{ public int getCustomerNumberByUid( final String inUid ){ HibernateTemplate ht = getHibernateTemplate(); return( ((Long)ht.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query numQuery = session .createQuery( "select count(*) from TCustomer customer where customer.uid like :UID" ); numQuery.setString( "UID", inUid ); return( (Long) numQuery.uniqueResult() ); } } )).intValue() ); } public TCustomer findCustomerByUid(final String inUid) { HibernateTemplate ht = getHibernateTemplate(); return( ((TCustomer)ht.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query priceQuery = session .createQuery("from TCustomer customer where customer.uid like :UID" ); priceQuery.setString( "UID", inUid ); return( (TCustomer)priceQuery.uniqueResult() ); } } ))); } @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public void saveCustomer( String inUid, String inPasswordMD5, String inName, String inEmail ){ TCustomer saveCustomer = new TCustomer(); saveCustomer.setUid( inUid ); saveCustomer.setPasswordmd5( inPasswordMD5 ); saveCustomer.setName( inName ); saveCustomer.setEmail( inEmail ); getHibernateTemplate().save( saveCustomer ); } }
OrderDAOImpl.java
package bookstore.dao.hibernate; import java.util.Calendar; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; import bookstore.dao.OrderDAO; import bookstore.pbean.TCustomer; import bookstore.pbean.TOrder; @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public class OrderDAOImpl extends HibernateDaoSupport implements OrderDAO{ @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public TOrder createOrder(TCustomer inCustomer){ TOrder saveOrder = new TOrder(); saveOrder.setTCustomer( inCustomer ); saveOrder.setOrderday( Calendar.getInstance().getTime() ); getHibernateTemplate().save( saveOrder ); return( saveOrder ); } }
OrderDetailDAOImpl.java
package bookstore.dao.hibernate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; import bookstore.dao.OrderDetailDAO; import bookstore.pbean.TBook; import bookstore.pbean.TOrder; import bookstore.pbean.TOrderDetail; @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public class OrderDetailDAOImpl extends HibernateDaoSupport implements OrderDetailDAO{ @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public void createOrderDetail(TOrder inOrder, TBook inBook) { TOrderDetail saveOrderDetail = new TOrderDetail(); saveOrderDetail.setTOrder( inOrder ); saveOrderDetail.setTBook( inBook ); getHibernateTemplate().save( saveOrderDetail ); } }
BookStore\src\main\java\bookstore\dao
BookDAO.java
package bookstore.dao; import java.util.List; import bookstore.pbean.TBook; public interface BookDAO{ public int getPriceByISBNs( List<String> inISBNList ); public List<TBook> retrieveBooksByKeyword( String inKeyword ); public List<TBook> retrieveBooksByISBNs( List<String> inISBNList ); }
CustomerDAO.java
package bookstore.dao; import bookstore.pbean.TCustomer; public interface CustomerDAO{ public int getCustomerNumberByUid( String inUid ); public TCustomer findCustomerByUid( String inUid ); public void saveCustomer( String inUid, String inPasswordMD5, String inName, String inEmail ); }
OrderDAO.java
package bookstore.dao; import bookstore.pbean.TCustomer; import bookstore.pbean.TOrder; public interface OrderDAO{ public TOrder createOrder( TCustomer inCustomer ); }
OrderDetailDAO.java
package bookstore.dao; import bookstore.pbean.TBook; import bookstore.pbean.TOrder; public interface OrderDetailDAO{ public void createOrderDetail( TOrder inOrder, TBook inBook ); }
BookStore\src\main\java\bookstore\logic
BookLogic.java
package bookstore.logic; import java.util.List; import bookstore.vbean.VBook; import bookstore.vbean.VCheckout; public interface BookLogic{ public List<String> getAllBookISBNs(); public List<String> retrieveBookISBNsByKeyword( String inKeyword ); public List<VBook> createVBookList(List<String> inProductList, List<String> inSelectedList ); public VCheckout createVCheckout( List<String> inSelectedList ); public List<String> createCart( List<String> inProductList, List<String> inSelectedList, List<String> inCart ); }
BookLogicImpl.java
package bookstore.logic; import java.util.ArrayList; import java.util.List; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; import bookstore.dao.BookDAO; import bookstore.pbean.TBook; import bookstore.vbean.VBook; import bookstore.vbean.VCheckout; @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public class BookLogicImpl implements BookLogic{ BookDAO bookdao; public List<String> getAllBookISBNs() { List<String> isbns = new ArrayList<String>(); for( TBook bookIter : bookdao.retrieveBooksByISBNs( null ) ){ isbns.add( bookIter.getIsbn() ); } return( isbns ); } public List<String> retrieveBookISBNsByKeyword(String inKeyword) { List<String> isbns = new ArrayList<String>(); for( TBook bookIter : bookdao.retrieveBooksByKeyword( inKeyword ) ){ isbns.add( bookIter.getIsbn() ); } return( isbns ); } public List<VBook> createVBookList(List<String> inProductList, List<String> inSelectedList) { List<VBook> vArrayList = new ArrayList<VBook>(); for( TBook bookIter : bookdao.retrieveBooksByISBNs( inProductList ) ){ VBook currentVBook = new VBook( bookIter ); currentVBook.setSelected( false ); if( inSelectedList != null && inSelectedList.size() != 0 ){ if( inSelectedList.contains( bookIter.getIsbn() ) ){ currentVBook.setSelected( true ); } } vArrayList.add( currentVBook ); } return( vArrayList ); } public VCheckout createVCheckout( List<String> inSelectedList ){ VCheckout vc = new VCheckout(); vc.setTotal( bookdao.getPriceByISBNs( inSelectedList ) ); List<VBook> viewList = new ArrayList<VBook>(); for( TBook iterBook : bookdao.retrieveBooksByISBNs( inSelectedList ) ){ VBook vb = new VBook( iterBook ); vb.setSelected(true ); viewList.add( vb ); } vc.setSelecteditems( viewList ); return( vc ); } public List<String> createCart( List<String> inProductList, List<String> inSelectedList, List<String> inCart ){ inCart.removeAll( inProductList ); if( inSelectedList != null && inSelectedList.size() != 0 ){ inCart.addAll( inSelectedList ); } return( inCart ); } public void setBookdao( BookDAO bookdao ){ this.bookdao = bookdao; } }
CustomerLogic.java
package bookstore.logic; import bookstore.vbean.VCustomer; public interface CustomerLogic { public boolean isAlreadyExsited( String inSid ); public boolean createCustomer( String inUid, String inPassword, String inName, String inEmail ); public boolean isPasswordMatched( String inUid, String inPassword ); public VCustomer createVCustomer( String inUid ); }
CustomerLogicImpl.java
package bookstore.logic; import bookstore.dao.CustomerDAO; import bookstore.pbean.TCustomer; import bookstore.vbean.VCustomer; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public class CustomerLogicImpl implements CustomerLogic { CustomerDAO customerdao; public boolean isAlreadyExsited( String inUid ){ int customernum = customerdao.getCustomerNumberByUid( inUid ); if( customernum != 0 ){ return( true ); }else{ return( false ); } } @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public boolean createCustomer( String inUid, String inPassword, String inName, String inEmail ){ if( isAlreadyExsited( inUid ) ){ return( false ); } String passwordMD5 = getStringDigest( inPassword ); customerdao.saveCustomer( inUid, passwordMD5, inName, inEmail ); return( true ); } public boolean isPasswordMatched(String inUid, String inPassword) { if( !isAlreadyExsited( inUid ) ){ return( false ); } TCustomer customer = customerdao.findCustomerByUid( inUid ); if( customer.getPasswordmd5() .equals( getStringDigest( inPassword ) ) == false ){ return( false ); } return( true ); } public VCustomer createVCustomer(String inUid) { return( new VCustomer( customerdao.findCustomerByUid( inUid ) ) ); } private static String getStringDigest( String inString ){ return( DigestUtils.md5Hex( inString + "digested" ) ); } public void setCustomerdao( CustomerDAO inCdao ){ this.customerdao = inCdao; } }
OrderLogic.java
package bookstore.logic; import java.util.List; public interface OrderLogic { public void orderBooks( String inUid, List<String> inISBNs ); }
OrderLogicImpl.java
package bookstore.logic; import java.util.List; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; import bookstore.dao.BookDAO; import bookstore.dao.CustomerDAO; import bookstore.dao.OrderDAO; import bookstore.dao.OrderDetailDAO; import bookstore.pbean.TBook; import bookstore.pbean.TCustomer; import bookstore.pbean.TOrder; @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public class OrderLogicImpl implements OrderLogic { BookDAO bookdao; CustomerDAO customerdao; OrderDAO orderdao; OrderDetailDAO odetaildao; @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public void orderBooks( String inUid, List<String> inISBNs ){ TCustomer customer = customerdao.findCustomerByUid( inUid ); TOrder order = orderdao.createOrder( customer ); for( TBook iterBook : bookdao.retrieveBooksByISBNs( inISBNs ) ){ odetaildao.createOrderDetail( order, iterBook ); } } public void setBookdao( BookDAO bookdao ){ this.bookdao = bookdao; } public void setCustomerdao( CustomerDAO customerdao ){ this.customerdao = customerdao; } public void setOrderdao( OrderDAO orderdao ){ this.orderdao = orderdao; } public void setOrderdetaildao( OrderDetailDAO odetaildao ){ this.odetaildao = odetaildao; } }
BookStore\src\main\java\bookstore\pbean
TBook.java
package bookstore.pbean; // Generated 2008/12/06 0:12:15 by Hibernate Tools 3.2.1.GA import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; import org.hibernate.annotations.GenericGenerator; /** * TBook generated by hbm2java */ @Entity @Table(name = "t_book", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "isbn")) public class TBook implements java.io.Serializable { private int id; private String isbn; private String title; private String author; private String publisher; private int price; private Set<TOrderDetail> TOrderDetails = new HashSet<TOrderDetail>(0); public TBook() { } public TBook(int id, String isbn, String title, String author, String publisher, int price) { this.id = id; this.isbn = isbn; this.title = title; this.author = author; this.publisher = publisher; this.price = price; } public TBook(int id, String isbn, String title, String author, String publisher, int price, Set<TOrderDetail> TOrderDetails) { this.id = id; this.isbn = isbn; this.title = title; this.author = author; this.publisher = publisher; this.price = price; this.TOrderDetails = TOrderDetails; } @Id @GeneratedValue(generator="hibernate_sequence") @GenericGenerator(name="hibernate_sequence", strategy = "native") @Column(name="id", unique=true, nullable=false) public int getId() { return this.id; } public void setId(int id) { this.id = id; } @Column(name = "isbn", unique = true, nullable = false) public String getIsbn() { return this.isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } @Column(name = "title", nullable = false) public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } @Column(name = "author", nullable = false) public String getAuthor() { return this.author; } public void setAuthor(String author) { this.author = author; } @Column(name = "publisher", nullable = false) public String getPublisher() { return this.publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } @Column(name = "price", nullable = false) public int getPrice() { return this.price; } public void setPrice(int price) { this.price = price; } @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TBook") public Set<TOrderDetail> getTOrderDetails() { return this.TOrderDetails; } public void setTOrderDetails(Set<TOrderDetail> TOrderDetails) { this.TOrderDetails = TOrderDetails; } }
TCustomer.java
package bookstore.pbean; // Generated 2008/12/06 0:12:15 by Hibernate Tools 3.2.1.GA import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; import org.hibernate.annotations.GenericGenerator; /** * TCustomer generated by hbm2java */ @Entity @Table(name = "t_customer", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "uid")) public class TCustomer implements java.io.Serializable { private int id; private String uid; private String passwordmd5; private String name; private String email; private Set<TOrder> TOrders = new HashSet<TOrder>(0); public TCustomer() { } public TCustomer(int id, String uid, String passwordmd5, String name, String email) { this.id = id; this.uid = uid; this.passwordmd5 = passwordmd5; this.name = name; this.email = email; } public TCustomer(int id, String uid, String passwordmd5, String name, String email, Set<TOrder> TOrders) { this.id = id; this.uid = uid; this.passwordmd5 = passwordmd5; this.name = name; this.email = email; this.TOrders = TOrders; } @Id @GeneratedValue(generator="hibernate_sequence") @GenericGenerator(name="hibernate_sequence", strategy = "native") @Column(name="id", unique=true, nullable=false) public int getId() { return this.id; } public void setId(int id) { this.id = id; } @Column(name = "uid", unique = true, nullable = false) public String getUid() { return this.uid; } public void setUid(String uid) { this.uid = uid; } @Column(name = "passwordmd5", nullable = false) public String getPasswordmd5() { return this.passwordmd5; } public void setPasswordmd5(String passwordmd5) { this.passwordmd5 = passwordmd5; } @Column(name = "name", nullable = false) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Column(name = "email", nullable = false) public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TCustomer") public Set<TOrder> getTOrders() { return this.TOrders; } public void setTOrders(Set<TOrder> TOrders) { this.TOrders = TOrders; } }
TOrder.java
package bookstore.pbean; // Generated 2008/12/06 0:12:15 by Hibernate Tools 3.2.1.GA import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.GenericGenerator; /** * TOrder generated by hbm2java */ @Entity @Table(name = "t_order", schema = "public") public class TOrder implements java.io.Serializable { private int id; private TCustomer TCustomer; private Date orderday; private Set<TOrderDetail> TOrderDetails = new HashSet<TOrderDetail>(0); public TOrder() { } public TOrder(int id, TCustomer TCustomer, Date orderday) { this.id = id; this.TCustomer = TCustomer; this.orderday = orderday; } public TOrder(int id, TCustomer TCustomer, Date orderday, Set<TOrderDetail> TOrderDetails) { this.id = id; this.TCustomer = TCustomer; this.orderday = orderday; this.TOrderDetails = TOrderDetails; } @Id @GeneratedValue(generator="hibernate_sequence") @GenericGenerator(name="hibernate_sequence", strategy = "native") @Column(name="id", unique=true, nullable=false) public int getId() { return this.id; } public void setId(int id) { this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "customer_id_fk", nullable = false) public TCustomer getTCustomer() { return this.TCustomer; } public void setTCustomer(TCustomer TCustomer) { this.TCustomer = TCustomer; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "orderday", nullable = false, length = 35) public Date getOrderday() { return this.orderday; } public void setOrderday(Date orderday) { this.orderday = orderday; } @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TOrder") public Set<TOrderDetail> getTOrderDetails() { return this.TOrderDetails; } public void setTOrderDetails(Set<TOrderDetail> TOrderDetails) { this.TOrderDetails = TOrderDetails; } }
TOrderDetail.java
package bookstore.pbean; // Generated 2008/12/06 0:12:15 by Hibernate Tools 3.2.1.GA import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; /** * TOrderDetail generated by hbm2java */ @Entity @Table(name = "t_order_detail", schema = "public") public class TOrderDetail implements java.io.Serializable { private int id; private TOrder TOrder; private TBook TBook; public TOrderDetail() { } public TOrderDetail(int id, TOrder TOrder, TBook TBook) { this.id = id; this.TOrder = TOrder; this.TBook = TBook; } @Id @GeneratedValue(generator="hibernate_sequence") @GenericGenerator(name="hibernate_sequence", strategy = "native") @Column(name="id", unique=true, nullable=false) public int getId() { return this.id; } public void setId(int id) { this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "order_id_fk", nullable = false) public TOrder getTOrder() { return this.TOrder; } public void setTOrder(TOrder TOrder) { this.TOrder = TOrder; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "book_id_fk", nullable = false) public TBook getTBook() { return this.TBook; } public void setTBook(TBook TBook) { this.TBook = TBook; } }
BookStore\src\main\java\bookstore\vbean
VBook.java
package bookstore.vbean; import bookstore.pbean.TBook; public class VBook{ private String isbn; private String title; private String author; private String publisher; private int price; private boolean selected; public VBook(){} public VBook( TBook book ){ this.isbn = book.getIsbn(); this.title = book.getTitle(); this.author = book.getAuthor(); this.publisher = book.getPublisher(); this.price = book.getPrice(); this.selected = false; } public String getIsbn() { return this.isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return this.author; } public void setAuthor(String author) { this.author = author; } public String getPublisher() { return this.publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public int getPrice() { return this.price; } public void setPrice(int price) { this.price = price; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } }
VCheckout.java
package bookstore.vbean; import java.util.List; public class VCheckout{ private int total; private List selecteditems; public int getTotal(){ return( this.total ); } public void setTotal( int inTotal ){ this.total = inTotal; } public List getSelecteditems() { return selecteditems; } public void setSelecteditems(List inSelecteditems) { this.selecteditems = inSelecteditems; } }
VCustomer.java
package bookstore.vbean; import bookstore.pbean.TCustomer; public class VCustomer{ private String uid; private String name; private String email; public VCustomer(){} public VCustomer( TCustomer customer ){ uid = customer.getUid(); name = customer.getName(); email = customer.getEmail(); } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
BookStore\src\main\resources
MessageResources.properties
errors.header=<font color="red"> errors.footer=</font> error.login.pwmismatch=\u30ed\u30b0\u30a4\u30f3\u540d\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u4e00\u81f4\u3057\u307e\u305b\u3093\u3002 error.createuser.pass2inmatch=\u78ba\u8a8d\u7528\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u4e00\u81f4\u3057\u307e\u305b\u3093\u3002 error.createuser.useralreadyexist=\u3053\u306e\u30a2\u30ab\u30a6\u30f3\u30c8\u306f\u3059\u3067\u306b\u5229\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002 error.createuser.cannotcreate=\u30e6\u30fc\u30b6\u304c\u4f5c\u6210\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002 error.createuser.hasempty=\u5165\u529b\u6b04\u306b\u7a7a\u6b04\u304c\u3042\u308a\u307e\u3057\u305f\u3002 error.addtocart.notselected=\u5546\u54c1\u304c\u9078\u629e\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002 error.checkout.noselected=\u5546\u54c1\u304c\u9078\u629e\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002 error.search.notfound=\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002
(欲知后事如何,请见二 )
代码来自日本的技术图书 :http://www.shuwasystem.co.jp/products/7980html/2197.html
发表评论
-
Struts2.1+Spring3.0+JPA1.0(Hibernate3.3实现)例子
2010-01-28 14:03 7664本文代码是传智播客黎活明老师所讲的《Spring2.5视频教程 ... -
Struts+Spring+iBATIS做的XML文件操作例子
2010-01-21 10:08 1075去年圣诞节前的一个例子,初版,给别人说明用的。 Str ... -
Struts1的版本差异——Action!
2009-12-25 13:44 897近来的项目要用Struts1.0.2框架,真够老的! 昨天模 ... -
中规模应用的开发(二)
2009-07-20 22:13 1007BookStore\src\main\webapp\WEB-I ... -
导入Velocity
2009-07-20 19:24 2360src\main\java\bookstore\action: ... -
导入Spring
2009-07-20 17:38 1234src\main\java\chap12app: Mai ... -
导入Maven
2009-07-20 17:27 1020一.Maven的展开 1.在 ... -
小规模应用的开发
2009-07-20 16:30 1133init: initdb dropdb BookSto ... -
导入Struts2
2009-07-20 15:54 902src: struts.xml <?xml ver ... -
导入Hibernate
2009-07-20 15:44 1182src\chap06app: Main.java pa ... -
JDBC登录
2009-07-20 15:31 1253LoginCheckServlet.java package ... -
JDBC
2009-07-20 15:18 711Main.java package chap05app; ... -
JSP/Servlet编程
2009-07-20 15:06 582PersonalInfoCheckServlet.java ... -
Spring Live-Spring Quick Start Tutorial
2007-03-02 11:35 1361http://sourcebeat.com/downloads ... -
Sysdeo Eclipse Tomcat Launcher plugin备份
2007-02-26 09:48 3063http://www.sysdeo.com/eclipse/t ... -
Struts+Spring+Hibernate添加用户例子
2007-02-02 12:41 4145由于jar太大,抓成图片以供下载. 图片在lib目录下. 数据 ... -
关于Struts+Hibernate分页的例子
2007-01-08 08:37 3026这个程序很久了,当时就查询分页这没有成功,一点遗憾:( ... -
测试 Enterprise Beans
2006-12-29 11:45 1721这个测试企业Beans的教程内容不多,却花费我很长时间。主要是 ... -
创建 J2EE 应用程序客户端
2006-12-29 10:24 1779确切地说,这个例子很失败! 从http://www.netbe ... -
J2EE 应用程序快速入门指南
2006-12-29 08:28 2255近来想系统地学习一下Java,NetBeas有较丰富的在线教程 ...
相关推荐
在大规模iOS开发中,Buck构建工具的应用实践逐渐成为行业内的一个热点话题。随着iOS应用项目的增长,无论是从代码量、团队规模、还是构建速度和效率等方面,都对现有的构建系统提出了更高的要求。在这篇关于《BUCK在...
亚马逊的大规模应用机器学习实践,展示了MLOps在机器学习模型开发和部署中的重要性。MLOps能够提高机器学习模型的质量和稳定性,提高开发效率和部署速度,降低开发成本和维护成本。 知识点: 1. 机器学习的生命...
在当前的数字化时代,基于Android的云应用开发已经成为开发者们关注的焦点,特别是在移动设备、平板电脑和Java Web三者之间实现数据同步和共享时。本文将深入探讨如何利用云技术构建Android应用,并以X5Cloud云平台...
云计算与大数据应用开发第六章:云计算应用开发(一) 云计算与大数据应用开发第六章:云计算应用开发(一)主要介绍了云计算应用开发的基本概念和技术架构,着重于REST应用程序架构和JSON数据交换语言的应用。 ...
标题中的“自己开发的简单桌面应用”表明这是一个个人或独立开发者创建的计算机程序,主要设计为在用户桌面环境中运行。这种应用通常具有特定的功能,旨在解决用户日常操作中的一些问题或者提供某种服务。 描述中的...
然而,随着互联网规模的不断扩大,对应用程序性能的要求也随之提高,因此,了解和实施高性能的PHP应用开发显得尤为重要。 首先,书中可能涵盖了PHP的基础知识,包括语法特性、变量、数据类型、流程控制等,这些都是...
在华为提供的培训资料中,我们得到了关于MapReduce应用开发的详细说明,包括它的定义、应用场景、开发过程以及一个典型程序——WordCount的实现。 首先,MapReduce定义上可以被理解为三层含义的集合。它是一个基于...
2. **数据库系统**:在Java Web应用中,常见的数据库管理系统如MySQL,它是一个开源、轻量级的关系型数据库,适合中小规模应用。连接MySQL通常使用JDBC(Java Database Connectivity),它是Java平台的标准接口,...
精益-敏捷理论在产品开发项目中的应用 精益-敏捷理论是供应链管理中的一种思想原理,旨在平衡大规模生产的经济效益和客户化定制的快速响应,以达到既有效降低生产流通成本,又提升客户满意度。该理论的应用在产品...
HBase应用开发是华为大数据培训课程中的一个重要组成部分,它不仅介绍了HBase的基本概念,还深入讲解了HBase的适用场景、应用开发流程、案例分析、表设计指导、常用开发接口以及实践操作等知识点。 首先,HBase被...
在Hadoop应用开发中,数据预处理是关键步骤,包括数据清洗、转换、归一化等,以确保数据质量和处理效率。 6. 案例分析: 课程中的案例实战部分可能涵盖电商数据分析、日志处理、推荐系统等,通过实际场景的应用,...
* 最多可支持 8 人同时使用:物联网应用开发实验台支持多人同时使用,满足大规模教学和研究需求。 * 支持多种教学环境下使用:物联网应用开发实验台支持多种教学环境下使用,如在线教学、offline 教学等,满足不同...
Java中间件技术是现代企业级应用开发的核心组成部分,它为构建大规模、高可用、可扩展的应用提供了基础架构。本文将深入探讨Java中间件的关键概念、技术及其在实际开发中的应用。 中间件是一种软件,它位于操作系统...
大数据技术在油田开发中的应用是一个逐渐深入并展现出巨大价值的领域。本研究对大数据技术进行了概述,并分析了其在油田企业应用中的发展趋势,进而研究了其在油田生产中的具体应用,目的是希望大数据技术能在我国...
《HBase企业应用开发实战》主要面向实际应用开发人员,提供了大量实际案例和解决方案,帮助读者了解如何在企业环境中有效地利用HBase。书中涵盖了HBase的安装配置、数据模型设计、表管理、性能调优、故障排查等方面...
尽管现代的Web技术(比如那些在HTML5中引入的技术)提供了应用程序中各自独立的逻辑页面相互感知和导航的能力,页面却不会在过程中重新加载任何端点,或者将控制转到另外一个页面. 同单页面应用程序的交互常常设计...
在物联网技术领域,单片机应用程序的开发是至关重要的,特别是在各类竞赛中,它能够帮助参赛者构建创新的解决方案。本资源"物联网竞赛-单片机应用程序开发库.rar"提供了全面的支持,包括针对不同通信协议的开发库,...