JavaWeb08(MVC应用02[家居商城]&连接数据库)

简介: JavaWeb08(MVC应用02[家居商城]&连接数据库)

一.绑定分类

1.1 效果预览

1.2 代码实现

  ①底层代码

/**
 * 查询类别全部
 */
    public List<Type> getAll() {
        List<Type> ls = new ArrayList<Type>();
        try {
            con=DBHelper.getCon();
            String sql="select * from tb_type";
            ps=con.prepareStatement(sql);
            rs=ps.executeQuery();
            while(rs.next()) {
                Type t = new Type(rs.getInt(1),rs.getString(2));
                ls.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBHelper.myClose(con, ps, rs);
        }
        return ls;
    }

//调用业务逻辑层
ITypeBiz itb = new TypeBiz();
//设置编码
req.setCharacterEncoding("utf-8");
//调用查询全部商品的集合方法
 List<Goods> goodsls = igb.getAll(tid,str);
//存起来  仅转发有效
 req.setAttribute("typels", typels);
//转发到type.jsp进行绑值展示数据
req.getRequestDispatcher("type.jsp").forward(req, resp);

②前端代码

<div class="col-3">
      <div class="list-group">
           <a href="#" class="list-group-item list-group-item-action  active"> 所有商品</a>
           <c:forEach items="${typels}" var="t">
             <a href="#" class="list-group-item list-group-item-action>${t.tname}</a>
                    </c:forEach>
</div>
        </div>

二.绑定所有商品

2.1 效果预览

2.2.代码实现

①底层代码

/**
 * 查询类别全部
 */
    public List<Type> getAll() {
        List<Type> ls = new ArrayList<Type>();
        try {
            con=DBHelper.getCon();
            String sql="select * from tb_type";
            ps=con.prepareStatement(sql);
            rs=ps.executeQuery();
            while(rs.next()) {
                Type t = new Type(rs.getInt(1),rs.getString(2));
                ls.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBHelper.myClose(con, ps, rs);
        }
        return ls;
    }

//调用业务逻辑层
IGoodsBiz igb = new GoodsBiz();
 //调用查询全部商品的集合方法
 List<Goods> goodsls = igb.getAll(tid,str);
//把查询出来的商品集合存起来
req.setAttribute("goodsls", goodsls);


②前端代码

<!--绑定商品  -->
<div class="row">
      <c:forEach items="${goodsls}" var="g">
             <div class="col-lg-3 col-md-4 col-sm-6">
                    <figure class="figure  text-center">
                        <img src="${g.gpath}" class="figure-img img-fluid rounded" >
                            <figcaption class="figure-caption">
                                  <b>${g.gname}<b><br>
                                        <b>&yen;${g.gprice}</b>
                </c:forEach>

三.分类查询

3.1效果预览

3.2代码实现

①底层代码

public List<Goods> getAll(String tid) {
        String sql="select * from tb_goods";
//tid为就意味着查询全部 不为空就意味着点了其他类别 传了类别id
        if(tid!=null) {
                sql+=" and tid="+tid;
            }
        sql+="order by gid desc";
}

//调用业务逻辑层
IGoodsBiz igb = new GoodsBiz();
//接收表单提交过来的类别ID
String tid = req.getParameter("tid");//第一次进来就是null
 //调用分类查询的方法
 List<Goods> goodsls = igb.getAll(tid,str);
//方便显示选中的样式 为了让对应的类别加active样式
req.setAttribute("tid", tid);

②前端代码

<!-- 点击“所有商品”的时候 tid为null 因此判断为empty才选中 同时刷新所有-->
 <a href="type.do" class="list-group-item list-group-item-action ${empty tid?'active':''}"> 所有商品</a>
<!-- 让对应的类别选中  加入active样式 因此要将tid存起来-->
 <a href="type.do?tid=${t.tid}&index=2" class="list-group-item list-group-item-action ${t.tid==tid?'active':''}">${t.tname}</a>

四.模糊查询

4.1 效果预览

4.2代码实现

①底层代码

public List<Goods> getAll(String tid,String str) {
            if(str==null) {
                str="";//相当于查询全部
            }
            String sql="select * from tb_goods where gname like '%"+str+"%'";
               //不为空就意味着点了其他类别  传了类别id
            if(tid!=null&&!"".equals(tid)) {
                sql+=" and tid="+tid;//将where----->and
            }
            sql+=" order by gid desc";

//调用分类查询方法
List<Goods> goodsls = igb.getAll(tid,str);
//方便连同分类一起查询  同时文本框显示关键字
req.setAttribute("str", str);

②前端代码

<form action="type.do" method="post">
                <!--传index 为了让分类选中  -->
                <input type="hidden" name="index" value="2"/>
                <!--传tid 为了模糊查询类别联合用  -->
                <input type="hidden" name="tid" value="${tid}"/>
                    <div class="form-group">
                        <div class="input-group mb-3">
                            <input name="str" value="${str}" type="text" class="form-control" placeholder="请输入家居关键字">
                            <div class="input-group-append">
                                <button class="btn btn btn-primary" type="submit" ></button>
                            </div>
                        </div>
                </form>
        </div>

五.分类模糊查询

5.1 效果预览

5.2 代码实现

//说明点了其他类别 tid是有值的   不为空也不为空字符串的时候
if(tid!=null&&!"".equals(tid)) {
                sql+=" and tid="+tid;
 }
 <!--传tid 为了模糊查询类别联合用  -->
 <input type="hidden" name="tid" value="${tid}"/>
 <!--点击类别的同时传递关键字到servlet  -->
 <a href="type.do?tid=${t.tid}&index=2&gname=${gname}">${t.tname}</a>

六.细节处理

6.1 效果预览

6.2代码实现

<!--查询未果  -->
      <c:if test="${empty goodsls}">
            <div class="h2" >暂未查找到"<span class="text-danger">${str}</span>"相关的宝贝~<a href="type.do?index=2">点我刷新喔~</a>
</div>
</c:if>


相关文章
|
9天前
|
关系型数据库 MySQL 数据库连接
Unity连接Mysql数据库 增 删 改 查
在 Unity 中连接 MySQL 数据库,需使用 MySQL Connector/NET 作为数据库连接驱动,通过提供服务器地址、端口、用户名和密码等信息建立 TCP/IP 连接。代码示例展示了如何创建连接对象并执行增删改查操作,确保数据交互的实现。测试代码中,通过 `MySqlConnection` 类连接数据库,并使用 `MySqlCommand` 执行 SQL 语句,实现数据的查询、插入、删除和更新功能。
|
23天前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
|
1天前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
40 25
|
28天前
|
缓存 NoSQL JavaScript
Vue.js应用结合Redis数据库:实践与优化
将Vue.js应用与Redis结合,可以实现高效的数据管理和快速响应的用户体验。通过合理的实践步骤和优化策略,可以充分发挥两者的优势,提高应用的性能和可靠性。希望本文能为您在实际开发中提供有价值的参考。
55 11
|
28天前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
52 2
|
2月前
|
人工智能 容灾 关系型数据库
【AI应用启航workshop】构建高可用数据库、拥抱AI智能问数
12月25日(周三)14:00-16:30参与线上闭门会,阿里云诚邀您一同开启AI应用实践之旅!
|
2月前
|
XML 前端开发 安全
Spring MVC:深入理解与应用实践
Spring MVC是Spring框架提供的一个用于构建Web应用程序的Model-View-Controller(MVC)实现。它通过分离业务逻辑、数据、显示来组织代码,使得Web应用程序的开发变得更加简洁和高效。本文将从概述、功能点、背景、业务点、底层原理等多个方面深入剖析Spring MVC,并通过多个Java示例展示其应用实践,同时指出对应实践的优缺点。
127 2
|
3月前
|
JSON JavaScript 关系型数据库
node.js连接GBase 8a 数据库 并进行查询代码示例
node.js连接GBase 8a 数据库 并进行查询代码示例
|
10天前
|
关系型数据库 MySQL 数据库
Docker Compose V2 安装常用数据库MySQL+Mongo
以上内容涵盖了使用 Docker Compose 安装和管理 MySQL 和 MongoDB 的详细步骤,希望对您有所帮助。
82 42
|
28天前
|
缓存 关系型数据库 MySQL
【深入了解MySQL】优化查询性能与数据库设计的深度总结
本文详细介绍了MySQL查询优化和数据库设计技巧,涵盖基础优化、高级技巧及性能监控。
222 0