分页插件_mybatis注解开发_过滤器

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();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis PageHelper 是一个 MyBatis 分页插件,能够快速、便捷的进行分页查询,支持多种数据库。使用 PageHelper 可以避免手写 SQL 语句进行分页操作,同时 PageHelper 支持物理分页和逻辑分页两种方式。 下面是使用 PageHelper 进行分页查询的步骤: 1. 导入 PageHelper 依赖 Maven 项目在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency> ``` 2. 配置 PageHelper 在 MyBatis 的配置文件中添加以下配置: ``` <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="dialect" value="mysql"/> </plugin> </plugins> ``` 其中 dialect 属性指定了数据库类型,PageHelper 支持的数据库类型包括:oracle、mysql、mariadb、sqlite、hsqldb、postgresql、db2、sqlserver、informix、达梦、人大金仓、南大通用、神通、PostgreSQL9.3-9.5。 3. 使用 PageHelper 进行分页查询 在需要进行分页查询的方法中使用 PageHelper.startPage 方法进行分页设置,然后调用查询方法获取查询结果。例如: ``` PageHelper.startPage(1, 10); // 第一页,每页显示 10 条记录 List<User> userList = userDao.selectUserList(); // 查询用户列表 PageInfo<User> pageInfo = new PageInfo<>(userList); // 封装分页结果 ``` 其中 PageHelper.startPage 方法接收两个参数,第一个参数为当前页码,第二个参数为每页显示的记录数。 最后使用 PageInfo 类对查询结果进行封装,得到分页结果。PageInfo 类中包含了分页信息和查询结果。 以上就是使用 MyBatis PageHelper 进行分页查询的基本步骤。需要注意的是,在使用 PageHelper 进行分页查询时,需要确保查询语句中不要使用 limit 关键字。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值