CMS-day05

CMS-Day05

一、高级查询

1、拼接SQL

  • 1 高级查询就是在查询基础上添加后面的where条件
  • 2 根据不同的情况,where后面的条件都不同的
  • 3 提供的过滤条件越多,我们查询的排列组合也就越多
/*//拼接方式第一种,where 1=1,会影响SQL查询性能
	public String creatWhereSql(){
		String whereSql=" where 1=1 ";
		//title不为null,且不是空字符串
		if (title!=null&&!"".equals(title)) {
			whereSql +=" and title like '%"+title+"%' ";
		}if (positiontype!=null) {
			whereSql +=" and positiontype = "+positiontype;
		}
		return whereSql;
	}*/
	
	/*//方法二:准备一个标识,true加where、false加and
	public String creatWhereSql(){
		boolean flag=true;
		String whereSql="";
		//title不为null,且不是空字符串
		if (title!=null&&!"".equals(title)) {
			if (flag) {
				whereSql +=" where ";
				flag=false;
			} else {
				whereSql +=" and ";
			}
			whereSql +=" title like '%"+title+"%' ";
		}if (positiontype!=null) {
		if (flag) {
				whereSql +=" where ";
				flag=false;
			} else {
				whereSql +=" and ";
			whereSql +=" and positiontype = "+positiontype;
		}
		return whereSql;
	}*/
	
	
	/*//方法三:把SQL存在一个集合中
	public String creatWhereSql(){
		List sqlList = new ArrayList();
		String whereSql="";
		//title不为null,且不是空字符串
		if (title!=null&&!"".equals(title)) {
			sqlList.add(" title like '%"+title+"%' ");
		}if (positiontype!=null) {
			sqlList.add(" positiontype = "+positiontype);
		}
		for (int i = 0; i < sqlList.size(); i++) {
			if (i==0) {
				whereSql +=" where ";
			} else {
				whereSql +=" and ";
			}
			whereSql +=sqlList.get(i);
		}
		return whereSql;
	}*/
	
	//方法四
	public String creatWhereSql(){
		String whereSql="";
		//title不为null,且不是空字符串
		if (title!=null&&!"".equals(title)) {
			whereSql +=" and title like '%"+title+"%' ";
		}if (positiontype!=null) {
			whereSql +=" and positiontype = "+positiontype;
		}
		return whereSql.replaceFirst("and", "where");
	}
  • 在分页的方法中的SQL语句中加上条件

二、高级查询+分页

  • 高级查询在点击分页时会使高级查询失效
    1、在提交高级查询的form表单中添加一个
<input type="hidden" name="currentPage" id="currentPage" value="1"/>

2、然后写个方法,使每次点击分页功能的按钮都会将当前页的值传到form表单中并提交

<script type="text/javascript">
			function goPage(page){
				//把当前要调到几页的值放到表单中去
				document.getElementById("currentPage").value=page;
				//提交表单
				document.getElementById("searchform").submit();
			}
		</script>

三、登录

  • 主要靠拦截器,将输入的信息存到session中,拿出来比较看是否存在决定是否放行
    1、在LoginController中根据用户名与密码到后台拿到用户保存到session中
...
@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String name,String password,HttpSession session){
		//根据用户名与密码到后台拿到用户
		User loginUser = userService.login(name, password);
		//用户存在,登陆成功。保存到session中
		if (loginUser != null && loginUser.isIsenabled()) {
			session.setAttribute("loginUser", loginUser);
			return "redirect:/system/index";
		}
		//用户不存在,登录失败
		return "forward:/login.jsp";
	}
...

2、写一个类来实现SpringMVC中准备的HandlerInterceptor接口,覆写preHandle方法,从session中拿到数据进行业务判断

...
//在执行方法之前进行拦截
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object obj) throws Exception {
		//从session中拿到数据
		HttpSession session = req.getSession();
		User loginUser = (User)session.getAttribute("loginUser");
		//如果用户不存在跳到主页
		if (loginUser == null) {
			resp.sendRedirect("/login");
			return false;
		}
		//存在放行
		return true;
	}
...

3、在applicationContext-mvc.xml中配置

...
<!-- 配置拦截器:interceptors 可以配置多个拦截器-->
	<mvc:interceptors>
		<mvc:interceptor>
			<!--你要拦截的是哪些请求 -->
			<mvc:mapping path="/**"/>
			<!--配置你不拦截的请求  -->
			<mvc:exclude-mapping path="/login"/>
			<mvc:exclude-mapping path="/assets/**"/>
			<!-- 所有请求都会经过这个拦截器 -->
			<bean class="cn.itsource.cms.web.interceptor.LoginInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors>
...
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值