mybatis学习三

Mybatis与pageHelper

分页:
    分页分为假分页和真分页对应的专业术语叫做逻辑分页和物理分页
    逻辑分页:将所有的数据从数据库查询出来,根据需求截取符合要求的数据返回,方便统一但效率低
    物理分页:直接将需要的数据查询出来,但是数据库之间有差异性,但是效率高,数据库性能损耗小
    
物理分页的关键:
    需要自己编写处理方法来处理数据库之间的差异
    mysql:limit
    oracle:roowid
    sqlServer:top
    
Mybatis物理分页的分页插件
    * 引入分页的jar包    
      jsqlparser-0.9.5.jar
      pagehelper-4.2.1.jar
    * 在主配置文件中配置plugins
    <!-- 配置pageHelper分页插件 -->
     <plugins>
          <plugin interceptor="com.github.pagehelper.PageHelper">
          <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
            <property name="dialect" value="mysql"/>         
            <!--当设置为true的时候,如果pagesize设置为0 就不执行分页,返回全部结果  -->
            <property name="pageSizeZero" value="true"/>
            <!--合理化查询 比如如果pageNum<1会查询第一页;如果pageNum>pages会查询最后一页(设置为false返回空)-->
            <property name="reasonable" value="false"/>
            <!-- 支持通过Mapper接口参数来传递分页参数 -->
            <property name="supportMethodsArguments" value="false"/>
             <!-- 总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->    
            <property name="returnPageInfo" value="none"/>
        </plugin>
    </plugins>
    
应用
开发一个简单的登录功能通过servlet

思考:spring如何应用在项目中
    在java工作中,我们可以通过记载spring的配置文件,初始化容器
    ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    IUserService userService = (IUserService) ac.getBean("userService");
    
    在web工程中IOC容器怎么创建,创建后要怎么获取容器管理的对象
        IOC容器必须在web容器启动的时候创建
            1:servlet提供了ServletContextListener.contextInitialized方法可以在web容器启动的时候加载我们需要的东西
            2:创建IOC容器以后,将其放在ServletContext
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            // 将容器对象放入ServletContext中
            ServletContext servletContext = servletContextEvent.getServletContext();
            servletContext.setAttribute("ApplicationContext", ac);


posted @ 2017-10-30 20:59  庄子游世  阅读(110)  评论(0编辑  收藏  举报