1.配置 pom.xml,引入相关 jar
4.0.0
com.rent.merchant
MerchantManage
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
5.1.24
jar
compile
com.baomidou
mybatisplus-spring-boot-starter
1.0.5
com.baomidou
mybatis-plus
2.3
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.9
org.projectlombok
lombok
true
1.8
org.springframework.boot
spring-boot-maven-plugin
spring-releases
https://repo.spring.io/libs-release
spring-releases
https://repo.spring.io/libs-release
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 map = new HashMap<>();
map.put("merchantName", merchantName);
map.put("idnumber", idnumber);
map.put("telephone", telephone);
map.put("limit", limit);
map.put("page", page);
List 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 {
@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 merchantList(Map 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 map) {
StringBuffer sql = new StringBuffer(
"select m.* from t_merchant m where 1=1 "
);
Map 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 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 merchantList(Map map) {
int currentPage = Integer.parseInt(String.valueOf(map.get("page")));
int pageSize = Integer.parseInt(String.valueOf(map.get("limit")));
PageHelper.startPage(currentPage, pageSize);
List withdrawBeanList = merchantMapper.merchantList(map);
int countNums = withdrawBeanList.size();
PageBean pageData = new PageBean<>(currentPage, pageSize, countNums);
pageData.setItems(withdrawBeanList);
return pageData.getItems();
}
}
至此,既可使用pagehelper实现分页功能
博主扣扣: 博主微信: