家庭记账管理系统的增删改查,分页列表显示

上次登陆的时候也已经学习了增加的功能,这次再回忆一下

1.增

前端代码add.jsp中form表单把刷新后界面的地址与后面的action对象中结合起来,通过struts.xml配置文件进行跳转

1.1form表单

<FORM id=form1 name=form1
        action="${pageContext.request.contextPath }/customer_add.action"
        method=post>
        

1.2 customerAction类中,add方法,返回“add”与xml文件一致,action中注入service

 1 //1.添加页面
 2     public String toAddPage() {
 3         return "toAddPage";
 4     }
 5     //2.添加方法
 6     public String add()
 7     {
 8         //添加逻辑
 9         customerService.add(customer);
10         return "add";
11     }
12     

1.3struts.xml文件

1 <!-- 到添加的页面 -->
2     <result name="toAddPage">/jsp/customer/add.jsp</result>
3     
4     <!-- 添加成功之后 -->
5         <result name="add" type="redirectAction">customer_list</result>
6             

1.4service中注入dao

void add(Customer customer);

1.5 dao的实体类中实现增加的功能,调用hibernateTemplate中的save方法保存到数据库

//添加客户的功能
    public void add(Customer customer) {
        this.getHibernateTemplate().save(customer);
    }

2列表显示

2.0创建list.jsp前端界面

 1 <TR>
 2                                     <TD>
 3                                         <TABLE id=grid
 4                                             style="BORDER-TOP-WIDTH: 0px; FONT-WEIGHT: normal; BORDER-LEFT-WIDTH: 0px; BORDER-LEFT-COLOR: #cccccc; BORDER-BOTTOM-WIDTH: 0px; BORDER-BOTTOM-COLOR: #cccccc; WIDTH: 100%; BORDER-TOP-COLOR: #cccccc; FONT-STYLE: normal; BACKGROUND-COLOR: #cccccc; BORDER-RIGHT-WIDTH: 0px; TEXT-DECORATION: none; BORDER-RIGHT-COLOR: #cccccc"
 5                                             cellSpacing=1 cellPadding=2 rules=all border=0>
 6                                             <TBODY>
 7                                                 <TR
 8                                                     style="FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none">
 9                                                     <TD>客户名称</TD>
10                                                     <TD>客户级别</TD>
11                                                     <TD>客户来源</TD>
12                                                 
13                                                     <TD>电话</TD>
14                                                     <TD>手机</TD>
15                                                     <TD>操作</TD>
16                                                 </TR>
17                                                 <c:forEach items="${list }" var="customer">
18                                                 <TR
19                                                     style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
20                                                     <TD>${customer.custName }</TD>
21                                                     <TD>${customer.custLevel }</TD>
22                                                     <TD>${customer.custSource }</TD>
23                                                     
24                                                     <TD>${customer.custPhone }</TD>
25                                                     <TD>${customer.custMobile }</TD>
26                                                     <TD>
27                                                     <a href="${pageContext.request.contextPath }/customer_show.action?cid=${customer.cid}">修改</a>
28                                                     &nbsp;&nbsp;
29                                                     <a href="${pageContext.request.contextPath }/customer_delete.action?cid=${customer.cid}">删除</a>
30                                                     </TD>
31                                                 </TR>
32                                                 
33                                                 </c:forEach>
34 
35                                             </TBODY>
36                                         </TABLE>
37                                     </TD>
38                                 </TR>
39                                 

 2.1 action中创建list方法,注入service的findall,以及返回“list”与xml文件一致,好用来重定向

 1     private List<Customer> list;
 2     
 3     public List<Customer> getList() {
 4         return list;
 5     }
 6     //3.客户列表
 7     
 8     public String list()
 9     {
10         //List<Customer> list =customerService.findAll();
11         //放到域对象
12         //ServletActionContext.getRequest().setAttribute("list", list);
13         //还可用值栈的方式
14         list = customerService.findAll();
15         return "list";
16     }

2.2 xml文件


<!-- 到列表页面 -->
<result name="list">/jsp/customer/list.jsp</result>

