修改动态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.效果如下