Mybatis提供一个RowBounds类型,在查询指定RowBounds参数对象可以控制查询记录的起点和数量。
此案例在之前代码基础上执行:
http://blog.csdn.net/u013821825/article/details/51620371
1、SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/xsx/entity/EmpSQL.xml" />
</mappers>
</configuration>
2、实体类和其映射接口
package org.xsx.entity;
public class Emp {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package org.xsx.entity;
import java.util.List;
public interface EmpMapper{
public void addEmp(Emp emp);
public void updateEmp(Emp emp);
public Emp findById(int id);
public void deleteById(int id);
public List<Emp> findAll();
}
3、EmpSQL.xml配置wenjian
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="org.xsx.entity.EmpMapper">
<insert id="addEmp" parameterType="org.xsx.entity.Emp">
insert into emp (id,name,age)
values (#{id},#{name},#{age})
</insert>
<update id="updateEmp" parameterType="org.xsx.entity.Emp">
update emp set name=#{name},age=#{age} where id=#{id}
</update>
<select id="findById" parameterType="int" resultType="org.xsx.entity.Emp">
select name,age from emp where id=#{id}
</select>
<delete id="deleteById" parameterType="int">
delete from emp where id=#{id}
</delete>
<select id="findAll" resultType="org.xsx.entity.Emp">
select id,name,age from emp
</select>
</mapper>
4、测试方法
package org.xsx.test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.xsx.entity.Emp;
public class TestEmp extends MybatisHelper {
static{
conf = "SqlMapConfig.xml";
}
@Test
public void testDatasource() throws Exception{
String conf = "SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(conf);
//SessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(reader);
//Session
SqlSession session = factory.openSession();
System.out.println(session);
session.close();
}
@Test
public void testAddEmp() throws Exception{
// String conf = "SqlMapConfig.xml";
// Reader reader = Resources.getResourceAsReader(conf);
// //SessionFactoryBuilder对象
// SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// SqlSessionFactory factory = builder.build(reader);
//Session
SqlSession session = factory.openSession();
Emp emp = new Emp();
emp.setId(4);
emp.setName("da");
emp.setAge(91);
session.insert("addEmp", emp);
session.commit();
session.close();
}
@Test
public void testUpdate(){
//Session
SqlSession session = factory.openSession();
Emp emp = (Emp)session.selectOne("findById", 3);
emp.setId(3);
emp.setName("ccc");
emp.setAge(100);
session.update("updateEmp", emp);
session.commit();
session.close();
}
@Test
public void testFindById(){
//Session
SqlSession session = factory.openSession();
Emp emp = (Emp)session.selectOne("findById", 3);
System.out.println(emp.getName());
session.close();
}
@Test
public void testDeleteById(){
//Session
SqlSession session = factory.openSession();
session.delete("deleteById", 4);
session.commit();
session.close();
}
@Test
public void testRowBounds(){
//Session
SqlSession session = factory.openSession();
int offset = 0;//起点
int limit=3;//数量
RowBounds rowBounds = new RowBounds(offset, limit);
List<Emp> list = session.selectList("findAll", null, rowBounds);
for(Emp e:list){
System.out.println(e.getId()+"--"+e.getName()+"--"+e.getAge());
}
session.close();
}
}
5、静态加载类准备SqlSession资源
package org.xsx.test;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
public class MybatisHelper {
public static String conf ;
public static Reader reader;
public static SqlSessionFactoryBuilder builder;
public static SqlSessionFactory factory;
@Before
public void startMybatis() throws Exception{
reader = Resources.getResourceAsReader(conf);
builder = new SqlSessionFactoryBuilder();
factory = builder.build(reader);
}
}
6、结果