Day18JavaWeb【旅游项目】导航***

学习目标

  • (1)数据是最重要的
  • (2)开发一层测试一层
  • (3)基础:登录

导航开发

在这里插入图片描述

导航后台代码开发

  • (1)CateogryServiceTest
  • (2)CateogryService
  • (3)CateogryDao
  • (4)CateogryDaoTest
  • (5)CateogryServlet

1 CategoryServiceTest

public class TestCategoryService {
    
    
    @Test
    public void test01(){
    
    
        //创建业务对象
        CategoryService categoryService = new CategoryService();
        //所有的分类
        List<Category> categoryList=categoryService.findAll();
        //显示
        System.out.println(categoryList);
    }
}

Category

public class Category {
    
    
    private int cid;//分类id
    private String cname;//分类名称

2 CategoryService

public class CategoryService {
    
    
    public List<Category> findAll() {
    
    
        //调用dao
        CategoryDao dao = MySessionUtils2.getMapper(CategoryDao.class);
        //查询所有的分类数据
        List<Category> list =  dao.findAll();
        return list;
    }
}

在这里插入图片描述

3 CategoryDao

public interface CategoryDao {
    
    
    //select * from tab_category order by cid asc;
    List<Category> findAll();
}

com\wzx\dao\CategoryDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wzx.dao.CategoryDao">

    <select id="findAll" resultType="category">
        select * from tab_category order by cid asc;
    </select>

</mapper>

SqlMapConfig.xml

<!--   一个mapper标签可以指定一个映射文件-->
    <mappers>
        <mapper resource="com/wzx/dao/UserDao.xml"/>
        <mapper resource="com/wzx/dao/CategoryDao.xml"/>
    </mappers>

3 CategoryServlet

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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //创建业务对象
        CategoryService categoryService = new CategoryService();
        //所有的分类
        List<Category> categoryList=categoryService.findAll();
   
      //显示
        ResponseInfo info = new ResponseInfo();
        info.setCode(200);
        info.setData(categoryList);
        String json = new ObjectMapper().writeValueAsString(info);
        response.getWriter().println(json);
    }
}

导航前台代码开发

  • (1)header.jsp
  • (2)ajax发送get请求
  • (3)jquery的for循环
  • (4)html()函数修改界面

header.jsp

<script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
    
    
        //页面加载成功之后执行
        $.get(
            "categoryServlet",
            function (data) {
    
     //这个data就是服务器返回的字符串
                if (data.code == 200) {
    
    
                    var list = data.data;
                    //定义一个变量,拼接导航的显示内容  li
                    var lis = "";
                    //拼接第一项
                    lis += '<li class="nav-active"><a href="index.jsp">首页</a></li>'
                    //循环所有的分类
                    for (var i = 0; i < list.length; i++) {
    
    
                        //在js中,使用引号号表示字符串,避免生成转译的\
                        var li = '<li><a href="route_list.html">' + list[i].cname + '</a></li>'
                        lis += li
                    }

                    //拼接最后一项
                    lis += ' <li><a href="favoriterank.html">收藏排行榜</a></li>'
                    //显示在ul标签里面
                    $("#nav").html(lis)
                }
            },
            "json"
        );
    })
</script>
  • 在js中,使用引号号表示字符串,避免生成转译的\
  • 在js中的字符串拼接变量 ' + 变量名 + '

导航优化

redis缓存json

  • (1)哪个地方要优化?为什么?
    分类的数据在每一次页面加载后都会重新请求数据库来加载,对数据库的压力比较大,而且分类的数据不会经常产生变化,所有可以使用redis来缓存这个数据。
  • (2)redis缓存的流程
    》先访问redis获取数据
    》对数据进行判断
    如果不为null,则直接返回
    否则调用service查数据库
    在这里插入图片描述

redis使用

  • redis服务器
    在这里插入图片描述

  • redis图形化工具
    在这里插入图片描述

  • JedisUtil工具读写数据
    直接复到项目中

  • jedis.properties

host=192.168.21.101
port=6379
maxTotal=100
maxIdle=10

CategoryServlet使用redis


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    

        //访问redis 较快,但是第一次是没有数据的
        //访问service,获取json,将json保存到redis
        Jedis jedis = JedisUtil.getJedis();
        String json = jedis.get("category_list");

        if (json != null) {
    
    
            System.out.println("redis cache");
            response.getWriter().println(json);

        } else {
    
    
            System.out.println("mysql data");
            //创建业务对象
            CategoryService categoryService = new CategoryService();
            //所有的分类
            List<Category> categoryList=categoryService.findAll();
            //显示
            ResponseInfo info = new ResponseInfo();
            info.setCode(200);
            info.setData(categoryList);
            json = new ObjectMapper().writeValueAsString(info);
            //将数据保存到redis
            jedis.set("category_list",json);
            response.getWriter().println(json);
        }
        //关闭连接
        JedisUtil.close(jedis);

    }

猜你喜欢

转载自blog.csdn.net/u013621398/article/details/108926032