mybatis物理分页的实现(转)

本文介绍了一个基于MyBatis的物理分页实现方案,包括实体类定义、DAO接口、映射文件、主配置文件等内容,并提供了一个测试类用于验证分页功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


转:http://dove19900520.iteye.com/blog/1724414

最近两周一直在学习mybatis,昨天经理让我将mybatis的物理分页整理一下,打成一个jar包供以后调用。结果昨天我整了一天,也没整出个1、2、3来。现在终于写出来了,先记录下来再说,哈哈。

下面是所有的代码:

Java代码 收藏代码
  1. packagecom.xxyd.mybatis.pojo;
  2. importjava.io.Serializable;
  3. /**
  4. *实体类
  5. *@authordove
  6. *
  7. */
  8. publicclassTestEntityimplementsSerializable{
  9. privatestaticfinallongserialVersionUID=-5849200248418883686L;
  10. privateintid;
  11. privateStringname;
  12. privateintno;
  13. privateintsex;
  14. privateintage;
  15. privateStringcount;
  16. privateStringschool;
  17. privateintweight;
  18. privateintheight;
  19. privateStringhabbit;
  20. privateStringmemo;
  21. publicintgetId(){
  22. returnid;
  23. }
  24. publicvoidsetId(intid){
  25. this.id=id;
  26. }
  27. publicStringgetName(){
  28. returnname;
  29. }
  30. publicvoidsetName(Stringname){
  31. this.name=name;
  32. }
  33. publicintgetNo(){
  34. returnno;
  35. }
  36. publicvoidsetNo(intno){
  37. this.no=no;
  38. }
  39. publicintgetSex(){
  40. returnsex;
  41. }
  42. publicvoidsetSex(intsex){
  43. this.sex=sex;
  44. }
  45. publicintgetAge(){
  46. returnage;
  47. }
  48. publicvoidsetAge(intage){
  49. this.age=age;
  50. }
  51. publicStringgetCount(){
  52. returncount;
  53. }
  54. publicvoidsetCount(Stringcount){
  55. this.count=count;
  56. }
  57. publicStringgetSchool(){
  58. returnschool;
  59. }
  60. publicvoidsetSchool(Stringschool){
  61. this.school=school;
  62. }
  63. publicintgetWeight(){
  64. returnweight;
  65. }
  66. publicvoidsetWeight(intweight){
  67. this.weight=weight;
  68. }
  69. publicintgetHeight(){
  70. returnheight;
  71. }
  72. publicvoidsetHeight(intheight){
  73. this.height=height;
  74. }
  75. publicStringgetHabbit(){
  76. returnhabbit;
  77. }
  78. publicvoidsetHabbit(Stringhabbit){
  79. this.habbit=habbit;
  80. }
  81. publicStringgetMemo(){
  82. returnmemo;
  83. }
  84. publicvoidsetMemo(Stringmemo){
  85. this.memo=memo;
  86. }
  87. @Override
  88. publicStringtoString(){
  89. return"TestEntity[id="+id+",name="+name+",no="+no
  90. +",sex="+sex+",age="+age+",count="+count
  91. +",school="+school+",weight="+weight+",height="
  92. +height+",habbit="+habbit+",memo="+memo+"]";
  93. }
  94. }

2、DAO接口

Java代码 收藏代码
  1. packagecom.xxyd.mybatis.dao;
  2. importjava.util.List;
  3. importorg.apache.ibatis.annotations.Param;
  4. importcom.xxyd.mybatis.pojo.TestEntity;
  5. /**
  6. *dao接口
  7. *@authordove
  8. *
  9. */
  10. publicinterfaceTestMapper{
  11. publicvoidcreateTestEntity(TestEntityentity);
  12. publicList<TestEntity>getTestEntityByPager(@Param("pageNo")intpageNo,@Param("pageSize")intpageSize);
  13. publicList<TestEntity>getListTestEntity();
  14. publicintgetTotalCount(@Param("pageNo")intpageNo,@Param("pageSize")intpageSize);
  15. publicvoidupdateTestEntity(TestEntityentity);
  16. publicvoiddeleteTestEntityById(@Param("id")intid);
  17. }

