Mybatis实现数据库分页查询
2016-06-09 13:40
501 查看
Mybatis提供一个RowBounds类型,在查询指定RowBounds参数对象可以控制查询记录的起点和数量。
此案例在之前代码基础上执行:
http://blog.csdn.net/u013821825/article/details/51620371
1、SqlMapConfig.xml
2、实体类和其映射接口
3、EmpSQL.xml配置wenjian
4、测试方法
5、静态加载类准备SqlSession资源
6、结果
此案例在之前代码基础上执行:
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、结果
相关文章推荐
- more、less 和 most 的区别
- AJAX实现瀑布流触发分页与分页触发瀑布流的方法
- 十万条Access数据表分页的两个解决方法
- sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查询计划】
- 高效的mysql分页方法及原理
- asp又一个分页的代码例子
- SqlServer 2000、2005分页存储过程整理第1/3页
- JQuery的Pager分页器实现代码
- ADO存取数据库时如何分页显示
- 透彻掌握ASP分页技术很详细的分析
- jQuery实现的简单分页示例
- Android ListView分页功能实现方法
- 在ASP.NET 2.0中操作数据之二十四:分页和排序报表数据
- ajax无刷新分页的简单实现
- 一条SQL语句搞定Sql2000 分页
- 分页 SQLServer存储过程
- 实现SQL分页的存储过程代码
- sql分页查询几种写法
- SQL行号排序和分页(SQL查询中插入行号 自定义分页的另类实现)
- mysql 分页优化解析