Day54
过滤器
Filter接口
@WebFilter(urlPatterns={"/list.html","/admin/order"})
public class LoginFilter{
init
destroy
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
参数:request---ServletRequest父接口/HttpServletRequest子接口===请求
response---ServletResponse父接口/HttpServletResponse子接口===响应
chain----过滤链;可以实现放行;
HttpSession session=req.getSessio();
Object obj=session.getAttribute("sys_user");
if(obj!=null && obj instanceof User){
//放行
return;//空return,结束方法;
}
//返回json
}
注册过滤器方式
web.xml--配置方式
注解方法:@WebFilter
注:添加过滤器拦截的路径; 例: '/list.html'如果请求 list.html,此时会进入到过滤器中再次进行登录状态的判断;
多表关联查询的两种方式
1,直接映射字段
第一种:
UserMapper.xml
<resultMap id="base"> 用户表--订单
<id column="" property=""/>
<result column="" property=""/>
...
<collection property="对多的属性-订单" ofType="泛型类型">订单属性和列字段映射
<id column="" property=""/>
<result column="" property=""/>
。。。
<collection property="对多的属性-订单明细" ofType="泛型类型">订单明细属性和列字段映射
<id column="" property=""/>
<result column="" property=""/>
。。。
</collection>
</collection>
</resultMap>
<select id="" resultMap="base">
select * from 用户表,订单表,订单明细表 where 等值条件 and uname='昵称' and upass='密码'
</select>
实体:
User用户实体
基本属性。。。
List<Orders> orders;//一个用户的多个订单,对多属性
Orders订单实体
基本属性。。。
List<OrderItems> items;//一个订单有多个订单明细;对多属性;
OrderItems订单明细实体
基本属性。。。。
2,通过名空间+id形式
第二种:
UserMapper.xml
<resultMap id="base"> 用户表--订单
<id column="" property=""/>
<result column="" property=""/>
...
</resultMap>
<select resultMap="base">
select * from sys_user where uname='昵称' and upass='密码'
</select>
<resultMap extends="base" id="sub">
<collection property="对多属性" select="名空间+查询语句的id" column="u_id"/>
</resultMap>
<select resultMap="sub">
select * from sys_user用户,sys_order订单 where 等值条件 and uname='昵称' and upass='密码'
</select>
OrderMapper.xml
<resultMap id="base"> 订单表
<id column="" property=""/>
<result column="" property=""/>
...
</resultMap>
<resultMap extends="base" id="sub">
<collection property="" select="" column="订单编号"/>
</resultMap>
<select id="" resultMap="base">
select * from sys_order where u_id=字段
</select>
OrderItemMapper.xml
<resultMap id="base"> 订单明细表
<id column="" property=""/>
<result column="" property=""/>
...
</resultMap>
<select>
select * from sys_item订单明细 where o_id='订单编号'
</select>
注解开发–mybatis
1,注解的声明
@Select("sql语句")
@Results(value={
@Result(id=true,column="列字段名",property="实体属性名"), //主键
@Result(column="列字段名",property="实体属性名") //非主键字段的映射 ,id=false默认
@Result(column="" ,property="",one = @One(select="全类名+方法名"))
//一对一时的属性
@Result(column="" ,property="",many = @Many(select="全类名+方法名"))
//一对多属性
})
@Insert("sql语句")
@Delete("sql语句")
@Update("sql语句")
mapper接口中的抽象方法上添加注解;
3,注解开发的接口注册
将接口注册在核心配置文件中:
<mappers>
<mapper class="接口全类名com.qf.mapper.UserMapper"/>
</mappers>
工具类
com.jlf.util自定义封装工具类
SYS---接口
静态常量属性;
STATUS--枚举类
public enum STATUS{
//固定实例
SUCCESS(1),FAILD(0),TUIHUO(2),TUIKUAN(3);
public STATUS(){}
private int code;
public STATUS(int code){
this.code=code;
}
}
断点调试
启动方式:debug
打断点:在程序执行过程中,在debug模式下可以停留在断点处;
向下一行/向下一个断点/进入方法/跳出方法;
在执行过程观察程序中的变量及程序的走势;
分页插件–mybatis
配置
在mybatis.xml中配置一下
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
添加依赖
在pom.xml中,添加依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
简单应用
PageHelper.startPage(页码,每页行数);
//查询操作--集合
PageInfo<实体> pageInfo=new PageInfo<>(上一步的查询结果);
如:
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pages=req.getParameter("page");//代表当前页码值
int page=pages==null?1:Integer.valueOf(pages);//如果没有page参数,默认第1页;否则。。。
PrintWriter pw = resp.getWriter();
//根据登录用户id查询所有订单;
HttpSession session=req.getSession();
Object obj = session.getAttribute(SYS.USER);
if(obj!=null && obj instanceof User){
User user=(User) obj;
//1设置当前页和每页行数
PageHelper.startPage(page,1);
//2查询操作
List<Orders> orders = orderService.selectOrderByUid(user.getUid());
//3PageInfo
PageInfo<Orders> ordersPageInfo = new PageInfo<>(orders);
pw.write(JSON.toJSONString(ordersPageInfo));//json串:java对象转为json串
pw.flush();
pw.close();
return;
}
pw.write("{\"code\":203}");//json串:java对象转为json串
pw.flush();
pw.close();
}