学习目标
(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
;
private String cname
;
2 CategoryService
public class CategoryService {
public List
<Category> findAll() {
CategoryDao dao
= MySessionUtils2
.getMapper(CategoryDao
.class);
List
<Category> list
= dao
.findAll();
return list
;
}
}
3 CategoryDao
public interface CategoryDao {
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
<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
) {
if (data
.code
== 200) {
var list
= data
.data
;
var lis
= "";
lis
+= '<li class="nav-active"><a href="index.jsp">首页</a></li>'
for (var i
= 0; i
< list
.length
; i
++) {
var li
= '<li><a href="route_list.html">' + list
[i
].cname
+ '</a></li>'
lis
+= li
}
lis
+= ' <li><a href="favoriterank.html">收藏排行榜</a></li>'
$("#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
{
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
);
jedis
.set("category_list",json
);
response
.getWriter().println(json
);
}
JedisUtil
.close(jedis
);
}