亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

一起來(lái)學(xué)SpringBoot | 第八篇:通用Mapper與分頁(yè)插件的集成

韓冰 / 1000人閱讀

摘要:通用是為了解決使用中的基本操作,使用它可以很方便的進(jìn)行開(kāi)發(fā),可以節(jié)省開(kāi)發(fā)人員大量的時(shí)間。當(dāng)該參數(shù)設(shè)置為時(shí),時(shí)會(huì)查詢第一頁(yè),超過(guò)總數(shù)時(shí),會(huì)查詢最后一頁(yè)。

SpringBoot 是為了簡(jiǎn)化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問(wèn)題而誕生的產(chǎn)物,自動(dòng)裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕易的搭建出一個(gè) WEB 工程

在一起來(lái)學(xué)SpringBoot | 第七篇:整合Mybatis一文中,我們介紹了Mybatis這款優(yōu)秀的框架,順便提及了民間大神開(kāi)發(fā)的兩款插件通用Mapper、PageHelper,從此告別簡(jiǎn)單CURD代碼的編寫(xiě)....

插件介紹

以下兩款插件作者均是同一個(gè)人,如果你想深入了解Mybatis以及插件開(kāi)發(fā)可以購(gòu)買作者的書(shū)籍

京東: https://item.jd.com/12103309.html

當(dāng)當(dāng): http://product.dangdang.com/25098208.html

分頁(yè)插件

GIT地址: https://github.com/pagehelper/Mybatis-PageHelper

在沒(méi)有分頁(yè)插件之前,寫(xiě)一個(gè)分頁(yè)需要兩條SQL語(yǔ)句,一條查詢一條統(tǒng)計(jì),然后才能計(jì)算出頁(yè)碼,這樣的代碼冗余而又枯燥,更重要的一點(diǎn)是數(shù)據(jù)庫(kù)遷移,眾所周知不同的數(shù)據(jù)庫(kù)分頁(yè)寫(xiě)法是不同的,而Mybatis不同于Hibernate的是它只提供動(dòng)態(tài)SQL和結(jié)果集映射。值得慶幸的是,它雖然沒(méi)有為分頁(yè)提供良好的解決方案,但卻提供了Interceptor以供開(kāi)發(fā)者自己擴(kuò)展,這也是這款分頁(yè)插件的由來(lái)....

通用Mapper

GIT地址: https://gitee.com/free/Mapper

通用 Mapper 是一個(gè)可以實(shí)現(xiàn)任意 MyBatis 通用方法的框架,項(xiàng)目提供了常規(guī)的增刪改查操作以及 Example 相關(guān)的單表操作。通用 Mapper 是為了解決 MyBatis 使用中 90% 的基本操作,使用它可以很方便的進(jìn)行開(kāi)發(fā),可以節(jié)省開(kāi)發(fā)人員大量的時(shí)間。

導(dǎo)入依賴

pom.xml 中添加通用Mapper與分頁(yè)插件的依賴包



    tk.mybatis
    mapper-spring-boot-starter
    2.0.2



    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.5



    mysql
    mysql-connector-java



    org.springframework.boot
    spring-boot-starter-web



    org.springframework.boot
    spring-boot-starter-test
    test
屬性配置

application.properties 文件中分別添加上數(shù)據(jù)庫(kù)、Mybatis通用Mapper、PageHelper的屬性配置,這里只提供了常見(jiàn)場(chǎng)景的配置,更全的配置可以參考上文所述的文文檔(#^.^#)

spring.datasource.url=jdbc:mysql://localhost:3306/chapter7?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
spring.datasource.password=root
spring.datasource.username=root
# 如果想看到mybatis日志需要做如下配置
logging.level.com.battcn=DEBUG
########## Mybatis 自身配置 ##########
mybatis.mapper-locations=classpath:com/battcn/mapper/*.xml
mybatis.type-aliases-package=com.battcn.entity
# 駝峰命名規(guī)范 如:數(shù)據(jù)庫(kù)字段是  order_id 那么 實(shí)體字段就要寫(xiě)成 orderId
mybatis.configuration.map-underscore-to-camel-case=true
########## 通用Mapper ##########
# 主鍵自增回寫(xiě)方法,默認(rèn)值MYSQL,詳細(xì)說(shuō)明請(qǐng)看文檔
mapper.identity=MYSQL
mapper.mappers=tk.mybatis.mapper.common.BaseMapper
# 設(shè)置 insert 和 update 中,是否判斷字符串類型!=""
mapper.not-empty=true
# 枚舉按簡(jiǎn)單類型處理
mapper.enum-as-simple-type=true
########## 分頁(yè)插件 ##########
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=false
pagehelper.support-methods-arguments=true
通用Mapper

mapper.enum-as-simple-type: 枚舉按簡(jiǎn)單類型處理,如果有枚舉字段則需要加上該配置才會(huì)做映射

mapper.not-empty: 設(shè)置以后,會(huì)去判斷 insert 和 update 中符串類型!=""

分頁(yè)插件

pagehelper.reasonable: 分頁(yè)合理化參數(shù),默認(rèn)值為false。當(dāng)該參數(shù)設(shè)置為 true 時(shí),pageNum<=0 時(shí)會(huì)查詢第一頁(yè), pageNum>pages(超過(guò)總數(shù)時(shí)),會(huì)查詢最后一頁(yè)。默認(rèn)false 時(shí),直接根據(jù)參數(shù)進(jìn)行查詢。

support-methods-arguments: 支持通過(guò) Mapper 接口參數(shù)來(lái)傳遞分頁(yè)參數(shù),默認(rèn)值false,分頁(yè)插件會(huì)從查詢方法的參數(shù)值中,自動(dòng)根據(jù)上面 params 配置的字段中取值,查找到合適的值時(shí)就會(huì)自動(dòng)分頁(yè)。

注意事項(xiàng)

由于 mybatis.mapper-locations=classpath:com/battcn/mapper/*.xml配置的在java package中,而Spring Boot默認(rèn)只打入java package -> *.java,所以我們需要給pom.xml文件添加如下內(nèi)容


    
        
            src/main/resources
        
        
            src/main/java
            
                **/*.xml
            
            true
        
    
    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    
具體編碼

完成基本配置后,接下來(lái)進(jìn)行具體的編碼操作。

表結(jié)構(gòu)

創(chuàng)建一張 t_user 的表

CREATE TABLE `t_user` (
  `id` int(8) NOT NULL AUTO_INCREMENT COMMENT "主鍵自增",
  `username` varchar(50) NOT NULL COMMENT "用戶名",
  `password` varchar(50) NOT NULL COMMENT "密碼",
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="用戶表";
實(shí)體類

通用Mapper采用了JPA規(guī)范包中的注解,這種的設(shè)計(jì)避免了重復(fù)造輪子,更是讓Spring Data Jpa的應(yīng)用可以輕松切換到Mybatis

package com.battcn.entity;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * @author Levin
 * @since 2018/5/10 0007
 */
@Table(name = "t_user")
public class User implements Serializable {

    private static final long serialVersionUID = 8655851615465363473L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;
    // TODO  省略get set
}
持久層

為了更好的讓熟悉它,此處模擬了一個(gè)自定義的SQL,可以發(fā)現(xiàn)使用 通用Mapper 后并不會(huì)破壞原有代碼結(jié)構(gòu)

UserMapper

繼承 BaseMapper 就可以了,這點(diǎn)是不是有點(diǎn)類似 JpaRepository,同時(shí)也可以根據(jù)自己需要擴(kuò)展出更適合自己項(xiàng)目的BaseMapper,它的靈活也是眾多開(kāi)發(fā)者喜愛(ài)的因素之一

package com.battcn.mapper;

import com.battcn.entity.User;
import org.apache.ibatis.annotations.Mapper;
import tk.mybatis.mapper.common.BaseMapper;

/**
 * t_user 操作,繼承 BaseMapper 就可以了,是不是有點(diǎn)類似 JpaRepository
 *
 * @author Levin
 * @since 2018/5/10 0007
 */
@Mapper
public interface UserMapper extends BaseMapper {

    /**
     * 根據(jù)用戶名統(tǒng)計(jì)(TODO 假設(shè)它是一個(gè)很復(fù)雜的SQL)
     *
     * @param username 用戶名
     * @return 統(tǒng)計(jì)結(jié)果
     */
    int countByUsername(String username);
}
UserMapper 映射文件




  
測(cè)試

完成數(shù)據(jù)訪問(wèn)層接口后,編寫(xiě)一個(gè)junit測(cè)試類來(lái)檢驗(yàn)代碼的正確性。

package com.battcn;

import com.battcn.entity.User;
import com.battcn.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @author Levin
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter7ApplicationTests {

    private static final Logger log = LoggerFactory.getLogger(Chapter7ApplicationTests.class);

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test1() throws Exception {
        final User user1 = new User("u1", "p1");
        final User user2 = new User("u1", "p2");
        final User user3 = new User("u3", "p3");
        userMapper.insertSelective(user1);
        log.info("[user1回寫(xiě)主鍵] - [{}]", user1.getId());
        userMapper.insertSelective(user2);
        log.info("[user2回寫(xiě)主鍵] - [{}]", user2.getId());
        userMapper.insertSelective(user3);
        log.info("[user3回寫(xiě)主鍵] - [{}]", user3.getId());
        final int count = userMapper.countByUsername("u1");
        log.info("[調(diào)用自己寫(xiě)的SQL] - [{}]", count);

        // TODO 模擬分頁(yè)
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        userMapper.insertSelective(new User("u1", "p1"));
        // TODO 分頁(yè) + 排序 this.userMapper.selectAll() 這一句就是我們需要寫(xiě)的查詢,有了這兩款插件無(wú)縫切換各種數(shù)據(jù)庫(kù)
        final PageInfo pageInfo = PageHelper.startPage(1, 10).setOrderBy("id desc").doSelectPageInfo(() -> this.userMapper.selectAll());
        log.info("[lambda寫(xiě)法] - [分頁(yè)信息] - [{}]", pageInfo.toString());

        PageHelper.startPage(1, 10).setOrderBy("id desc");
        final PageInfo userPageInfo = new PageInfo<>(this.userMapper.selectAll());
        log.info("[普通寫(xiě)法] - [{}]", userPageInfo);
    }
}
總結(jié)

Mybatis官方文檔: http://www.mybatis.org/mybatis-3/zh/index.html

通用Mapper文檔: https://gitee.com/free/Mapper/wikis/1.1-java?parent=1.integration

分頁(yè)插件文檔: https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

目前很多大佬都寫(xiě)過(guò)關(guān)于 SpringBoot 的教程了,如有雷同,請(qǐng)多多包涵,本教程基于最新的 spring-boot-starter-parent:2.0.1.RELEASE編寫(xiě),包括新版本的特性都會(huì)一起介紹...

說(shuō)點(diǎn)什么

個(gè)人QQ:1837307557

battcn開(kāi)源群(適合新手):391619659

微信公眾號(hào)(歡迎調(diào)戲):battcn

個(gè)人博客:http://blog.battcn.com/

全文代碼:https://github.com/battcn/spring-boot2-learning/tree/master/chapter7

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/71361.html

相關(guān)文章

  • SpringBoot2.0之五 優(yōu)雅整合SpringBoot2.0+MyBatis+druid+Pa

    摘要:當(dāng)禁用時(shí),所有關(guān)聯(lián)對(duì)象都會(huì)即時(shí)加載。不同的驅(qū)動(dòng)在這方便表現(xiàn)不同。參考驅(qū)動(dòng)文檔或充分測(cè)試兩種方法來(lái)決定所使用的驅(qū)動(dòng)。需要適合的驅(qū)動(dòng)。系統(tǒng)默認(rèn)值是設(shè)置字段和類是否支持駝峰命名的屬性。 ??上篇文章我們介紹了SpringBoot和MyBatis的整合,可以說(shuō)非常簡(jiǎn)單快捷的就搭建了一個(gè)web項(xiàng)目,但是在一個(gè)真正的企業(yè)級(jí)項(xiàng)目中,可能我們還需要更多的更加完善的框架才能開(kāi)始真正的開(kāi)發(fā),比如連接池、分...

    hatlonely 評(píng)論0 收藏0
  • 起來(lái)學(xué)SpringBoot | 第七篇:整合Mybatis

    摘要:但是鑒于國(guó)內(nèi)市場(chǎng)環(huán)境而言,掌握無(wú)異于是佳的選擇,低學(xué)習(xí)成本和動(dòng)態(tài)解耦的特點(diǎn)使得更容易被人們所接受。 SpringBoot 是為了簡(jiǎn)化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問(wèn)題而誕生的產(chǎn)物,自動(dòng)裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕易的搭建出一個(gè) WEB 工程 MyBatis 是一款優(yōu)秀的持久層框架,它支持...

    includecmath 評(píng)論0 收藏0
  • springboot+mybatis+mybatis-plus分頁(yè)查詢(簡(jiǎn)單實(shí)現(xiàn))

    摘要:讀取控制臺(tái)內(nèi)容請(qǐng)輸入請(qǐng)輸入正確的代碼生成器全局配置實(shí)體屬性注解數(shù)據(jù)源配置包配置這里有個(gè)模塊名的配置,可以注釋掉不用。 最近在研究mybatis,然后就去找簡(jiǎn)化mybatis開(kāi)發(fā)的工具,發(fā)現(xiàn)就有通用Mapper和mybatis-plus兩個(gè)比較好的可是使用,可是經(jīng)過(guò)對(duì)比發(fā)現(xiàn)還是mybatis-plus比較好,個(gè)人覺(jué)得,勿噴。。。 集成還是非常簡(jiǎn)單的,然后就在研究怎么分頁(yè),開(kāi)始研究通用ma...

    Pocher 評(píng)論0 收藏0
  • springboot ()集成tkmapper

    摘要:整合想著每次搭建新項(xiàng)目時(shí)框架都要從新搭建,基本常用的也就哪幾種,現(xiàn)在就來(lái)搭建一種常用的后臺(tái)框架,以后新開(kāi)小項(xiàng)目可以直接拿來(lái)使用項(xiàng)目整體結(jié)構(gòu)圖新建空白項(xiàng)目,選中依賴略,也可以完全根據(jù)本人代碼操作文件依賴項(xiàng)展示 springboot整合tkMapper 想著每次搭建新項(xiàng)目時(shí)框架都要從新搭建,基本常用的也就哪幾種,現(xiàn)在就來(lái)搭建一種常用的springboot后臺(tái)框架,以后新開(kāi)小項(xiàng)目可以直接拿來(lái)...

    Shihira 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<