1 7 package org.jfox.petstore.dao; 8 9 import java.sql.SQLException ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.HashMap ; 13 import java.util.Arrays ; 14 import javax.ejb.Local ; 15 import javax.ejb.Stateless ; 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 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", resultClass = Product.class, 57 hints = { 58 @QueryHint(name = "cache.partition", value = "product") 59 } 60 ) 61 62 } 63 ) 64 @Stateless 65 @Local 66 public class ProductDAOImpl extends DAOSupport implements ProductDAO { 67 68 public final static String GET_PRODUCT = "getProduct"; 69 public final static String GET_PRODUCT_LIST_BY_CATEGORY = "getProductListByCategory"; 70 public final static String SEARCH_PRODUCT = "searchProduct"; 71 72 73 @PersistenceContext(unitName = "JPetstoreMysqlDS") 74 EntityManager em; 75 76 protected EntityManager getEntityManager() { 77 return (EntityManagerExt)em; 78 } 79 80 86 87 public Product getProduct(String productId) throws SQLException { 88 return (Product)createNamedNativeQuery(GET_PRODUCT).setParameter("id", productId).getSingleResult(); 89 } 90 91 public List <Product> getProductListByCategory(String categoryId) throws SQLException { 92 return (List <Product>)createNamedNativeQuery(GET_PRODUCT_LIST_BY_CATEGORY).setParameter("categoryId", categoryId).getResultList(); 93 } 94 95 public List <Product> searchProductList(String [] keywords) throws SQLException { 96 return (List <Product>)createNamedNativeQuery(SEARCH_PRODUCT).setParameter("keywords", Arrays.asList(keywords)).getResultList(); 98 } 99 100 public static void main(String [] args) { 101 String 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"; Map <String , Object > parameterMap = new HashMap <String , Object >(); 111 parameterMap.put("keywords", new String []{"a", "b"}); 112 String result = VelocityUtils.evaluate(query, parameterMap); 113 System.out.println(result); 114 } 115 } 116 | Popular Tags |