Day18JavaWeb【Travel Project】Navigation***

learning target

  • (1) Data is the most important
  • (2) Develop one layer and test one layer
  • (3) Basics: Login

Navigation development

Insert picture description here

Navigation background code development

  • (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;
    }
}

Insert picture description here

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);
    }
}

Navigation front-end code development

  • (1)header.jsp
  • (2) Ajax sends a get request
  • (3) jquery for loop
  • (4) html() function modification interface

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>
  • In js, use quotation marks to represent strings to avoid generating translated \
  • String splicing variables in js ' + 变量名 + '

Navigation optimization

redis cache json

  • (1) Where to optimize? why?
    The classified data will be re-requested to load the database after each page load. The pressure on the database is relatively large , and the classified data will not change frequently , so redis can be used to cache this data.
  • (2) Redis caching process
    "Access redis to get data first
    " to judge the data
    If it is not null, return directly,
    otherwise call service to check the database
    Insert picture description here

redis use

  • redis server
    Insert picture description here

  • redis graphical tool
    Insert picture description here

  • JedisUtil tool reads and writes data
    directly to the project

  • jedis.properties

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

CategoryServlet uses 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);

    }

Guess you like

Origin blog.csdn.net/u013621398/article/details/108926032