mybatis动态条件查询和翻页查询的例子

接上篇文章:http://blog.csdn.net/rishengcsdn/article/details/39206993


继续测试动态查询的语句和翻页查询语句。


Test3.java源码:


package domain;  
      
import java.io.IOException;  
import java.io.Reader;  
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;  
import org.apache.ibatis.session.SqlSession;  
import org.apache.ibatis.session.SqlSessionFactory;  
import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
import com.springdemo.usermgr.vo.SUser;


      
    public class Test3 {  
        public static void main(String[] args) throws IOException {  
            String resource = "config.xml";  
            Reader reader = Resources.getResourceAsReader(resource);  
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);  
            SqlSession session = ssf.openSession(false); //true 为自动提交事务

            try {  
                
                SUser condi=new SUser();
                condi.setUserName("中文名zhou");
                condi.setPwd("y");        //模糊条件
                List<SUser> as=session.selectList("dynamicWhereTest", condi);

                System.out.println("查询结果:"+as.size());
                Map<String, Object> parms = new HashMap<String, Object>();  
         //       parms.put("pwd", "x");  
                parms.put("stanum", 2); //从第2条纪录起,查出4条记录,不含第2条
                parms.put("offset", 4);
                
                List<SUser> as2=session.selectList("dynamicWherePage", parms);
                
                System.out.println("翻页结果:"+as2.size());
                session.commit(true);
            } catch (Exception e) {
                session.rollback(true);
                e.printStackTrace();  
            } finally {  
                session.close();  
            }  
        }  
    } 


修改SUser.xml,增加翻页查询和动态条件查询

<?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="com.springdemo.usermgr.vo.SUserMapper">  
    <select id="selectSUser" parameterType="int" resultType="SUser">  
        select * from Suser where id = #{id}  
   </select>
    <select id="getSUser" parameterType="String" resultType="SUser">  
        select * from Suser where username = #{name}  
   </select>
     <insert id="insertSUser" parameterType="SUser">
      <selectKey resultType="int" keyProperty="id">
      SELECT LAST_INSERT_ID()
      </selectKey>
      insert into
      suser(userName,pwd,signUpTime)values(#{userName},#{pwd},#{signUpTime})
    </insert>    
    <select id="dynamicWhereTest" parameterType="SUser" resultType="SUser">  
        select * from Suser   
        <where>  
            <if test="userName != null">  
                userName = #{userName}  
            </if>  
            <if test="pwd != null">  
                and pwd LIKE CONCAT('%', CONCAT(#{pwd}, '%'))
            </if>  

        </where>  
    </select>  
    <select id="dynamicWherePage" parameterType="java.util.Map" resultType="SUser">  
        select * from Suser   
        <where>  
            <if test="pwd != null">  
                and pwd LIKE CONCAT('%', CONCAT(#{pwd}, '%'))
            </if>  

        </where>  
        limit #{stanum},#{offset}
    </select>         
</mapper> 

数据库,suser表记录也增加一些记录,便于测试查询结果:


mybatis动态条件查询和翻页查询的例子_第1张图片


运行test3,可能的结果为:

查询结果:3
翻页结果:4



你可能感兴趣的:(mybatis)