2.3service中

public List<Customer> findAll() {
// TODO Auto-generated method stub
return customerDao.findAll();
}

2.4 dao与daoimpl

   dao中:   List<Customer> findAll();

实现类中:

依旧使用hibernate的模板

//列表功能实现
@SuppressWarnings("all")
public List<Customer> findAll(){

return (List<Customer>) this.getHibernateTemplate().find("from Customer");
}

2.5界面效果

3删

在列表界面已经添加了删除和修改的超链接

 <a href="${pageContext.request.contextPath }/customer_delete.action?cid=${customer.cid}">删除</a>

注意跳转方法的地址,已经按先查找id,根据id删除的方式

3.1 action中
 1     //4.删除
 2     public String delete() {
 3     //模型驱动   先id查询再删除
 4          int cid = customer.getCid();
 5          //现根据id查询
 6          Customer c= customerService.findOne(cid);
 7         //先进行判断
 8          if(c!=null) {
 9             //在进行删除
10              customerService.delete(c);
11              
12          }
13         
14         return "delete";
15     }

3.2service中findone方法

    public Customer findOne(int cid) {
        // TODO Auto-generated method stub
        return customerDao.findOne(cid);
    }

3.3 dao和daoimpl

     dao:    Customer findOne(int cid);

daoimpl:  
//根据id查询
    
    public Customer findOne(int cid) {
        
        return this.getHibernateTemplate().get(Customer.class, cid);
    }

3.5根据查询到的id进行删除(先判断是否有这个人)

 
      //先进行判断
 8          if(c!=null) {
 9             //在进行删除
10              customerService.delete(c);
11              
12          }
13         
14         return "delete";
15     }
3。6进入service中删除方法
    public void delete(Customer c) {
        // TODO Auto-generated method stub
        customerDao.delete(c);
    }

3.7dao和daoimpl

dao:   void delete(Customer c);
daoimpl


    //删除方法的实现
    public void delete(Customer c) {
        this.getHibernateTemplate().delete(c);
        
    }

3.8 xml文件,删除之后重定向到list

<!-- 删除之后-->
        <result name="delete" type="redirectAction">customer_list</result>

4 改 与添加类似

4.1先创建一个修改的界面edit.jsp

t提交按钮保存后form表单action

<FORM id=form1 name=form1
        action="${pageContext.request.contextPath }/customer_update.action"
        method=post>

4.2  action中与删除类似,线获取id,再根据id进性修改

 1 //5.修改
 2     public String show() {
 3         //使用模型驱动获得id
 4          int cid = customer.getCid();
 5          //现根据id查询
 6          Customer c= customerService.findOne(cid);
 7         //放到域对象里
 8          ServletActionContext.getRequest().setAttribute("customer", c);
 9              
10          
11         return "showCustomer";
12     }

4.3 service dao中findone已经完成

将查找到记录放到域对象中

4.4点击修改按钮跳转到修改界面
xml文档
<!-- 修改-到修改界面-->
        <result name="showCustomer">/jsp/customer/edit.jsp</result>

4.5 action中写修改的方法,注入service

    //6.修改的方法
    public String update() {
        customerService.update(customer);
        return "update";
    }

4.6 service中注入dao

    public void update(Customer customer) {
        // TODO Auto-generated method stub
        customerDao.update(customer);
    }

4.7 dao和daoimpl

dao:void update(Customer customer);

daoimpl :   

//修改方法的实现
public void update(Customer customer) {
this.getHibernateTemplate().update(customer);

}

4.8 修改之后重定向到list界面xml文档

<!-- 修改之后-->
<result name="update" type="redirectAction">customer_list</result>

5 查 (条件查询)

5.1

list界面加一个查询的按钮

    <TR>
   <TD>客户名称:</TD>
 <TD><INPUT class=textbox id=sChannel2
style="WIDTH: 80px" maxLength=50 name="custName"></TD>
      
 <TD><INPUT class=button id=sButton2 type=submit
  value=" 筛选 " name=sButton2></TD>
    </TR>

此前整个界面就一个按钮所以在form表单中进性重定向,但多个按钮就得在按钮中,至于按钮中还没研究

所以查询之后在form表单进性跳转

<FORM id="customerForm" name="customerForm"
        action="${pageContext.request.contextPath }/customer_listcondition.action"
        method=post>

5.2 action中创建listcondition 方法

注入service方法findcondition,并保存查询到的记录到域对象(第7行)

不输入任何内容的时候调用list方法中的显示所有

 1 //条件查询方法
 2     public String listcondition() {
 3         //如果输入客户名称,根据客户名称查询
 4         //如果不输入,查询所有
 5         if(customer.getCustName()!=null &&!"".equals(customer.getCustName())) {
 6             //不为空
 7             List<Customer> list=customerService.findCondition(customer);
 8             ServletActionContext.getRequest().setAttribute("list", list);
 9         }else {
10             //不输入任何内容,查询所有
11              list = customerService.findAll();
12         }
13         return "listcondition";
14     }

5.3 service中注入dao

    public List<Customer> findCondition(Customer customer) {
        // TODO Auto-generated method stub
        return customerDao.findCondition(customer);
    }

5.4 dao和daoimpl

dao:List<Customer> findCondition(Customer customer);

daoimpl:

条件查询有三种方式(常用第三种,可以级联查询)

 1 //条件查询
 2     public List<Customer> findCondition(Customer customer) {
 3         //第一种方式:
 4         //SessionFactory sessionFactory=this.getHibernateTemplate().getSessionFactory();
 5         //得到session对象
 6         //Session session =sessionFactory.getCurrentSession();
 7         //Query query=session.createQuery("from Customer where custName like ?");
 8         //query.setParameter(0, "%"+customer.getCustName()+"%");
 9         //List<Customer> list =query.list();
10         //第二种方式
11         //@SuppressWarnings("all")
12         //List<Customer> list=
13         //(List<Customer>) this.getHibernateTemplate()
14         //.find("from Customer where custName like ?", "%"+customer.getCustName()+"%");
15         
16         //拼借hql多条件查询
17         //第三种方式
18         //创建离线对象,设置对那个实体类经行操作
19         DetachedCriteria criteria =DetachedCriteria.forClass(Customer.class);
20         //设置对实体类的那个属性
21         criteria.add(Restrictions.like("custName", "%"+customer.getCustName()+"%"));
22         //调用hibernateTemplate里面的方法
23         List<Customer> list=
24                 (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
25         
26         return list;
27     }

5.5 xml文档,查询之后重定向到list界面

<!-- 条件查询之后-->
<result name="listcondition">/jsp/customer/list.jsp</result>

6 分页功能的实现

6.1 创建一个分页的实体类,并get和set

//当前页
    private Integer currentPage;
    //总记录数
    private Integer totalCount;
    //每页显示的记录数
    private Integer PageSize;
    //总的页数
    private Integer totalPage;
    //开始位置
    private Integer begin;
    //每页list集合
    private List<Customer> list;

6.2 在action中使用属性封装获取当前页,并注入service的分页方法

//使用属性封装获取
    private Integer currentPage;
    
    public Integer getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

//分页方法
public String listpage() {
//调用service的方法实现封装
PageBean pageBean=customerService.listpage(currentPage);
//放到域对象里面
ServletActionContext.getRequest().setAttribute("pageBean", pageBean);
return "listpage";
}

 

6.3分页的jsp

 1     
 2                                 <TR>
 3                                     <TD><SPAN id=pagelink>
 4                                             <DIV
 5                                                 style="LINE-HEIGHT: 20px; HEIGHT: 20px; TEXT-ALIGN: right">
 6                                                 共[<B>${pageBean.totalCount}</B>]条记录,共[<B>${pageBean.totalPage}</B>]页
 7                                                 ,当前第[<b>${pageBean.currentPage }</b>]页
 8                                                 ,每页显示
 9                                                 <select name="pageSize">
10                                                 
11                                                 <option value="15" <c:if test="${pageSize==1 }">selected</c:if>>1</option>
12                                                 <option value="30" <c:if test="${pageSize==30 }">selected</c:if>>30</option>
13                                                 </select>
14 15                                                 
16 <c:if test="${pageBean.currentPage!=1}">
17 
18     [<A href="${pageContext.request.contextPath }/customer_listpage.action?currentPage=${pageBean.currentPage-1}">前一页</A>]                                            
19 </c:if>
20                                                 
21                                                 
22 <c:if test="${pageBean.currentPage!=pageBean.totalPage}">                                                
23     [<A href="${pageContext.request.contextPath }/customer_listpage.action?currentPage=${pageBean.currentPage+1}">后一页</A>] 
24 </c:if>                                                
25 
26                                         
27                                                 
28                                                 
29                                                 
30                                                 
31 32                                                 <input type="text" size="3" id="page" name="page" />
33 34                                                 
35                                                 <input type="button" value="Go" onclick="to_page()"/>
36                                             </DIV>
37                                     </SPAN></TD>
38                                 </TR>

6.4

service中封装分页数据到pagebean中,并注入dao中findpage方法

 1     //封装分页数据到pagebean中去
 2     public PageBean listpage(Integer currentPage) {
 3         // 创建pagebean的对象
 4         PageBean pageBean =new PageBean();
 5         //当前页
 6         pageBean.setCurrentPage(currentPage);
 7         //总记录数
 8         int totalCount =customerDao.findCount();
 9         pageBean.setTotalCount(totalCount);
10         //每页显示记录数
11         int pageSize=3;
12         //总页数
13         //总页数除以每页显示记录数
14         //能够整除
15         int totalPage=0;
16         if(totalCount%pageSize==0) {//整出
17             
18             totalPage=totalCount/pageSize;
19         }else {
20             totalPage=totalCount/pageSize+1;
21         }
22             pageBean.setTotalPage(totalPage);    
23             //开始位置】
24             int begin =(currentPage-1)*pageSize;
25             //每页记录的list集合
26             List<Customer> list =customerDao.findPage(begin,pageSize);
27             pageBean.setList(list);
28         return pageBean;
29     }

6.5 dao 和daoimpl

dao: 

    int findCount();

    List<Customer> findPage(int begin, int pageSize);

daoimpl:查记录数时注意强转方式,分页操作一般采用离线对象hibernate方法实现

 1 //查询记录数
 2     public int findCount() {
 3         // 调用hibernateTemplate里面的find方法实现
 4         
 5         List<Object> list=(List<Object>) this.getHibernateTemplate().find("select count(*) from Customer");
 6         //从list中把值得到
 7         if(list!=null&&list.size()!=0) {
 8             Object obj=list.get(0);
 9             Long lobj=(Long) obj;
10             int count =lobj.intValue();
11             return count;
12         }
13         return 0;
14     }
15     //分页查询操作
16     public List<Customer> findPage(int begin, int pageSize) {
17         //1.hibernate底层代码实现(一般不用)
18         //得到sessionFactory
19         //SessionFactory sessionFactory=this.getHibernateTemplate().getSessionFactory();
20         //得到session对象
21         //Session session=sessionFactory.getCurrentSession();
22         //设置分页信息
23         //Query query=(Query) session.createCriteria("from Customer");
24         //query.setFirstResult(begin);
25         //query.setMaxResults(pageSize);
26         //List<Customer> list=query.list();
27         
28         
29         
30         
31         
32         //2.使用离线对象和hibernate方法实现
33         //2。1创建离线对象,设置对那个实体类进性操作
34         DetachedCriteria criteria =DetachedCriteria.forClass(Customer.class);
35         //调用hibernate方法实现,第一个参数离线对象,第二个开始位置,第三个每页记录数
36         List<Customer> list=(List<Customer>) this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize);
37         return list;
38     }

好了吗,这些边学边做也是花了一天半,殊不知这篇文章都写了一个小时

开始转向深度学习去了,加油啊,

清明节快乐,哈哈哈哈


























猜你喜欢

转载自www.cnblogs.com/stt-ac/p/10655129.html