zoukankan      html  css  js  c++  java
  • crm开发(基于ssh)(2)

    今天内容

    1 新增客户

    2 客户列表

    3 修改客户

    4 删除客户

    5 分页显示客户列表

    Hibernate模板里面的方法

     1 新增客户

    配置

    <mapping resource="cn/itcast/entity/Customer.hbm.xml" />

    创建userAction和CustomerAction,然后在struts.xml中进行配置。

    创建实体类,User和Customer,并创建配置文件,在数据库中建表。

    使用模型驱动的条件:模型驱动的属性(javabean)名字和表单输入项name属性值要一样。

    出现如下异常

    这样改变即可。

    import org.springframework.transaction.annotation.Transactional;
    
    import cn.itcast.dao.CustomerDao;
    import cn.itcast.entity.Customer;
    
    @Transactional
    public class CustomerService {

    表单提交方式是post提交,不需要进行处理,自动处理乱码。

    客户列表功能

     1 查询所有的客户显示到页面的列表里面

    (1)查询所有客户,在dao里面调用Hibernate模板里面的find方法

    (2)在action得到返回所有客户list集合,把list集合传递到页面中显示

    -把list放到域对象

    -把list放到值栈

    2 具体实现

    (1)在dao使用hibernate模板的find方法

    //客户列表功能
        @SuppressWarnings("all")
        public List<Customer> findAll() {
            return (List<Customer>) this.getHibernateTemplate().find("from Customer");
        }

    在action中

    // 3 客户添加列表的方法
        public String list() {
            List<Customer> list = customerService.findAll();
            //放到域对象
            ServletActionContext.getRequest().setAttribute("list", list);
            return "list";
        }

    使用值栈的方式

    //定义list变量
        private List<Customer> list;
        //生成变量的list方法
        public List<Customer> getList(){
            return list;
        }
        
        // 3 客户添加列表的方法
        public String list() {
            //List<Customer> list = customerService.findAll();
            //放到域对象
            //ServletActionContext.getRequest().setAttribute("list", list);
            
            //返回list放到值栈里面
            list = customerService.findAll();
            //上面操作就是把数据放到值栈
            //运行成功没有报错
            return "list";
        }

    使用struts2标签+ognl表达式

    <s:iterator value="list" var="cus">
                                                        <TR
                                                            style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
                                                            <TD><s:property value="#cus.custName" /></TD>
                                                            <TD><s:property value="#cus.custLevel" /></TD>
                                                            <TD><s:property value="#cus.custSource" /></TD>
                                                            <TD><s:property value="#cus.custPhone" /></TD>
                                                            <TD><s:property value="#cus.custMobile" /></TD>
                                                            
                                                        </TR>
                                                    </s:iterator>

    而用EL表达式的性能比较低。

    删除客户功能

    1 在每条记录后面删除超链接,点击超链接删除这条记录

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

    2 创建action的删除方法

    (1)得到cid值

    <result name="delete" type="redirectAction">customer_list</result>
    //根据id查询
        public Customer findOne(int cid) {
            return this.getHibernateTemplate().get(Customer.class, cid);
        }
    
        //删除功能
        public void delete(Customer c) {
            this.getHibernateTemplate().delete(c);
        }

    (1)有的浏览器会出问题,如两次提交。

    添加判断是否为空的语句就行了。

    Customer c = customerService.findOne(cid);
            //判断根据id查询对象是否为空
            if(c != null){
                //调用方法删除
                customerService.delete(c);
                //级联,分页
            }

    (2)在修改页面表单里面,修改内容,点击保存,真正修改数据库

    <input type="hidden" name="custId" value="${customer.cid }"/>

    分页相关知识点

    1 在mysql里面使用分页sql语句:

    (1)使用关键字limit

     select * from t_customer limit 0,3

    第一个参数是开始位置,第二个参数是每页显示几条记录

    2 分页相关属性

    (1)当前页

    (2)每页显示记录数

    (3)总记录数

    (4)总页数

    -总记录数 除以 每页显示记录数

    -比如10条记录,每页显示5条,有2页

    -比如10条记录,每页显示9条,有两页

    -总记录除以每页显示记录数,如果能够整除,结果是相除结果,如果不能整除,结果为整数结果+1

    int a = 10;

    int b = 9;

    a/b = 1

    1+1 = 2;

    (5)开始位置

    (6)每页记录的list集合

    3 hibernate分页操作

     (1)hql

    (2)qbc

    //封装分页的数据到pagebean里面
        public PageBean listpage(Integer currentPage) {
            //创建PageBean对象
            PageBean pageBean = new PageBean();
            //当前页
            pageBean.setCurrentPage(currentPage);
            //总记录数
            int totalCount = customerDao.findCount();
            pageBean.setTotalCount(totalCount);
            
            //每页显示记录数
            int pageSize = 3;
            
            //总页数
            //总记录数除以每页显示记录数
            //能够整除
            int totalPage = 0;
            if(totalCount % pageSize == 0){//整除
                totalPage = totalCount / pageSize;
            }else{
                totalPage = totalCount / pageSize + 1;
            }
            pageBean.setTotalPage(totalPage);
            
            //开始位置
            int begin = (currentPage - 1) * pageSize;
            
            //每页记录的list集合
            List<Customer> list = customerDao.findPage(begin,pageSize);
            
            pageBean.setList(list);
            
            return pageBean;
        }

    在页面中显示分页数据,判断前一页和后一页

    <c:if test="${pageBean.currentPage!=1 }">
        [<A href="${pageContext.request.contextPath}/customer_listpage.action?currentPage=${pageBean.currentPage-1 }">前一页</A>]
        </c:if>
        
        <c:if test="${pageBean.currentPage!=pageBean.totalPage }">
        [<A href="${pageContext.request.contextPath}/customer_listpage.action?currentPage=${pageBean.currentPage+1 }">后一页</A>] 
        </c:if>

    分页基本信息

    共[<B>${pageBean.totalCount}</B>]条记录,[<B>${pageBean.totalPage}</B>]页
         ,当前第[<b>${pageBean.currentPage}</b>]页

     2 在dao进行分页操作

    (1)查询记录数

    //查询记录数
        public int findCount() {
            //调用hibernateTemplate里面的find方法实现
            List<Object> list = (List<Object>) this.getHibernateTemplate().find("select count(*) from Customer");
            //从list中得到值
            if(list != null && list.size() != 0){
                Object obj = list.get(0);
                //变成int
                Long lobj = (Long)obj;
                int count = lobj.intValue();
                return count;
            }
            return 0;
        }

    (2)分页查询

    有两种方式

    第一种 在hibernate模板里面使用底层hibernate代码实现(不用,但是需要知道)。

    第二种 使用离线对象和Hibernate模板的方法

    第二种方式是对第一种方式的封装

    //分页查询操作
        public List<Customer> findPage(int begin, int pageSize) {
            //第一种 使用Hibernate底层代码实现(了解)
            //得到sessionFactory
            //SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
            //得到session对象
            //Session session = sessionFactory.getCurrentSession();
            //设置分页信息
            /*Query query = session.createQuery("from Customer");
            query.setFirstResult(begin);
            query.setMaxResults(pageSize);
            List<Customer> list = query.list();*/
            //hibernate本身有一定缺陷,无法做复杂查询,
            //这种情况是在复杂查询的时候会用到
            
            //第二种 使用离线对象和hibernateTemplate的方法实现
            //1 创建离线对象,设置对哪个实体类进行操作
            DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
            
            //调用hibernateTemplate的方法实现
            //第一个参数是离线对象
            //第二个参数是开始位置
            //第三个参数是每页记录数
            //用find的方法无法实现分页
            List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize);
            return list;
        }

    条件查询

    //条件查询
        public List<Customer> findCondition(Customer customer) {
            //第一种方式:
            /*SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
            //得到session对象
            Session session = sessionFactory.getCurrentSession();
            Query query = session.createQuery("from Customer where custName=?");
            query.setParameter(0, "%"+customer.getCustName()+"%");
            
            List<Customer> list = query.list();
            return list;*/
            
            //第二种方式:调用hibernateTemplate的find方法实现
            List<Customer> list = (List<Customer>) this.getHibernateTemplate().find("from Customer where custName like ?", "%"+customer.getCustName()+"%");
            return list;
        }

    第三种方式

    (1)使用离线对象,调用hibernateTemplate里面的方法

    //第三种方式
            // 1 创建离线对象,设置对哪个实体类进行操作
            DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
            // 2 设置对实体类哪个属性
            criteria.add(Restrictions.like("custName", "%"+customer.getCustName()+"%"));
            // 3 调用hibernateTemplate里面的方法得到list集合
            List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
            return list;

    5 web阶段多条件组合查询,拼接sql语句实现

    String sql = "select * from user where 2=2";

    List list = new ArrayList();

    if(username != null){

    sql += "and username=?";

    }

    if(password != null){

    sql += "and password=?";

    }

  • 相关阅读:
    Server Application Unavailable 解决办法 (转)
    SQL SERVER:分割函数 split
    Win32汇编_基础
    Win32汇编_异常_筛选器
    创建进程常用函数
    内存常用函数
    桃花庵歌
    文天祥的诗
    Socket I/O模型全接触
    函数指针的神奇
  • 原文地址:https://www.cnblogs.com/liaoxiaolao/p/9945825.html
Copyright © 2011-2022 走看看