KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > petstore > dao > ProductDAOImpl


1 /*
2  * JFox - The most lightweight Java EE Application Server!
3  * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
4  *
5  * JFox is licenced and re-distributable under GNU LGPL.
6  */

7 package org.jfox.petstore.dao;
8
9 import java.sql.SQLException JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import javax.ejb.Local JavaDoc;
15 import javax.ejb.Stateless JavaDoc;
16 import javax.persistence.NamedNativeQueries;
17 import javax.persistence.NamedNativeQuery;
18 import javax.persistence.PersistenceContext;
19 import javax.persistence.EntityManager;
20 import javax.persistence.QueryHint;
21
22 import org.jfox.petstore.entity.Product;
23 import org.jfox.entity.dao.DAOSupport;
24 import org.jfox.entity.EntityManagerExt;
25 import org.jfox.util.VelocityUtils;
26
27 /**
28  * @author <a HREF="mailto:jfox.young@gmail.com">Young Yang</a>
29  */

30 @NamedNativeQueries(
31         {
32         @NamedNativeQuery(
33                 name = ProductDAOImpl.GET_PRODUCT,
34                 query = "select productid, name, descn, category from product where productid = $id",
35                 resultClass = Product.class
36         ),
37         @NamedNativeQuery(
38                 name = ProductDAOImpl.GET_PRODUCT_LIST_BY_CATEGORY,
39                 query = "select productid, name, descn, category from product where category = $categoryId",
40                 resultClass = Product.class,
41                 hints = {
42                     @QueryHint(name = "cache.partition", value = "product")
43                         }
44         ),
45         @NamedNativeQuery(
46                 name = ProductDAOImpl.SEARCH_PRODUCT,
47                 query = "select productid, name, descn, category from product " +
48                         "#if($keywords.size() > 0)" +
49                         "where 0!=0 " +
50                         "#foreach($word in $keywords)" +
51                         " OR lower(name) like $word " +
52                         " OR lower(category) like $word " +
53                         " OR lower(descn) like $word " +
54                         "#end" +
55                         "#end", // $p1 is keyword array
56
resultClass = Product.class,
57                 hints = {
58                     @QueryHint(name = "cache.partition", value = "product")
59                         }
60         )
61
62                 }
63 )
64 @Stateless JavaDoc
65 @Local JavaDoc
66 public class ProductDAOImpl extends DAOSupport implements ProductDAO {
67
68     public final static String JavaDoc GET_PRODUCT = "getProduct";
69     public final static String JavaDoc GET_PRODUCT_LIST_BY_CATEGORY = "getProductListByCategory";
70     public final static String JavaDoc SEARCH_PRODUCT = "searchProduct";
71
72
73     @PersistenceContext(unitName = "JPetstoreMysqlDS")
74     EntityManager em;
75
76     protected EntityManager getEntityManager() {
77         return (EntityManagerExt)em;
78     }
79
80 // 容器外测试的时候使用
81
/*
82     protected EntityManagerExt getEntityManager() {
83         return (EntityManagerExt)Persistence.createEntityManagerFactory("JPetstoreMysqlDS").createEntityManager();
84     }
85 */

86
87     public Product getProduct(String JavaDoc productId) throws SQLException JavaDoc {
88         return (Product)createNamedNativeQuery(GET_PRODUCT).setParameter("id", productId).getSingleResult();
89     }
90
91     public List JavaDoc<Product> getProductListByCategory(String JavaDoc categoryId) throws SQLException JavaDoc {
92         return (List JavaDoc<Product>)createNamedNativeQuery(GET_PRODUCT_LIST_BY_CATEGORY).setParameter("categoryId", categoryId).getResultList();
93     }
94
95     public List JavaDoc<Product> searchProductList(String JavaDoc[] keywords) throws SQLException JavaDoc {
96         // å°† keywords 作为一个å?‚数对象,å?¦åˆ™ï¼ŒJDK默认会使用å?˜å?‚(varargs)
97
return (List JavaDoc<Product>)createNamedNativeQuery(SEARCH_PRODUCT).setParameter("keywords", Arrays.asList(keywords)).getResultList();
98     }
99
100     public static void main(String JavaDoc[] args) {
101         String JavaDoc query = "select productid, name, descn, category from product " +
102                 "#if(!$keywords.isEmpty())" +
103                 "where 0!=0 " +
104                 "#foreach($word in $keywords)" +
105                 " OR lower(name) like \"%$word%\" " +
106                 " OR lower(category) like \"%$word%\" " +
107                 " OR lower(descn) like \"%$word%\"" +
108                 "#end" +
109                 "#end"; // $p1 is keyword array
110
Map JavaDoc<String JavaDoc, Object JavaDoc> parameterMap = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
111         parameterMap.put("keywords", new String JavaDoc[]{"a", "b"});
112         String JavaDoc result = VelocityUtils.evaluate(query, parameterMap);
113         System.out.println(result);
114     }
115 }
116
Popular Tags