`

网上商城学习札记(五)

SQL 
阅读更多
1、在学习一些新东西后者写一些新东西的的时候,要写一段,运行一段,再判断一段

2、动态方法(无static)与静态方法的区别:
   动态方法有一个好处:
 
   在一个类中的方法声明为动态的,则这个类中可以保留一个成员变量,当有人调用其中的一个方法时,可以把这个成员变量初始化,若初始化好了以后,再有人来用这个方法时,不用再从DB中取了!从这个成员变量直接拿出来(即内存中),前提是这个方法没有变化就可以了!也就是说这个包含动态方法的类做了一件事情——缓存!即缓存别人搜索过的对象!这样效率就快很多了,但是这样做消耗内存!这就是用空间换时间!


3、在SQL语句中如果where后跟了很多的过滤条件,则这些过滤条件的前后顺序对于效率有没有影响!
  
   有一点影响!应该将过滤粒度大的放在前面!(这个仅代表个人意见!)
    因为在DB中有对SQL语句的优化策略,具体怎样优化,不好意思不是很了解!没有研究过!


4、在项目汇总涉及到的复杂查询的代码:
/**
	 * 这个方法提供了可以根据产品的id来查询
	 * 根据名字查询
	 * 根据描述查询
	 * 根据一般价格段查询
	 * 根据会员价格段查询
	 * 根据日期段查询
	 * 而且这个查询支持分页
	 * 
	 * @param categoryId
	 * @param name
	 * @param descr
	 * @param lowNormalPrice
	 * @param highNormalPrice
	 * @param lowMemberPrice
	 * @param highMemberPrice
	 * @param startDate
	 * @param endDate
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public  List<Product> findProducts(int[] categoryId, 
											 String keyWord, 
											 double lowNormalPrice, 
											 double highNormalPrice, 
											 double lowMemberPrice,
											 double highMemberPrice,
											 Date startDate,
											 Date endDate,
											 int pageNo,
											 int pageSize) {
		Connection conn = null ;
		ResultSet rs = null ;
		List<Product> list = new ArrayList<Product>() ;
		try {
			conn = DB.getConn() ;
			String sql = "select * from product where 1=1 " ;
			
			String strId = "" ;
			//(2,3) (2,3,4)
			if(categoryId != null && categoryId.length > 0) {
				strId += "(" ;
				for(int i = 0 ; i < categoryId.length ; i ++) {
					if(i < categoryId.length - 1) {
						strId += categoryId[i] + "," ;
					}else {
						strId += categoryId[i] ;
					}
				}
				strId += ")" ;
				sql += " and categoryid in " + strId ;
			}
			
			if(keyWord != null && !keyWord.trim().equals("")) {
				sql += " and name like '%" + keyWord + "%' or descr like '%" + keyWord + "%'" ;
			}
			
			if(lowNormalPrice >= 0) {
				sql += " and normalprice > " + lowNormalPrice ;
			}
			
			if(highNormalPrice > 0) {
				sql += " and normalprice < " + highNormalPrice ;
			}
			
			if(lowMemberPrice >= 0) {
				sql += " and memberprice > " + lowMemberPrice ;
			}
			
			if(highMemberPrice > 0) {
				sql += " and memberprice < " + highMemberPrice ;
			}
			
			if(startDate != null) {
				sql += " and pdate >= '" + new SimpleDateFormat("yyyy-MM-dd").format(startDate) + "'";
			}
			
			if(endDate != null) {
				sql += " and pdate <= '" + new SimpleDateFormat("yyyy-MM-dd").format(endDate) + "'";
			}
			
			sql += " limit " + (pageNo - 1) * pageSize + " , " + pageSize ;
System.out.println(sql);
			
			//String sql = "select * from product limit " + (pageNo - 1) * pageSize + " , " + pageSize ;
			rs = DB.executeQuery(conn, sql) ;
			while(rs.next()) {
				Product p = new Product() ;
				p.setId(rs.getInt("id")) ;
				p.setName(rs.getString("name")) ;
				p.setDescr(rs.getString("descr")) ;
				p.setNormalprice(rs.getDouble("normalprice")) ;
				p.setMemberprice(rs.getDouble("memberprice")) ;
				p.setPdate(rs.getTimestamp("pdate")) ;
				p.setCategoryId(rs.getInt("categoryid")) ;
				
				list.add(p) ;
			}
			}catch(SQLException e) {
				e.printStackTrace() ;
			}finally {
				DB.closeRs(rs) ;
				DB.closeConn(conn) ;
		}
		return list;
	}

页面如图:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics