springboot中mybatis使用PageHelper和tk.mybatis

公司要做前后端分離总放,后端決定采用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的會報錯桩砰。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末拓春,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子亚隅,更是在濱河造成了極大的恐慌硼莽,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件枢步,死亡現(xiàn)場離奇詭異沉删,居然都是意外死亡,警方通過查閱死者的電腦和手機醉途,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門矾瑰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人隘擎,你說我怎么就攤上這事殴穴。” “怎么了货葬?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵采幌,是天一觀的道長。 經(jīng)常有香客問我震桶,道長休傍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任蹲姐,我火速辦了婚禮磨取,結果婚禮上人柿,老公的妹妹穿的比我還像新娘。我一直安慰自己忙厌,他們只是感情好凫岖,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著逢净,像睡著了一般哥放。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上爹土,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天甥雕,我揣著相機與錄音,去河邊找鬼胀茵。 笑死犀农,一個胖子當著我的面吹牛,可吹牛的內容都是我干的宰掉。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼赁濒,長吁一口氣:“原來是場噩夢啊……” “哼轨奄!你這毒婦竟也來了?” 一聲冷哼從身側響起拒炎,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤挪拟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后击你,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體玉组,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年丁侄,在試婚紗的時候發(fā)現(xiàn)自己被綠了惯雳。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡鸿摇,死狀恐怖石景,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情拙吉,我是刑警寧澤潮孽,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站筷黔,受9級特大地震影響往史,放射性物質發(fā)生泄漏。R本人自食惡果不足惜佛舱,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一椎例、第九天 我趴在偏房一處隱蔽的房頂上張望挨决。 院中可真熱鬧,春花似錦粟矿、人聲如沸凰棉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撒犀。三九已至,卻和暖如春掏秩,著一層夾襖步出監(jiān)牢的瞬間或舞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工蒙幻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留映凳,地道東北人。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓邮破,卻偏偏與公主長得像诈豌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子抒和,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內容