案例练习
今日内容:分类模块(categoryServlet) 旅游信息模块(routeServlet)
导航条功能
旅游信息分页展示
旅游信息搜索
涉及的表:tab_category 分类表 tab_route 旅游信息表
导航条功能
1 当用户访问首页的时候,页面的导航条上就交互完数据库
页面加载事件:$.ajax(...)
2 servlet端:直接调用service
3 service端:直接调用dao
4 dao端:编写一条查询的sql语句去tab_category做查询
select * from tab_category;
5 将查询出来的java的复杂数据转换成json数据
6 将json传递给ajax,再ajax的回调函数中:获取导航条的标签元素添加json的内容
<!-- 首页导航 -->
<script>
$(function(){
// 页面一加载就和服务器进行数据交互
var url="/ee92travel/category";
var data="methodName=findCategory";
$.post(url,data,function(d){
for(var i=0;i<d.length;i++){
$("#categoryUI").append("<li><a href=/ee92travel/route?methodName=findRoute&pageNumber=1&cid="+d[i].cid+">"+d[i].cname+"</a></li>");
}
},"json");
})
</script>
1 BaseServlet
public class BaseServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String methodName = request.getParameter("methodName");
Class clazz = this.getClass();
Method method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.setAccessible(true);
method.invoke(this,request,response);
}catch (Exception e){
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
2 CategoryServlet
public class CategoryServlet extends BaseServlet {
//查询导航条分类数据展示
private void findCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//不需要接受参数,直接调用service
CategoryService categoryService = new CategoryService();
String jsonValue = categoryService.findCategory();
System.out.println(jsonValue);
response.getWriter().print(jsonValue);
}
}
3 CategoryService
public class CategoryService {
public String findCategory() throws JsonProcessingException {
CategoryDao categoryDao = new CategoryDao();
List<Category> list = categoryDao.findCategory();
// 将list数据转换成json数据
ObjectMapper objectMapper = new ObjectMapper();
String jsonValue = objectMapper.writeValueAsString(list);
return jsonValue;
}
}
4 CategoryDao
public class CategoryDao implements ICategory {
private JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());
public List<Category> findCategory() {
String sql="select * from tab_category";
List<Category> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Category.class));
return list;
}
}
旅游信息分页展示
回顾分页
如果页面展示数据不需要分页: 直接返回查询的数据list即可
如果页面展示数据需要分页: 返回一个分页的封装体(pageBean)
pageBean封装体需要的数据:
1 页面要展示的内容 List
select * from tabb_route limit ?,5;
2 总条数 totalCount
3 总页数 totalPage
4 当前页 pageNumber
5 每页显示的条数 pageSize
起始数据: (当前页-1)*每页显示的条数
入口:第一页
1 当点击导航条的某个分类,要跳转到服务器servlet中
传递的数据:methodName pageNumber cid
2 servlet端:接受数据 调用service
3 servce端:组装分页的封装体(pageBean)
1 页面需要数据
select * from tab_route where cid=cid limit (pageNumber-1)*pageSize,pageSize;
2 总条数
select count(*) from tab_route where cid=cid;
3 总页数
通过总条数计算出来
4 当前页
pageNumber
5 每页显示的条数
固定的(5)
4 将封装好的pagebean放入域中,带到route_list.jsp展示
5 在route_list.jsp页面中,使用el和jstl将进行数据的遍历展示
- RouteServlet
package cn.itcast.web;
import cn.itcast.domain.PageBean;
import cn.itcast.service.RouteService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RouteServlet extends BaseServlet {
//查询导航条分类的数据
// 全查 list<5条全查国内游的数据>==route_list.jsp分页展示
// 全查list<5条全查国内游的数据>+条件查list<5条国内游的双飞数据>==route_list.jsp分页展示
// 适用于:不止可以查分类的(从导航条分类) 还可以查分类内容的(分类中做查询) 还可以查所有分类内容的(首页)
/*private void findRoute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1 获取当前页
int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
// 2 设置每页显示的条数
int pageSize=5;
// 3 获取点击分类的cid
String cid = request.getParameter("cid"); //5 2
//4 带着数据调用service
RouteService routeService = new RouteService();
PageBean pb = routeService.findRoute(pageNumber, pageSize, cid);
// 5 将封装好的分页数据放入域中 带到jsp页面展示
request.setAttribute("pb",pb);
request.setAttribute("cid",cid);
// list-->全查的 按条件查的
request.getRequestDispatcher("/route_list.jsp").forward(request,response);
}*/
// 点击导航条的某个分类过来的
// 点击某个分类中的搜索过来的
// 点击首页中的搜索过来的
private void findRoute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//0 接受rname
String rname = request.getParameter("rname"); //null 双飞 // 双飞
// 1 获取当前页
int pageNumber = Integer.parseInt(request.getParameter("pageNumber")); //1 //1 //1
// 2 设置每页显示的条数
int pageSize=5; //5 //5 //5
// 3 获取点击分类的cid
String cid = request.getParameter("cid"); //5 //5 //null
//4 带着数据调用service
RouteService routeService = new RouteService();
// 从导航条分类过来的: 1 5 5 null
// 从分类中的搜索过来的:1 5 5 双飞
// 从首页搜索过来的: 1 5 null 双飞
PageBean pb = routeService.findRoute(pageNumber, pageSize, cid,rname);
// 5 将封装好的分页数据放入域中 带到jsp页面展示
request.setAttribute("pb",pb);
request.setAttribute("cid",cid);
request.setAttribute("rname",rname);
// list-->全查的 按条件查的
request.getRequestDispatcher("/route_list.jsp").forward(request,response);
}
}
2 RouteService
package cn.itcast.service;
import cn.itcast.dao.RouteDao;
import cn.itcast.domain.PageBean;
import cn.itcast.domain.Route;
import java.util.List;
public class RouteService {
// 封装分页的数据
/*public PageBean findRoute(int pageNumber, int pageSize, String cid) {
RouteDao routeDao = new RouteDao();
//1 页面要展示的内容 (数据库)
int start=(pageNumber-1)*pageSize;
List<Route> list = routeDao.findList(start, pageSize, cid);
//2 总条数 (数据库)
int totalCount=routeDao.findCount(cid);
//3 总页数 (计算)
int totalPage=0;
if(totalCount%pageSize==0){
totalPage=totalCount/pageSize;
}else{
totalPage=totalCount/pageSize+1;
}
//4 当前页 (参数)
//5 每页显示的条数(参数)
// 准备分页数据的封装体(pageBean)
PageBean pb = new PageBean();
pb.setList(list);
pb.setTotalCount(totalCount);
pb.setTotalPage(totalPage);
pb.setPageNumber(pageNumber);
pb.setPageSize(pageSize);
return pb;
}*/
public PageBean findRoute(int pageNumber, int pageSize, String cid,String rname) {
RouteDao routeDao = new RouteDao();
//1 页面要展示的内容 (数据库)
int start=(pageNumber-1)*pageSize;
List<Route> list = routeDao.findList(start, pageSize, cid,rname);
//2 总条数 (数据库)
int totalCount=routeDao.findCount(cid,rname);
//3 总页数 (计算)
int totalPage=0;
if(totalCount%pageSize==0){
totalPage=totalCount/pageSize;
}else{
totalPage=totalCount/pageSize+1;
}
//4 当前页 (参数)
//5 每页显示的条数(参数)
// 准备分页数据的封装体(pageBean)
PageBean pb = new PageBean();
pb.setList(list);
pb.setTotalCount(totalCount);
pb.setTotalPage(totalPage);
pb.setPageNumber(pageNumber);
pb.setPageSize(pageSize);
return pb;
}
}
3 RouteDao
package cn.itcast.dao;
import cn.itcast.domain.Route;
import cn.itcast.utils.JdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.List;
public class RouteDao {
private JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());
/*public List<Route> findList(int start, int pageSize, String cid) {
String sql="select * from tab_route where cid=? limit ?,?";
List<Route> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Route.class), cid, start, pageSize);
return list;
}*/
// 从导航条分类过来的: (1-1)*5 5 5 null
// 从分类中的搜索过来的: (1-1)*5 5 5 双飞
// 从首页搜索过来的: (1-1)*5 5 null 双飞
public List<Route> findList(int start, int pageSize, String cid,String rname) {
String sql="select * from tab_route where 1=1";
List list = new ArrayList();
if(cid!=null && cid.trim().length()>0){
sql=sql+" and cid=?";
list.add(cid);
}
if(rname!=null && rname.trim().length()>0){
sql=sql+" and rname like ?";
list.add("%"+rname+"%");
}
sql=sql+" limit ?,?";
list.add(start);
list.add(pageSize);
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Route.class), list.toArray());
}
// public int findCount(String cid) {
// String sql="select count(*) from tab_route where cid=?";
// return jdbcTemplate.queryForObject(sql,int.class,cid);
// }
public int findCount(String cid,String rname) {
String sql="select count(*) from tab_route where 1=1";
List list = new ArrayList();
if(cid!=null && cid.trim().length()>0){
sql=sql+" and cid=?";
list.add(cid);
}
if(rname!=null && rname.trim().length()>0){
sql=sql+" and rname like ?";
list.add("%"+rname+"%");
}
return jdbcTemplate.queryForObject(sql,int.class,list.toArray());
}
}
旅游信息搜索 (关注点:dao层)
1 如果是在某个分类下搜索,只能搜索到该分类下的内容
2 如果是在首页点击的搜索,所有分类的内容都要出来
3 将以前的全查方法改造成全查+条件查
首页中的查询:list=select * from tab_route where rname like ? limit ?,? 5条
点击某个分类查询:list=select * from tab_route where cid=? limit ?,? 5条
某个分类中做查询: list=select * from tab_route where rname like ? and cid=? limit ?,? 5条
list==route.jsp==分页展示
概述
在程序中我们一直在使用实例化对象的方式(new 对象),这样的方式到底好不好呢?
答案是当然没有任何问题,写出来的代码清晰,但是有一个问题就是,如果以后需要修改,那么这样的方式是及其麻烦的。如果我们在项目的多处地方中都实例化过该对象,那么想修改成实例化其他对象的时候,带来的工作量也就不言而喻了。
那么,问题产生了,我们应该如何解决呢?由硬编码(写死的编码)修改成可灵活动态变化的代码,让程序自己决
定创建的对象会不会更好呢?
解决技术: 配置文件+反射+工厂 (IOC)
解决问题:可以不修改任何源码的情况下,实现多套实例对象功能
1 Iuser接口
public interface Iuser {
public void add();
public void delete();
public void update();
public void find();
}
2 Factory
// 工厂类
public class Factory {
public static Object getbean(String key) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
// 加载配置文件 编写反射代码创建实例对象 返回
//ResourceBundle:专门用来加载后缀名为.properties的文件
// 自动去src下加载后缀名为.properties的文件
ResourceBundle rb = ResourceBundle.getBundle("bean");
String value = rb.getString(key);
//反射
Class clazz = Class.forName(value);
return clazz.newInstance(); //User1
}
}
3 Demo
package cn.itcast.ioc;
import org.junit.jupiter.api.Test;
// 可以不修改任何源码的情况下,实现多套实例对象功能(user1 user2 user3)
// 工厂模式:专门有一类,这个类专门用来生产对象的
// 工厂:解决部分不需要频繁修改源码的问题 但是工厂要不停的去新修改
// 工厂+配置文件+反射 (ioc的底层)
public class Demo {
@Test
public void test1() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Iuser user= (Iuser) Factory.getbean("iuser");
user.add();
}
@Test
public void test2() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Iuser user= (Iuser) Factory.getbean("iuser");
user.find();
}
@Test
public void test3() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Iuser user= (Iuser) Factory.getbean("iuser");
user.delete();
}
@Test
public void test4() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Iuser user= (Iuser) Factory.getbean("iuser");
user.update();
}
}
4 bean.properties
#key(唯一) #value(类的全限定名)
iuser=cn.itcast.ioc.User3
2997

被折叠的 条评论
为什么被折叠?



