java大搜罗

MyBatis-Plus PageHelper

2018-12-18  本文已影响716人  ssttIsme

使用PageHelper虽然不需要写Mapper.xml,但是要在Mapper层定义一个有分页参数的方法,如果还有查询参数,那么该方法还可以传入一个Wrapper<T>参数,那么对应的XXMapper接口写的时候就可以这样写

 List<T> selectList(@Param("page")Pagination page,@Param("ew")Wrapper<T> wrapper);

如果你不需要分页,那么查询方法不必在mapper定义,如果排序都不需要,直接传null就可以,对应的XXServiceImpl

    @Override
    public List<XXBean> findAll() {
        return XXMapper.selectList(null);
    }

pom.xml

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bmw</groupId>
    <artifactId>xxbaogao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8101</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- MP 核心库 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.1.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.9.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

config.properties

jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///baogao?useUnicode=true&characterEncoding=utf-8
jdbcUsername=root
jdbcPassword=g

spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <context:component-scan base-package="com.baogao"/>
    <import resource="classpath:spring-mvc.xml" />
    <import resource="classpath:spring-mybatis.xml" />
</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <mvc:annotation-driven />
    <!-- 2.放行静态资源文件 -->
    <mvc:default-servlet-handler />
    <!--配置springMVC视图解析器(负责视图解析操作) -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀 -->
        <property name="Prefix" value="/"></property>
        <property name="Suffix" value=".jsp"></property>
    </bean>
</beans>

spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <util:properties id="cfg" location="classpath:config.properties"></util:properties>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close" lazy-init="false">
        <property name="driverClassName" value="#{cfg.jdbcDriver}"></property>
        <property name="url" value="#{cfg.jdbcUrl}"></property>
        <property name="username" value="#{cfg.jdbcUsername}"></property>
        <property name="password" value="#{cfg.jdbcPassword}"></property>
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="1800" />
        <property name="MaxActive" value="10" />
    </bean>
    <!-- SqlSessionFactory Config -->
    <bean id="sqlSessionFactory"
        class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
        <property name="plugins">
            <array>
                <!-- 分页插件配置 -->
                <bean id="paginationInterceptor"
                    class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
                </bean>
            </array>
        </property>
    </bean>
    <!-- MyBatis Mapper Scan Config -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.baogao.mapper" />
    </bean>
</beans>
package com.baogao.global;

public class Constant {
    public static final int pageSize=4;
}

package com.baogao.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.baogao.pojo.DyBaoGao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;

public interface DyBaoGaoMapper extends BaseMapper<DyBaoGao> {

    List<DyBaoGao> selectList(@Param("page")Pagination page,@Param("ew")Wrapper<DyBaoGao> wrapper);
}
package com.baogao.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baogao.pojo.DyBaoGao;
import com.baogao.service.DyBaoGaoService;


@Controller
public class DyBaoGaoController {
    @Autowired
    DyBaoGaoService dyBaoGaoService;
    @RequestMapping("/dybaogao")
    public String findAll(Model model,Integer page) {
        if(page==null)page=1;
        List<DyBaoGao> list=dyBaoGaoService.findByPage(page);
        model.addAttribute("dybaogao", list);
        model.addAttribute("totalPage", dyBaoGaoService.totalPage());
        return "dybaogao";
    }
}

package com.baogao.pojo;

import java.io.Serializable;
import java.util.Date;

import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
@TableName("dybaogao")
public class DyBaoGao implements Serializable {
    @TableId(type=IdType.AUTO)
    private Long id;
    
    private String title;
    
    private String zhaiyao;

    private Date riqi;

    private String wenjian;

    private String pic;

    private Date crshijian;

    private Integer fid;
    
    private String zuozhe;
    
    private String keyword;

    private static final long serialVersionUID = 1L;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getRiqi() {
        return riqi;
    }

    public void setRiqi(Date riqi) {
        this.riqi = riqi;
    }

    public String getWenjian() {
        return wenjian;
    }

    public void setWenjian(String wenjian) {
        this.wenjian = wenjian;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public Date getCrshijian() {
        return crshijian;
    }

    public void setCrshijian(Date crshijian) {
        this.crshijian = crshijian;
    }

    public Integer getFid() {
        return fid;
    }

    public void setFid(Integer fid) {
        this.fid = fid;
    }

    public String getZhaiyao() {
        return zhaiyao;
    }

    public void setZhaiyao(String zhaiyao) {
        this.zhaiyao = zhaiyao;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getZuozhe() {
        return zuozhe;
    }

    public void setZuozhe(String zuozhe) {
        this.zuozhe = zuozhe;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
    
    
}
package com.baogao.service;

import java.util.List;

import com.baogao.pojo.DyBaoGao;

public interface DyBaoGaoService {

    List<DyBaoGao> findAll();
    List<DyBaoGao> findByPage(Integer currentPage);
    Integer totalPage();
}

package com.baogao.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baogao.global.Constant;
import com.baogao.mapper.DyBaoGaoMapper;
import com.baogao.pojo.DyBaoGao;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;

@Service
public class DyBaoGaoServiceImpl implements DyBaoGaoService {
    @Autowired
    DyBaoGaoMapper dyBaoGaoMapper;

    @Override
    public List<DyBaoGao> findAll() {
        EntityWrapper<DyBaoGao> ew = new EntityWrapper<>();
        ew.orderBy("crshijian", false);

        return dyBaoGaoMapper.selectList(ew);
    }

    @Override
    public List<DyBaoGao> findByPage(Integer currentPage) {
        int total = dyBaoGaoMapper.selectCount(null);
        Pagination pagination = new Page<>(currentPage, Constant.pageSize);
        pagination.setTotal(total);
        EntityWrapper<DyBaoGao> ew = new EntityWrapper<>();
        ew.orderBy("crshijian", false);
        return dyBaoGaoMapper.selectList(pagination,ew);
    }

    @Override
    public Integer totalPage() {
        int totalRecord = dyBaoGaoMapper.selectCount(null);
        int totalPage=totalRecord/Constant.pageSize;
        if(totalRecord%Constant.pageSize!=0){
            totalPage=totalPage+1;
        }
        return totalPage;
    }

}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<div class="news-box">
            <ul class="news-list">
                <c:forEach var="bg" items="${dybaogao}">
                    <li>
                        <div class="txt-box">
                            <h3>
                                <a href="ztyanjiudetail.jsp?id=${bg.id}">${bg.fid}.&nbsp;${bg.title}</a>
                            </h3>
                            <p>${bg.zhaiyao}</p>
                            完成日期:<fmt:formatDate value="${bg.crshijian}" pattern="yyyy-MM-dd"/>
                            <hr class="fengexian"></hr>
                        </div>
                        <div class="img-box">
                            <img src="./images/${bg.pic}" />
                        </div>
                    </li>
                </c:forEach>
            </ul>
        </div>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.baogao.mapper.DyBaoGaoMapper">
    

</mapper>
上一篇下一篇

猜你喜欢

热点阅读