欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springmvc+mybatis 做分页sql 语句实例代码

程序员文章站 2023-11-24 12:24:58
废话不多说了,直接给大家贴代码了,具体代码如下所示: ...

废话不多说了,直接给大家贴代码了,具体代码如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper
public "-//mybatis.org//dtd mapper 3.0//en"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ssmy.dao.userdao">
 <resultmap type="ssmy.dto.user" id="user">
 <!--<resultmap type="user" id="user"> 如果在sprin文件里配置初始化 mybatis里配置了别名就是有-->
 <!-- 用id属性来映射主键字段 -->
 <id property="id" column="id" jdbctype="integer"/>
 <!-- 用result属性来映射非主键字段 -->
 <result property="username" column="username" jdbctype="varchar"/>
 <result property="password" column="password" jdbctype="varchar"/>
 <result property="truename" column="truename" jdbctype="varchar"/>
 <result property="email" column="email" jdbctype="varchar"/>
 <result property="phone" column="phone" jdbctype="varchar"/>
 <result property="rolename" column="rolename" jdbctype="varchar"/> 
 </resultmap>
 <!--分页返回类型list 可以使用map user对应的是resultmap size每页的大小-->
 <select id="find" resultmap="user" parametertype="map">
 select t2.* from 
 ( select t1.*,rownum rn from t_user t1 
 <where> 
 <if test ="username !=null and username !='' ">
 t1.username like '%'||#{username,jdbctype=varchar}||'%'
 </if>
 </where>
 ) t2
 <where>
 <if test ="start !=null and start !=''">
 <![cdata[and t2.rn >=#{start}]]>
 </if>
 <if test ="size !=null and size !=''">
 and <![cdata[t2.rn <=#{size}]]>
 </if>
 </where>
 </select>
 <!--获取总记录数 -->
 <select id="gettotal" parametertype="map" resulttype="java.lang.integer">
 select count(1) from t_user
 <where> 
 <if test ="username !=null and username !='' ">
 username like '%'||#{username,jdbctype=varchar}||'%'
 </if>
 </where>
 </select>
 <!--<insert id="createser" parametertype="user">
 insert into news_user (id,username,password,email,usertype)
 values (#{id,jdbctype=numeric},#{username,jdbctype=varchar},
 #{password,jdbctype=varchar},#{email,jdbctype=varchar},1) 
 <selectkey resulttype="int" order="before" keyproperty="id"> 
 select seq_id.nextval from dual 
 </selectkey>
 </insert>-->
</mapper>

以上所述是小编给大家介绍的springmvc+mybatis 做分页sql 语句实例代码,希望对大家有所帮助