设为首页 收藏本站
查看: 1141|回复: 0

[经验分享] mybatis物理分页的实现(转)

[复制链接]

尚未签到

发表于 2016-11-25 10:23:06 | 显示全部楼层 |阅读模式


  转:http://dove19900520.iyunv.com/blog/1724414
  最近两周一直在学习mybatis,昨天经理让我将mybatis的物理分页整理一下,打成一个jar包供以后调用。结果昨天我整了一天,也没整出个1、2、3来。现在终于写出来了,先记录下来再说,哈哈。
  下面是所有的代码:
Java代码 DSC0000.png


  • packagecom.xxyd.mybatis.pojo;

  • importjava.io.Serializable;
  • /**
  • *实体类
  • *@authordove
  • *
  • */
  • publicclassTestEntityimplementsSerializable{

  • privatestaticfinallongserialVersionUID=-5849200248418883686L;
  • privateintid;
  • privateStringname;
  • privateintno;
  • privateintsex;
  • privateintage;
  • privateStringcount;
  • privateStringschool;
  • privateintweight;
  • privateintheight;
  • privateStringhabbit;
  • privateStringmemo;
  • publicintgetId(){
  • returnid;
  • }
  • publicvoidsetId(intid){
  • this.id=id;
  • }
  • publicStringgetName(){
  • returnname;
  • }
  • publicvoidsetName(Stringname){
  • this.name=name;
  • }
  • publicintgetNo(){
  • returnno;
  • }
  • publicvoidsetNo(intno){
  • this.no=no;
  • }
  • publicintgetSex(){
  • returnsex;
  • }
  • publicvoidsetSex(intsex){
  • this.sex=sex;
  • }
  • publicintgetAge(){
  • returnage;
  • }
  • publicvoidsetAge(intage){
  • this.age=age;
  • }
  • publicStringgetCount(){
  • returncount;
  • }
  • publicvoidsetCount(Stringcount){
  • this.count=count;
  • }
  • publicStringgetSchool(){
  • returnschool;
  • }
  • publicvoidsetSchool(Stringschool){
  • this.school=school;
  • }
  • publicintgetWeight(){
  • returnweight;
  • }
  • publicvoidsetWeight(intweight){
  • this.weight=weight;
  • }
  • publicintgetHeight(){
  • returnheight;
  • }
  • publicvoidsetHeight(intheight){
  • this.height=height;
  • }
  • publicStringgetHabbit(){
  • returnhabbit;
  • }
  • publicvoidsetHabbit(Stringhabbit){
  • this.habbit=habbit;
  • }
  • publicStringgetMemo(){
  • returnmemo;
  • }
  • publicvoidsetMemo(Stringmemo){
  • this.memo=memo;
  • }
  • @Override
  • publicStringtoString(){
  • return"TestEntity[id="+id+",name="+name+",no="+no
  • +",sex="+sex+",age="+age+",count="+count
  • +",school="+school+",weight="+weight+",height="
  • +height+",habbit="+habbit+",memo="+memo+"]";
  • }

  • }

  2、DAO接口
Java代码


  • packagecom.xxyd.mybatis.dao;

  • importjava.util.List;

  • importorg.apache.ibatis.annotations.Param;

  • importcom.xxyd.mybatis.pojo.TestEntity;
  • /**
  • *dao接口
  • *@authordove
  • *
  • */
  • publicinterfaceTestMapper{
  • publicvoidcreateTestEntity(TestEntityentity);
  • publicList<TestEntity>getTestEntityByPager(@Param("pageNo")intpageNo,@Param("pageSize")intpageSize);
  • publicList<TestEntity>getListTestEntity();
  • publicintgetTotalCount(@Param("pageNo")intpageNo,@Param("pageSize")intpageSize);
  • publicvoidupdateTestEntity(TestEntityentity);
  • publicvoiddeleteTestEntityById(@Param("id")intid);
  • }

  3、映射文件TestMapper.xml
