easyui04:datagrid数据查询

在easyui03上再加了些方法

com.zking.entity


package com.zking.entity;

import java.io.Serializable;

/**
 * 实体类:书籍类
 * @author zjjt
 *
 */
public class Book implements Serializable{
	private int bid;
	private String bname;
	private double bprice;
	private String btype;
	
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	public String getBname() {
		return bname;
	}
	public void setBname(String bname) {
		this.bname = bname;
	}
	public double getBprice() {
		return bprice;
	}
	public void setBprice(double bprice) {
		this.bprice = bprice;
	}
	public String getBtype() {
		return btype;
	}
	public void setBtype(String btype) {
		this.btype = btype;
	}
	
	public Book() {
		// TODO Auto-generated constructor stub
	}
	
	public Book(int bid, String bname, double bprice, String btype) {
		this.bid = bid;
		this.bname = bname;
		this.bprice = bprice;
		this.btype = btype;
	}
	
	public Book(String bname, double bprice, String btype) {
		this.bname = bname;
		this.bprice = bprice;
		this.btype = btype;
	}
	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", bprice=" + bprice + ", btype=" + btype + "]";
	}
	
	
	
}
数据库插入的新数据的脚本

create table tb_book (
  bid number not null,
  bname varchar2(50) not null,
  bprice float not null,
  btype varchar2(40) not null,
  primary key(bid)
)

