maven+ssm+sqlserver分页处理

在做项目的时候,涉及到分页,记录一个mybatis比较简单的分页方式——PageHelper。
在添加分页之前,首先先添加maven依赖。如果不是maven项目,直接导入jar到lib也是一样的。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.dawngroupId>
  <artifactId>appartifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>app Maven Webappname>
  <url>http://maven.apache.orgurl>
  <dependencies>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>4.3.3.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>4.3.3.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>4.3.3.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-context-supportartifactId>
        <version>3.2.12.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.4.0version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>1.3.0version>
    dependency>
    <dependency>
        <groupId>commons-dbcpgroupId>
        <artifactId>commons-dbcpartifactId>
        <version>1.4version>
    dependency>
    <dependency>
        <groupId>jstlgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
    dependency>
    <dependency>
        <groupId>commons-fileuploadgroupId>
        <artifactId>commons-fileuploadartifactId>
        <version>1.3.1version>
    dependency>
    <dependency>
        <groupId>commons-iogroupId>
        <artifactId>commons-ioartifactId>
        <version>2.4version>
    dependency>
    <dependency>
        <groupId>commons-codecgroupId>
        <artifactId>commons-codecartifactId>
        <version>1.10version>
    dependency>
    <dependency>
        <groupId>org.codehaus.jacksongroupId>
        <artifactId>jackson-mapper-aslartifactId>
        <version>1.9.13version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-txartifactId>
        <version>4.3.3.RELEASEversion>
    dependency>

    <dependency>
  <groupId>com.fasterxml.jackson.coregroupId>
  <artifactId>jackson-coreartifactId>
  <version>2.8.5version>
dependency>
<dependency>
  <groupId>com.fasterxml.jackson.coregroupId>
  <artifactId>jackson-databindartifactId>
  <version>2.8.5version>
dependency>
<dependency>
  <groupId>com.fasterxml.jackson.coregroupId>
  <artifactId>jackson-annotationsartifactId>
  <version>2.8.5version>
dependency>
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>fastjsonartifactId>
        <version>1.2.9version>
    dependency>
    <dependency>
        <groupId>org.codehaus.jacksongroupId>
        <artifactId>jackson-core-aslartifactId>
        <version>1.9.13version>
    dependency>
    <dependency>
        <groupId>net.sourceforge.jtdsgroupId>
        <artifactId>com.springsource.net.sourceforge.jtdsartifactId>
        <version>1.2.2version>
    dependency>
    <dependency>
        <groupId>javaeegroupId>
        <artifactId>javaee-apiartifactId>
        <version>5version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>4.3.3.RELEASEversion>
    dependency>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>4.3.3.RELEASEversion>
    dependency>
    <dependency>
        <groupId>com.github.pagehelpergroupId>
        <artifactId>pagehelperartifactId>
        <version>5.1.2version>
    dependency>
  dependencies>
  <build>
    <finalName>appfinalName>
        <plugins>  
          
        <plugin>  
            <groupId>org.apache.maven.pluginsgroupId>  
            <artifactId>maven-compiler-pluginartifactId>  
            <version>2.3.2version>  
            <configuration>  
                <source>1.7source>  
                <target>1.7target>  
            configuration>  
        plugin>  
    plugins> 
  build>
project>

其中
com.github.pagehelper
pagehelper
5.1.2

就是要分页的依赖。其他的都是为了配置ssm框架所导入的包。
然后添加mybatis-config.xml文件。


  
  
<configuration>  
    <plugins>  
          
        <plugin interceptor="com.github.pagehelper.PageInterceptor">  
        plugin>  
    plugins>
configuration>     

maven+ssm+sqlserver分页处理_第1张图片
这是我的项目的文件结构。
spring-mvc.xml内容如下


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                            http://www.springframework.org/schema/context  
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                            http://www.springframework.org/schema/mvc  
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8value>
            list>
        property>
    bean>

    
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />    
            list>
        property>
    bean>



    
    

    
    <context:component-scan base-package="com.dawn.controller" />

    
   <mvc:annotation-driven/>
   
    

  

    

    
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        <property name="defaultEncoding" value="UTF-8" />
        
        <property name="maxUploadSize" value="10485760000" />
        
        <property name="maxInMemorySize" value="40960" />
    bean>
beans>

这是spring-mybatis.xml文件


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    <context:component-scan base-package="com.dawn" />

    
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:config/jdbc.properties" />
    bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />

    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="mapperLocations" value="classpath:mappers/*.xml">property>
        <property name="configLocation" value="classpath:config/mybatis-config.xml">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dawn.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
    bean>

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>
beans>

还有数据库的配置文件 jdbc.properties

driver=net.sourceforge.jtds.jdbc.Driver
url=jdbc:jtds:sqlserver://192.168.1.3:1433;databaseName=bbh
username=db123
password=88

然后在mapper.xml的,写普通 的查询语句就好

<select id="queryOne" resultMap="messageMap">
    
    select * from dggc1.dbo.mybook where pkid=#{pkid}
    select>

如果需要分页,在写调用mapper.xml的接口的时候要提前添加
PageHelper.startPage(1, 20);这个分页的代码。表示当前第一页,显示20条记录。。。
下面是我的测试的代码,这仅仅是个使用分页的例子

@Test
public void testQueryAll() {
MessageMapper massageMapper = ctx.getBean("messageMapper",MessageMapper.class);
PageHelper.startPage(1,3);//分页的代码
Listmassages=massageMapper.queryAll("A");
for(Message i:massages)
 System.out.println(i);
        }

这就是pagehelper比较简单的做法。

你可能感兴趣的:(mybatis分页)