Xml代码


  • <?xmlversion="1.0"encoding="UTF-8"?>
  • <!DOCTYPEmapperSYSTEM"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  • <mappernamespace="com.xxyd.mybatis.dao.TestMapper">

  • <resultMaptype="com.xxyd.mybatis.pojo.TestEntity"id="test_test">
  • <idproperty="id"column="id"javaType="int"jdbcType="INTEGER"/>
  • <resultproperty="name"column="name"javaType="String"jdbcType="VARCHAR"/>
  • <resultproperty="no"column="no"javaType="int"jdbcType="INTEGER"/>
  • <resultproperty="sex"column="sex"javaType="int"jdbcType="INTEGER"/>
  • <resultproperty="age"column="age"javaType="int"jdbcType="INTEGER"/>
  • <resultproperty="count"column="count"javaType="String"jdbcType="VARCHAR"/>
  • <resultproperty="school"column="school"javaType="String"jdbcType="VARCHAR"/>
  • <resultproperty="weight"column="weight"javaType="int"jdbcType="INTEGER"/>
  • <resultproperty="height"column="height"javaType="int"jdbcType="INTEGER"/>
  • <resultproperty="habbit"column="habbit"javaType="String"jdbcType="VARCHAR"/>
  • <resultproperty="memo"column="memo"javaType="String"jdbcType="VARCHAR"/>
  • </resultMap>

  • <insertid="createTestEntity"useGeneratedKeys="true"parameterType="com.xxyd.mybatis.pojo.TestEntity">
  • insertintotest_test(name,no,sex,age,count,school,weight,height,habbit,memo)
  • values(#{name},#{no},#{sex},#{age},#{count},#{school},#{weight},#{height},#{habbit},#{memo});
  • </insert>

  • <selectid="getTestEntityByPager"resultMap="test_test">
  • selectid,name,no,sex,age,count,school,weight,height,habbit,memo
  • fromtest_test
  • limit#{pageNo,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
  • </select>

  • <selectid="getListTestEntity"resultMap="test_test">
  • selectid,name,no,sex,age,count,school,weight,height,habbit,memo
  • fromtest_test
  • </select>

  • <selectid="getTotalCount"resultType="int">
  • selectcount(sub.id)from
  • (selecttest.idasidfromtest_testtest
  • limit#{pageNo,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER})assub
  • </select>

  • <updateid="updateTestEntity"parameterType="com.xxyd.mybatis.pojo.TestEntity">
  • updatetest_test
  • <set>
  • <iftest="name!=nullandname!=''">
  • name=#{name,jdbcType=VARCHAR},
  • </if>
  • <iftest="no!=nullandno!=''">
  • no=#{no,jdbcType=INTEGER},
  • </if>
  • <iftest="sex!=nullandsex!=''">
  • sex=#{sex,jdbcType=INTEGER},
  • </if>
  • <iftest="age!=nullandage!=''">
  • age=#{age,jdbcType=INTEGER},
  • </if>
  • <iftest="count!=nullandcount!=''">
  • count=#{count,jdbcType=VARCHAR},
  • </if>
  • <iftest="school!=nullandschool!=''">
  • school=#{school,jdbcType=VARCHAR},
  • </if>
  • <iftest="weight!=nullandweight!=''">
  • weight=#{weight,jdbcType=INTEGER},
  • </if>
  • <iftest="height!=nullandheight!=''">
  • height=#{height,jdbcType=INTEGER},
  • </if>
  • <iftest="habbit!=nullandhabbit!=''">
  • habbit=#{habbit,jdbcType=VARCHAR},
  • </if>
  • <iftest="memo!=nullandmemo!=''">
  • memo=#{memo,jdbcType=VARCHAR},
  • </if>
  • </set>
  • whereid=#{id,jdbcType=INTEGER}
  • </update>

  • <deleteid="deleteTestEntityById"parameterType="int">
  • deletefromtest_testwhereid=#{id}
  • </delete>

  • </mapper>

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


  • <?xmlversion="1.0"encoding="UTF-8"?>
  • <!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
  • <configuration>
  • <!--配置数据库方言目前只有mysql和oracle两种-->
  • <properties>
  • <propertyname="dialect"value="mysql"/>
  • </properties>

  • <!--配置mysql分页拦截器start-->
  • <!--com.xxyd.mybatis.interceptor.PaginationInterceptor来自于jar包mybatis-pager-1.0.0.jar-->
  • <plugins>
  • <plugininterceptor="com.xxyd.mybatis.interceptor.PaginationInterceptor"></plugin>
  • </plugins>

  • <!--映射文件-->
  • <mappers>
  • <mapperresource="com/xxyd/mybatis/mapper/TestMapper.xml"/>
  • </mappers>
  • </configuration>


  6、jdbc.properties文件
Xml代码


  • driverClass=com.mysql.jdbc.Driver
  • url=jdbc\:mysql\://127.0.0.1\:3306/student_manager
  • username=root
  • password=123456

  7、spring配置文件部分配置
Xml代码


  • <?xmlversion="1.0"encoding="UTF-8"?>
  • <beansxmlns="http://www.springframework.org/schema/beans"
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
  • xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"
  • xmlns:context="http://www.springframework.org/schema/context"
  • xsi:schemaLocation="
  • http://www.springframework.org/schema/beans
  • http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  • http://www.springframework.org/schema/aop
  • http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  • http://www.springframework.org/schema/tx
  • http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  • http://www.springframework.org/schema/context
  • http://www.springframework.org/schema/context/spring-context.xsd">

  • <!--Properties文件读取配置,base的properties-->
  • <context:property-placeholderlocation="classpath:jdbc.properties"/>

  • <beanid="dataSource"
  • class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  • <propertyname="driverClassName"value="${driverClass}"/>
  • <propertyname="url"value="${url}"/>
  • <propertyname="username"value="${username}"></property>
  • <propertyname="password"value="${password}"></property>
  • </bean>

  • <beanid="transactionManager"
  • class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  • <propertyname="dataSource"ref="dataSource"/>
  • </bean>

  • <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
  • <propertyname="configLocation"value="classpath:mybatis-config.xml"/>
  • <propertyname="dataSource"ref="dataSource"/>
  • </bean>

  • <!--测试用例start-->
  • <beanid="TestMapperTest"class="com.xxyd.mybatis.test.TestMapperTest">
  • <propertyname="sessionFactory"ref="sqlSessionFactory"/>
  • </bean>
  • <!--测试用例end-->


  • <!--mapperbean-->
  • <beanid="TestMapper"class="org.mybatis.spring.mapper.MapperFactoryBean">
  • <propertyname="mapperInterface"value="com.xxyd.mybatis.dao.TestMapper"/>
  • <propertyname="sqlSessionFactory"ref="sqlSessionFactory"/>
  • </bean>

  • </beans>

  7、最后,测试类:
Java代码


  • packagecom.xxyd.mybatis.test;

  • importjava.util.List;

  • importorg.apache.ibatis.session.RowBounds;
  • importorg.apache.ibatis.session.SqlSession;
  • importorg.apache.ibatis.session.SqlSessionFactory;
  • importorg.apache.ibatis.session.SqlSessionFactoryBuilder;
  • importorg.junit.Test;
  • importorg.springframework.context.support.ClassPathXmlApplicationContext;

  • importcom.xxyd.mybatis.pojo.TestEntity;

  • publicclassTestMapperTest{

  • //privateSqlSessionFactorysessionFactory;
  • privatestaticSqlSessionFactoryBuilderbuilder;
  • privatestaticSqlSessionFactorysessionFactory;
  • static{
  • builder=newSqlSessionFactoryBuilder();
  • sessionFactory=builder.build(Thread.currentThread()
  • .getContextClassLoader()
  • .getResourceAsStream("mybatis-config.xml"));
  • }
  • /**
  • *@paramargs
  • */
  • publicstaticvoidmain(String[]args){
  • ClassPathXmlApplicationContextctx=newClassPathXmlApplicationContext("applicationContext.xml");
  • TestMapperTestTestMapperTest=(TestMapperTest)ctx.getBean("TestMapperTest");
  • TestMapperTest.getList();

  • }

  • @Test
  • publicvoidgetList(){
  • SqlSessionsqlSession=sessionFactory.openSession();
  • //TestEntityentity=newTestEntity();
  • List<TestEntity>list=sqlSession.selectList("com.xxyd.mybatis.dao.TestMapper.getListTestEntity",TestEntity.class,newRowBounds(0,200));
  • sqlSession.commit();
  • sqlSession.close();
  • for(TestEntitytestEntity:list){
  • System.out.println(testEntity.toString());
  • }
  • }

  • //publicSqlSessionFactorygetSessionFactory(){
  • //returnsessionFactory;
  • //}
  • //publicvoidsetSessionFactory(SqlSessionFactorysessionFactory){
  • //this.sessionFactory=sessionFactory;
  • //}
  • }

  能够成功运行的前提是,你已经将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类型的就可以,目前还没发现什么异常,或许我研究的较为浅显的原因;第三个参数则是需要分页的数据。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305389-1-1.html 上篇帖子: 使用mybatis 批量插入 下篇帖子: mybatis入门(二)--简单入门例子
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表