公司要做前后端分離总放,后端決定采用springboot提供接口程序陕见,持久層采用mybatis,為了方便即彪,需要對mapper進一步封裝紧唱,繼續(xù)整合PageHelper和tk.mybatis活尊。
pom添加依賴
<!--持久層配置開始-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!--持久層配置結束-->
application.properties配置
# 數(shù)據(jù)源基礎配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#數(shù)據(jù)庫
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mybatis 配置
mybatis.type-aliases-package=com.mos.quote.model
mybatis.mapper-locations=classpath:mapper/*.xml
# 通用 Mapper 配置
mapper.mappers=com.mos.quote.common.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
# 分頁插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
PS:此處的坑,pagehelper.reasonable,啟用合理化時候漏益,如果pageNo<1蛹锰,則會返回第一頁數(shù)據(jù),如果pageNo>pages會查詢最后一頁绰疤,作為接口程序铜犬,如果傳入的pageNo一直大于pages,則一直會有數(shù)據(jù)返回轻庆,前端還需要校驗頁碼問題癣猾。
建議:如果普通的分頁查詢,建議開啟該功能余爆,如果作為前后端分離或者提供接口之類的纷宇,建議禁用該功能
MyMapper
package com.mos.quote.common;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* 該接口不能被掃描到,否則會出錯
* @author Administrator
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
MyMapper的使用
package com.mos.quote.mapper;
import com.mos.quote.common.MyMapper;
import com.mos.quote.model.Area;
/**
* @author Administrator
*/
public interface AreaMapper extends MyMapper<Area> {
}
Service中使用
package com.mos.quote.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mos.quote.mapper.AreaMapper;
import com.mos.quote.model.Area;
import com.mos.quote.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author Administrator
*/
@Service
public class AreaServiceImpl implements IAreaService {
@Autowired
private AreaMapper areaMapper;
@Override
public List<Area> queryAllByPID(String parentId) {
Area area = new Area();
area.setParentId(parentId);
return areaMapper.select(area);
}
@Override
public PageInfo<Area> queryPage(Integer pageNo, Integer pageSize, String parentId) {
PageHelper.startPage(pageNo,pageSize);
List<Area> list = this.queryAllByPID(parentId);
return new PageInfo<>(list);
}
}
啟動添加mapper掃描
package com.mos.quote;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author Administrator
*/
@SpringBootApplication
@MapperScan(basePackages = "com.mos.quote.mapper")
public class QuoteApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(QuoteApplication.class, args);
}
@Override
public void run(String... strings) throws Exception {
System.out.println("starter");
}
}
測試controller
package com.mos.quote.controller;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.mos.quote.model.Area;
import com.mos.quote.service.IAreaService;
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 java.util.List;
/**
* @author Administrator
*/
@Controller
@RequestMapping("demo")
public class DemoController {
@Autowired
private IAreaService areaService;
@RequestMapping("/test")
public String test(Integer pageNo,Integer pageSize, Model model){
List<Area> areas = areaService.queryAllByPID("0");
model.addAttribute("listSize",areas.size());
System.out.println("areas---->"+JSON.toJSONString(areas));
PageInfo<Area> page = areaService.queryPage(pageNo,pageSize,"0");
System.out.println(JSON.toJSONString(page));
model.addAttribute("page---->", JSON.toJSONString(page));
return "test";
}
}
輸出結果(格式化Json后)
瀏覽器輸入http://localhost:8080/demo/test?pageNo=1&pageSize=10查看控制臺
控制臺數(shù)據(jù)
父id為0的全部數(shù)據(jù)
父id為0的前10條數(shù)據(jù)
其實和spring mvc中使用差不多蛾方,注意jar包引用即可像捶,大部分springboot都有自己對應的jar,使用springmvc的會報錯桩砰。