1.配置 pom.xml,引入相关 jar
<?xml version="1.0" encoding="UTF-8"?>
<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.rent.merchant</groupId>
<artifactId>MerchantManage</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<!-- springboot jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql jar -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- mybatis-plus jar -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<!-- pagehelper jar -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
<!-- lombok jar -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
2.编写主函数Application:
package com.rent.merchant;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.编写接口
/**
* merchant list
*
* @param withdraw
* @return ResultStruct
* @author liumengwei
* @date 2018/10/17
* @since V1.0
*/
@RequestMapping(value = "/merchantList", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
@ResponseBody
public ResultStruct merchantList(
@Param ("page") int page,
@Param ("limit") int limit,
@Param ("merchantName") String merchantName,
@Param ("idnumber") String idnumber,
@Param ("telephone") String telephone,
@Context HttpServletRequest request) {
try {
Map<String, Object> map = new HashMap<>();
map.put("merchantName", merchantName);
map.put("idnumber", idnumber);
map.put("telephone", telephone);
map.put("limit", limit);
map.put("page", page);
List<Merchant> list = merchantService.merchantList(map);
map.clear();
map.put("item", list);
return ResultStruct.setResultStructInfo(map, ResultMsgConstant.querySuccess, ResultStruct.OK);
} catch (Exception ex) {
return SystemException.setResult(ex, logger);
}
}
4.创建实体类
package com.rent.merchant.daoBean;
import lombok.Data;
@Data
public class Merchant {
private Long id;
private String password;
private String merchantName;
private String businessLicense;
private String businessLicenseNumber;
private String address;
private String bossName;
private String idnumber;
private String telephone;
private String bankNumber;
private String commissionRate;
private String subtopic;
private String insertTime;
private int delete;
private int level;
private String qccode;
}
5.编写mapper
使用@SelectProvider 注解方式获取 sql
package com.rent.merchant.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.rent.merchant.daoBean.Merchant;
import com.rent.merchant.provider.MerchantMapperProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;
import java.util.List;
import java.util.Map;
@Mapper
public interface MerchantMapper extends BaseMapper<Merchant> {
@Results({
@Result(property = "insertTime", column = "insert_time"),
@Result(property = "merchantName", column = "merchant_name"),
@Result(property = "businessLicense", column = "business_license"),
@Result(property = "bossName", column = "boss_name"),
@Result(property = "commissionRate", column = "commission_rate"),
@Result(property = "bankNumber", column = "bank_number"),
@Result(property = "businessLicenseNumber", column = "business_license_number")
})
@SelectProvider(type = MerchantMapperProvider.class, method = "merchantList")
List<Merchant> merchantList(Map<String, Object> map);
}
package com.rent.merchant.provider;
import com.rent.merchant.common.utils.StringUtil;
import java.util.HashMap;
import java.util.Map;
public class MerchantMapperProvider {
public String merchantList(Map<String, Object> map) {
StringBuffer sql = new StringBuffer(
"select m.* from t_merchant m where 1=1 "
);
Map<String, Object> sqlMap = new HashMap<>();
sqlMap.put("merchantName", " and m.merchant_name like '%" + map.get("merchantName") + "%'");
sqlMap.put("telephone", " and m.telephone like '%" + map.get("telephone") + "%'");
sqlMap.put("idnumber", " and m.idnumber like '%" + map.get("idnumber") + "%'");
for (Map.Entry<String, Object> mm : map.entrySet()) {
if (!StringUtil.isEmpty(mm.getValue())) {
if (sqlMap.get(mm.getKey()) != null)
sql.append(sqlMap.get(mm.getKey()));
}
}
return sql.toString();
}
}
6.编写 Service
package com.rent.merchant.service;
import com.github.pagehelper.PageHelper;
import com.rent.merchant.common.page.PageBean;
import com.rent.merchant.daoBean.Merchant;
import com.rent.merchant.mapper.MerchantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class MerchantService {
@Autowired
private MerchantMapper merchantMapper;
/**
* merchant list
*
* @return ResultStruct
* @author liumengwei
* @date 2018/10/12
* @since V1.0
*/
public List<Merchant> merchantList(Map<String, Object> map) {
int currentPage = Integer.parseInt(String.valueOf(map.get("page")));
int pageSize = Integer.parseInt(String.valueOf(map.get("limit")));
PageHelper.startPage(currentPage, pageSize);
List<Merchant> withdrawBeanList = merchantMapper.merchantList(map);
int countNums = withdrawBeanList.size();
PageBean<Merchant> pageData = new PageBean<>(currentPage, pageSize, countNums);
pageData.setItems(withdrawBeanList);
return pageData.getItems();
}
}
至此,既可使用pagehelper实现分页功能
博主扣扣: 博主微信: