mybatis框架oracle和mysql数据分页查询语句

第一种: 用mybatis分页查询(oracle)这种一定要注意特殊符号,需要转换

    这是在网上查询到的资料,原路径:https://www.cnblogs.com/hq233/p/6753712.html

首先当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序错误。

  这样的问题在mBatiS中或者自定义的xml处理sql的程序中经常需要我们来处理。其实很简单,我们只需作如下替换即可避免上述的错误:

原符号 < <=  >>= &' "
替换符号&lt;&lt;=&gt;&gt;=&amp;&apos;&quot;
 

例如: from为从哪条记录开始,pageSize为每页显示的条数

<select id="getProductType2" resultType="com.crm.model.ProductType">
		select id,pid,fcode,code,name,remark,sort,create_time as createTime 
		from (select t.*, rownum rn
        	from (select *
                from b_product_type
               order by id asc )t 
        where rownum <= #{pageSize}) where 1=1 and rn>#{from} 
		<if test="productType.name!=null and productType.name!=''" >
			and name like concat('%',#{productType.name},'%')
		</if>
		<if test="productType.code!=null and productType.code!=''">
			and code like concat('%',#{productType.code},'%')
		</if>
		limit #{from},#{pageSize}
	</select>

第二种:在mysql数据库用mybatis如何和进行分页

    

<select id="getProductType2" resultType="com.crm.model.ProductType">
		select id,pid,fcode,code,name,remark,sort,create_time as createTime 
		from b_product_type where 1=1
		<if test="productType.name!=null and productType.name!=''" >
			and name like concat('%',#{productType.name},'%')
		</if>
		<if test="productType.code!=null and productType.code!=''">
			and code like concat('%',#{productType.code},'%')
		</if>
		limit #{from},#{pageSize}
	</select>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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 支持的数据库类型包括:oraclemysql、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、付费专栏及课程。

余额充值