使用MyBatis(12)动态SQL 完成分页查询

修改动态SQL加上limit


1.和上一篇一样Student类不变

2.建立多条件查询的类

package com.yw.test12;

public class Condition
{
    private int id;
    private String name;
    private int start;
    private int count;
    
    public int getStart()
    {
        return start;
    }
    public void setStart(int start)
    {
        this.start = start;
    }
    public int getCount()
    {
        return count;
    }
    public void setCount(int count)
    {
        this.count = count;
    }
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    
}



3.配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="config.properties">
    </properties>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/> -->
        <mapper resource="com/yw/test12/StudentMapper.xml" />
    </mappers>

</configuration>



4.映射文件

<?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.yw.test12.StudentMapper">

    <select id="findStudent" resultType="com.yw.test12.Student" parameterType="com.yw.test12.Condition" >
        SELECT * FROM student
        
                    
        <if test="name !=null || name !='' " >
        where    name like #{name}
        </if>
        limit #{start},#{count}
        
    </select>

</mapper>



5.测试类

package com.yw.test12;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test01
{
    public static void main(String[] args) throws IOException
    {

        String resource = "com/yw/test12/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = sqlSessionFactory.openSession(false);
        try
        {
            System.out.println("=====动态SQL======");
            Condition s1=new Condition();
            s1.setName("%a%");
            s1.setStart(1);
            s1.setCount(2);
            
            
            List user = session.selectList("com.yw.test12.StudentMapper.findStudent", s1);
            System.out.println(user);

        }
        finally
        {
            session.close();
        }
    }
}

6.效果如下




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值