-- 测试数据
insert into TB_BOOK (bid, bname, bprice, btype)
values (1, '西游记', 180, '名著');
insert into TB_BOOK (bid, bname, bprice, btype)
values (2, '红楼梦1', 110.08, '名著');
insert into TB_BOOK (bid, bname, bprice, btype)
values (3, '倚天屠龙记', 150.16, '武侠');
insert into TB_BOOK (bid, bname, bprice, btype)
values (4, '聊斋志异1', 100.12, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (5, '永生', 110.11, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (6, '武动乾坤', 90.89, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (7, '完美世界1', 100, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (8, '万域之王', 56.5, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (9, '遮天1', 130.9, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (10, '凡人修仙传1', 200, '修仙');
insert into TB_BOOK (bid, bname, bprice, btype)
values (11, '倚天屠龙记', 150.16, '武侠');
insert into TB_BOOK (bid, bname, bprice, btype)
values (12, '斗破苍穹', 115.07, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (13, '超级兵王1', 145, '言情');
insert into TB_BOOK (bid, bname, bprice, btype)
values (14, '武极天下', 45.55, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (15, '聊斋志异', 100.12, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (16, '永生1', 110.11, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (17, '武动乾坤', 90.89, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (18, '完美世界', 100, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (19, '万域之王', 56.5, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (20, 'Java', 1000, '修仙');
insert into TB_BOOK (bid, bname, bprice, btype)
values (21, '娃哈哈', 100, '玄幻');
insert into TB_BOOK (bid, bname, bprice, btype)
values (22, '呼啸山庄1', 123, '哈哈');
insert into TB_BOOK (bid, bname, bprice, btype)
values (23, '平凡的世界', 123, '哈哈');
insert into TB_BOOK (bid, bname, bprice, btype)
values (24, '大红底1', 12, '哈哈');
insert into TB_BOOK (bid, bname, bprice, btype)
values (25, '屌丝的逆袭1', 34.67, '哈哈');
insert into TB_BOOK (bid, bname, bprice, btype)
values (26, '嗨害嗨1', 22.3, '哈哈');
commit;
com.zking.dao

package com.zking.dao;

import java.util.List;

import com.zking.entity.Book;

public interface IBookDao {
	
	/**
	 * 带模糊查询的分页
	 * @param pageIndex  页数
	 * @param pageSize 每页几条数据
	 * @param str  名称的关键字
	 * @return  结果集合
	 */
	public List<Book> getAllByPage(int pageIndex,int pageSize,String str,String col);
	
	/**
	 * 获得总行数
	 * @param str  表名等
	 * @return 总行数
	 */
	public int getRows(String str);
	
}




package com.zking.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zking.entity.Book;
import com.zking.util.DBHelper;

public class BookDao implements IBookDao{
	//三兄弟
	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	
	
	@Override
	public List<Book> getAllByPage(int pageIndex, int pageSize, String str,String col) {
		List<Book>  list = new ArrayList<Book>();
		int a =(pageIndex-1)*pageSize+1;
		int b = pageIndex*pageSize;
		try {
			//获得连接
			con=DBHelper.getCon();
			//定义SQL语句
			String sql = "select * from (select a.*,rownum as rid from tb_book a where "+col+" like '%"+str+"%') b  where b.rid between ? and ?";
			//获得执行对象
			ps=con.prepareStatement(sql);
			//给占位符赋值
			ps.setInt(1, a);
			ps.setInt(2, b);
			//获得结果集
			rs=ps.executeQuery();
			while(rs.next()) {
				Book  bb = new Book(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4));
				//加到集合里面去
				list.add(bb);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClo(con, ps, rs);
		}
		return list;
	}

	@Override
	public int getRows(String str) {
		int n = 0;
		try {
			con=DBHelper.getCon();
			//定义SQL语句
			String sql = "select count(*) from  "+str;
			//获得执行对象
			ps=con.prepareStatement(sql);
			//获得结果集
			rs=ps.executeQuery();
			if(rs.next()) {
				n=rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBHelper.myClo(con, ps, rs);
		}
		return n;
	}

}
com.zking.biz

package com.zking.biz;

import java.util.List;

import com.zking.entity.Book;

public interface IBookBiz {
	
	/**
	 * 带模糊查询的分页
	 * @param pageIndex  页数
	 * @param pageSize 每页几条数据
	 * @param str  名称的关键字
	 * @return  结果集合
	 */
	public List<Book> getAllByPage(int pageIndex,int pageSize,String str,String col);
	
	/**
	 * 获得总行数
	 * @param str  表名等
	 * @return 总行数
	 */
	public int getRows(String str);
	
}


package com.zking.biz;

import java.util.List;

import com.zking.dao.BookDao;
import com.zking.dao.IBookDao;
import com.zking.entity.Book;
/**
 * 业务逻辑层
 * @author zjjt
 *
 */
public class BookBiz implements IBookBiz{

	  IBookDao ibd = new BookDao();
	
	@Override
	public List<Book> getAllByPage(int pageIndex, int pageSize, String str,String col) {
		return ibd.getAllByPage(pageIndex, pageSize, str,col);
	}

	@Override
	public int getRows(String str) {
		return ibd.getRows(str);
	}
	
//	public static void main(String[] args) {
//		System.out.println(ibd.getAllByPage(1, 10, ""));
//	}

}


com.zking.servlet


package com.zking.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.zking.biz.BookBiz;
import com.zking.biz.IBookBiz;
import com.zking.entity.Book;

/**
 * Servlet implementation class BookListServlet
 */
@WebServlet("/BookListServlet")
public class BookListServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码方式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
	
		//获得out
		PrintWriter  out =  response.getWriter();
		
		int pageIndex=1;
		int pageSize=10;
		
		//接收前台传递过来的值 page
		String pid = request.getParameter("page");//当前页码
		if(pid!=null) {
			pageIndex=Integer.parseInt(pid);
		}
		//接收前台传递过来的值 size
		String size = request.getParameter("rows");//每页多少条
		if(size!=null) {
			pageSize=Integer.parseInt(size);
		}
		/*//接收文本框关键词
		String bname = request.getParameter("bname");//关键字
		if(bname==null) {
			bname="";//相当于查询全部
		}*/
		
		//下拉框
		String bname = request.getParameter("bname");
		if(bname==null) {
			bname="bname";
		}
		//System.out.println(bname+"=========");
		//关键字  文本框
		String str = request.getParameter("str");
		if(str==null) {
			str="";//相当于查询全部
		}
		//System.out.println(str+"=========");
		//servlet调用biz
		IBookBiz ibb = new BookBiz();
		//获取总行数
		int rows = ibb.getRows(" tb_book  where "+bname+" like '%"+str+"%'");
		//拿到分页的集合
		List<Book> ls =  ibb.getAllByPage(pageIndex, pageSize,str,bname);//相当于查询所有
		
		//前台的JSON数据需要的两个参数: total:总行数 rows:书籍集合
		Map<String, Object> mym = new HashMap<String, Object>();
		//放两对值
		mym.put("total",rows);
		mym.put("rows", ls);
		
		
		//把map集合变成Jason格式的对象字符串
		String  b =  JSON.toJSONString(mym);
		//输送到页面
		out.write(b);
		out.flush();
		out.close();
		
		
		
	}

}
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- 引入公共页面 -->
<%@ include file="common/head.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	$(function(){
		$('#myTree').tree({    
		    url:ctx+'/moduleServlet' ,//请求地址 Ajax
		    animate:true ,//折叠或者展开的时候是否有动画效果
		    onDblClick: function(node){
				//alert(node.text);  // 在用户双击的时候提示
				//alert(node.url);//路径
				//拿到后代节点的集合
				var cs = $('#myTree').tree('getChildren',node.target);
				//alert(cs);
				if(cs.length==0){//没有后代节点
					var f = $('#myTab').tabs('exists',node.text);
					//判断是否存在
					if(!f){//说明不存在
						//新打开一个选项卡(tab页)
						$('#myTab').tabs('add',{    
				    	    title:node.text,//标题
				    	    content:'<iframe  scrolling="no"  frameborder="0px" width="99%"  height="99%" src="'+node.url+'" ></iframe>', //内容   
				    	    closable:true, //是否可关闭
				    	    /* tools:[{    
				    	        iconCls:'icon-ok',    
				    	        handler:function(){    
				    	            alert('想干啥就干啥');    
				    	        }    
				    	    }]*/
				    	    iconCls:node.iconCls
				    	});  
					}
					else{//说明存在 让其对应选中
						$('#myTab').tabs('select',node.text);
					}
				}
				
				
		    }
		}); 
	})
</script>
</head>
<body class="easyui-layout">   
    <div data-options="region:'north',split:true" style="height:85px;text-align:center;">
    <h1>后台书籍管理</h1>
    </div>   
    <div data-options="region:'south',split:true" style="height:60px;text-align:center;">
     	<b>&copy;玉渊工作室所有,未经允许不可擅自使用</b>
    </div>   
    <div data-options="region:'west',title:'功能导航',split:false" style="width:150px;">
    	<!--左侧  tree控件  -->
    	<ul id="myTree" class="easyui-tree">   
   			
       	
		</ul>  
    	
    	
    
    </div>   
    <div data-options="region:'center'" style="padding:5px;background:#fff;">
    	<!--中间的tab控件  -->
	    <div id="myTab"  data-options="pill:true"  class="easyui-tabs" style="width:100%;height:100%;">   
	    <div  data-options="iconCls:'icon-application-home'" title="首页" style="padding:20px;display:none;">   
	        <img src="images/1.jpeg" width="100%" height="100%">  
	    </div>   
	    
</div> 
    
    
    </div>   
</body> 
</html>

效果图:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值