3、映射文件TestMapper.xml

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEmapperSYSTEM"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mappernamespace="com.xxyd.mybatis.dao.TestMapper">
  4. <resultMaptype="com.xxyd.mybatis.pojo.TestEntity"id="test_test">
  5. <idproperty="id"column="id"javaType="int"jdbcType="INTEGER"/>
  6. <resultproperty="name"column="name"javaType="String"jdbcType="VARCHAR"/>
  7. <resultproperty="no"column="no"javaType="int"jdbcType="INTEGER"/>
  8. <resultproperty="sex"column="sex"javaType="int"jdbcType="INTEGER"/>
  9. <resultproperty="age"column="age"javaType="int"jdbcType="INTEGER"/>
  10. <resultproperty="count"column="count"javaType="String"jdbcType="VARCHAR"/>
  11. <resultproperty="school"column="school"javaType="String"jdbcType="VARCHAR"/>
  12. <resultproperty="weight"column="weight"javaType="int"jdbcType="INTEGER"/>
  13. <resultproperty="height"column="height"javaType="int"jdbcType="INTEGER"/>
  14. <resultproperty="habbit"column="habbit"javaType="String"jdbcType="VARCHAR"/>
  15. <resultproperty="memo"column="memo"javaType="String"jdbcType="VARCHAR"/>
  16. </resultMap>
  17. <insertid="createTestEntity"useGeneratedKeys="true"parameterType="com.xxyd.mybatis.pojo.TestEntity">
  18. insertintotest_test(name,no,sex,age,count,school,weight,height,habbit,memo)
  19. values(#{name},#{no},#{sex},#{age},#{count},#{school},#{weight},#{height},#{habbit},#{memo});
  20. </insert>
  21. <selectid="getTestEntityByPager"resultMap="test_test">
  22. selectid,name,no,sex,age,count,school,weight,height,habbit,memo
  23. fromtest_test
  24. limit#{pageNo,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
  25. </select>
  26. <selectid="getListTestEntity"resultMap="test_test">
  27. selectid,name,no,sex,age,count,school,weight,height,habbit,memo
  28. fromtest_test
  29. </select>
  30. <selectid="getTotalCount"resultType="int">
  31. selectcount(sub.id)from
  32. (selecttest.idasidfromtest_testtest
  33. limit#{pageNo,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER})assub
  34. </select>
  35. <updateid="updateTestEntity"parameterType="com.xxyd.mybatis.pojo.TestEntity">
  36. updatetest_test
  37. <set>
  38. <iftest="name!=nullandname!=''">
  39. name=#{name,jdbcType=VARCHAR},
  40. </if>
  41. <iftest="no!=nullandno!=''">
  42. no=#{no,jdbcType=INTEGER},
  43. </if>
  44. <iftest="sex!=nullandsex!=''">
  45. sex=#{sex,jdbcType=INTEGER},
  46. </if>
  47. <iftest="age!=nullandage!=''">
  48. age=#{age,jdbcType=INTEGER},
  49. </if>
  50. <iftest="count!=nullandcount!=''">
  51. count=#{count,jdbcType=VARCHAR},
  52. </if>
  53. <iftest="school!=nullandschool!=''">
  54. school=#{school,jdbcType=VARCHAR},
  55. </if>
  56. <iftest="weight!=nullandweight!=''">
  57. weight=#{weight,jdbcType=INTEGER},
  58. </if>
  59. <iftest="height!=nullandheight!=''">
  60. height=#{height,jdbcType=INTEGER},
  61. </if>
  62. <iftest="habbit!=nullandhabbit!=''">
  63. habbit=#{habbit,jdbcType=VARCHAR},
  64. </if>
  65. <iftest="memo!=nullandmemo!=''">
  66. memo=#{memo,jdbcType=VARCHAR},
  67. </if>
  68. </set>
  69. whereid=#{id,jdbcType=INTEGER}
  70. </update>
  71. <deleteid="deleteTestEntityById"parameterType="int">
  72. deletefromtest_testwhereid=#{id}
  73. </delete>
  74. </mapper>

4、mybatis主配置文件mybatis-config.xml

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <!--配置数据库方言目前只有mysql和oracle两种-->
  5. <properties>
  6. <propertyname="dialect"value="mysql"/>
  7. </properties>
  8. <!--配置mysql分页拦截器start-->
  9. <!--com.xxyd.mybatis.interceptor.PaginationInterceptor来自于jar包mybatis-pager-1.0.0.jar-->
  10. <plugins>
  11. <plugininterceptor="com.xxyd.mybatis.interceptor.PaginationInterceptor"></plugin>
  12. </plugins>
  13. <!--映射文件-->
  14. <mappers>
  15. <mapperresource="com/xxyd/mybatis/mapper/TestMapper.xml"/>
  16. </mappers>
  17. </configuration>

6、jdbc.properties文件

Xml代码 收藏代码
  1. driverClass=com.mysql.jdbc.Driver
  2. url=jdbc\:mysql\://127.0.0.1\:3306/student_manager
  3. username=root
  4. password=123456

7、spring配置文件部分配置

Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/aop
  10. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  13. http://www.springframework.org/schema/context
  14. http://www.springframework.org/schema/context/spring-context.xsd">
  15. <!--Properties文件读取配置,base的properties-->
  16. <context:property-placeholderlocation="classpath:jdbc.properties"/>
  17. <beanid="dataSource"
  18. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  19. <propertyname="driverClassName"value="${driverClass}"/>
  20. <propertyname="url"value="${url}"/>
  21. <propertyname="username"value="${username}"></property>
  22. <propertyname="password"value="${password}"></property>
  23. </bean>
  24. <beanid="transactionManager"
  25. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  26. <propertyname="dataSource"ref="dataSource"/>
  27. </bean>
  28. <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
  29. <propertyname="configLocation"value="classpath:mybatis-config.xml"/>
  30. <propertyname="dataSource"ref="dataSource"/>
  31. </bean>
  32. <!--测试用例start-->
  33. <beanid="TestMapperTest"class="com.xxyd.mybatis.test.TestMapperTest">
  34. <propertyname="sessionFactory"ref="sqlSessionFactory"/>
  35. </bean>
  36. <!--测试用例end-->
  37. <!--mapperbean-->
  38. <beanid="TestMapper"class="org.mybatis.spring.mapper.MapperFactoryBean">
  39. <propertyname="mapperInterface"value="com.xxyd.mybatis.dao.TestMapper"/>
  40. <propertyname="sqlSessionFactory"ref="sqlSessionFactory"/>
  41. </bean>
  42. </beans>

7、最后,测试类:

Java代码 收藏代码
  1. packagecom.xxyd.mybatis.test;
  2. importjava.util.List;
  3. importorg.apache.ibatis.session.RowBounds;
  4. importorg.apache.ibatis.session.SqlSession;
  5. importorg.apache.ibatis.session.SqlSessionFactory;
  6. importorg.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. importorg.junit.Test;
  8. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  9. importcom.xxyd.mybatis.pojo.TestEntity;
  10. publicclassTestMapperTest{
  11. //privateSqlSessionFactorysessionFactory;
  12. privatestaticSqlSessionFactoryBuilderbuilder;
  13. privatestaticSqlSessionFactorysessionFactory;
  14. static{
  15. builder=newSqlSessionFactoryBuilder();
  16. sessionFactory=builder.build(Thread.currentThread()
  17. .getContextClassLoader()
  18. .getResourceAsStream("mybatis-config.xml"));
  19. }
  20. /**
  21. *@paramargs
  22. */
  23. publicstaticvoidmain(String[]args){
  24. ClassPathXmlApplicationContextctx=newClassPathXmlApplicationContext("applicationContext.xml");
  25. TestMapperTestTestMapperTest=(TestMapperTest)ctx.getBean("TestMapperTest");
  26. TestMapperTest.getList();
  27. }
  28. @Test
  29. publicvoidgetList(){
  30. SqlSessionsqlSession=sessionFactory.openSession();
  31. //TestEntityentity=newTestEntity();
  32. List<TestEntity>list=sqlSession.selectList("com.xxyd.mybatis.dao.TestMapper.getListTestEntity",TestEntity.class,newRowBounds(0,200));
  33. sqlSession.commit();
  34. sqlSession.close();
  35. for(TestEntitytestEntity:list){
  36. System.out.println(testEntity.toString());
  37. }
  38. }
  39. //publicSqlSessionFactorygetSessionFactory(){
  40. //returnsessionFactory;
  41. //}
  42. //publicvoidsetSessionFactory(SqlSessionFactorysessionFactory){
  43. //this.sessionFactory=sessionFactory;
  44. //}
  45. }

能够成功运行的前提是,你已经将mybatis+spring的运行环境搭建好了,并且将mybatis-pager-1.0.0.jar也放置到classpath下。之后,直接运行测试类就可以了。

注意1:mybatis-pager-1.0.0.jar该包就是浪费我昨天一整天也没整出来的分页jar包,里面目前只适用于mysql和oracle两种数据库使用,其他的数据库还在研究中。

注意2:List<TestEntity> list = sqlSession.selectList("com.xxyd.mybatis.dao.TestMapper.getListTestEntity", TestEntity.class, new RowBounds(0, 200));
第一个参数标准写法是TestMapper.xml的名称空间+select的对应id(建议sql映射文件中的sql语句结尾不要有分号);第二个参数是:实体类,当然也可以使字符串,只有是Object类型的就可以,目前还没发现什么异常,或许我研究的较为浅显的原因;第三个参数则是需要分页的数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值