成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

springboot+mybatis+mybatis-plus分頁查詢(簡(jiǎn)單實(shí)現(xiàn))

Pocher / 3358人閱讀

摘要:讀取控制臺(tái)內(nèi)容請(qǐng)輸入請(qǐng)輸入正確的代碼生成器全局配置實(shí)體屬性注解數(shù)據(jù)源配置包配置這里有個(gè)模塊名的配置,可以注釋掉不用。

最近在研究mybatis,然后就去找簡(jiǎn)化mybatis開發(fā)的工具,發(fā)現(xiàn)就有通用Mapper和mybatis-plus兩個(gè)比較好的可是使用,可是經(jīng)過對(duì)比發(fā)現(xiàn)還是mybatis-plus比較好,個(gè)人覺得,勿噴。。。

集成還是非常簡(jiǎn)單的,然后就在研究怎么分頁,開始研究通用mapper時(shí)發(fā)現(xiàn)有個(gè)pagehelper的分頁工具可以和它搭配。然后反過來看是不是也可以和mybatis-plus搭配使用呢?發(fā)現(xiàn)mybatis-plus之前是可以支持的,升級(jí)成3.X之后就不再支持了。然后就研究mybatis-plus自帶的分頁工具吧!今天就簡(jiǎn)單的寫個(gè)例子吧!

首先我們肯定要先建一個(gè)springboot的項(xiàng)目,這里我就不再多說怎么創(chuàng)建了昂,不會(huì)的話請(qǐng)參考
Spring Boot 的簡(jiǎn)單教程(一) Spring Boot 項(xiàng)目的創(chuàng)建

創(chuàng)建好項(xiàng)目之后我們就需要配置maven依賴了,這里附上我的pom.xml文件了。

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

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.1
        
        
        
            com.baomidou
            mybatis-plus-generator
            3.1.1
        
        
        
            org.freemarker
            freemarker
            2.3.28
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

配置好pom.xml文件之后,理所當(dāng)然的就需要配置application.yml了。

    #端口號(hào)
    server:
      port: 8888
    #數(shù)據(jù)庫的配置信息
    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/blog #自己的數(shù)據(jù)庫名稱
        username: root
        password: 123456
    mybatis:
      #開啟駝峰命名法
      configuration:
        map-underscore-to-camel-case: true
    mybatis-plus:
      # xml地址
      mapper-locations: classpath:mapper/*Mapper.xml
      # 實(shí)體掃描,多個(gè)package用逗號(hào)或者分號(hào)分隔
      type-aliases-package: com.zhouzhaodong.pagination.entity   #自己的實(shí)體類地址
      configuration:
        # 這個(gè)配置會(huì)將執(zhí)行的sql打印出來,在開發(fā)或測(cè)試的時(shí)候可以用
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

接下來就需要配置mybatis-plus的代碼生成器了。

/**
     * 

* 讀取控制臺(tái)內(nèi)容 *

*/ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("請(qǐng)輸入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("請(qǐng)輸入正確的" + tip + "!"); } public static void main(String[] args) { // 代碼生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("jobob"); gc.setOpen(false); // gc.setSwagger2(true); 實(shí)體屬性 Swagger2 注解 mpg.setGlobalConfig(gc); // 數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/blog?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("123456"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); //這里有個(gè)模塊名的配置,可以注釋掉不用。 // pc.setModuleName(scanner("模塊名")); // pc.setParent("com.zhouxiaoxi.www"); pc.setParent(scanner("模塊地址")); mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity // String templatePath = "/templates/mapper.xml.vm"; // 自定義輸出配置 List focList = new ArrayList<>(); // 自定義配置會(huì)被優(yōu)先輸出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會(huì)跟著發(fā)生變化?。? return projectPath + "/src/main/resources/mapper/" // + + pc.getModuleName() + 如果放開上面的模塊名,這里就有一個(gè)模塊名了 + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); /* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 判斷自定義文件夾是否需要?jiǎng)?chuàng)建 checkDir("調(diào)用默認(rèn)方法創(chuàng)建的目錄"); return false; } }); */ cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定義輸出模板 //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會(huì)根據(jù)使用的模板引擎自動(dòng)識(shí)別 // templateConfig.setEntity("templates/entity2.java"); // templateConfig.setService(); // templateConfig.setController(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); //數(shù)據(jù)庫表映射到實(shí)體的明明策略 strategy.setNaming(NamingStrategy.underline_to_camel); //數(shù)據(jù)庫表字段映射到實(shí)體的命名策略, 未指定按照 naming 執(zhí)行 strategy.setColumnNaming(NamingStrategy.underline_to_camel); //自定義繼承的Entity類全稱,帶包名 // strategy.setSuperEntityClass("***"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); //自定義繼承的Controller類全稱,帶包名 // strategy.setSuperControllerClass("***"); strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(",")); //自定義基礎(chǔ)的Entity類,公共字段(可添加更多) // strategy.setSuperEntityColumns("id"); //駝峰轉(zhuǎn)連字符 strategy.setControllerMappingHyphenStyle(true); //表前綴 // strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }

運(yùn)行之后就會(huì)出現(xiàn)所有需要的文件了。

需要配置一下分頁插件,新建一個(gè)文件MybatisPlusConfig。

    //Spring boot方式
    @EnableTransactionManagement
    @Configuration
    public class MybatisPlusConfig {
    
        /**
         * 分頁插件
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }
    }

這樣就只需要在controller里面寫方法就可以了。

    @RestController
    @RequestMapping("/student")
    public class StudentController {
    
        @Autowired
        IStudentService studentService;
    
        @RequestMapping(value = "/findAll",method = RequestMethod.POST)
        public Object findAll(HttpServletRequest request){
            //獲取前臺(tái)發(fā)送過來的數(shù)據(jù)
            Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
            Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
            IPage page = new Page<>(pageNo, pageSize);
            QueryWrapper wrapper = new QueryWrapper<>();
            Student student = new Student();
            student.setId(1);
            wrapper.setEntity(student);
            return studentService.page(page,wrapper);
        }
    
    }

實(shí)現(xiàn)的效果為:

具體代碼在github上面已經(jīng)上傳了,可以去下載使用哦!
https://github.com/zhouzhaodo...

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

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

相關(guān)文章

  • MyBatis-Plus初步

    摘要:是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一,在應(yīng)用方面,是最好的,關(guān)系數(shù)據(jù)庫管理系統(tǒng)應(yīng)用軟件。是一種關(guān)系數(shù)據(jù)庫管理系統(tǒng),關(guān)系數(shù)據(jù)庫將數(shù)據(jù)保存在不同的表中,而不是將所有數(shù)據(jù)放在一個(gè)大倉庫內(nèi),這樣就增加了速度并提高了靈活性。 本章主要是對(duì)MyBatis-Plus的初步介紹,包括一些背景知識(shí)、環(huán)境搭建、初步使用等知識(shí)和例子。對(duì)于背景知識(shí),主要包含對(duì)MyBatis-Plus的特性介紹、為什么使用MyB...

    娣辯孩 評(píng)論0 收藏0
  • Spring Boot 2.x(五):整合Mybatis-Plus

    摘要:的作用可以看到,它給我們提供了一些核心的功能代碼生成器和現(xiàn)成的接口以及可以結(jié)合的條件構(gòu)造器使我們的代碼變得足夠優(yōu)雅,分頁的使用也是相當(dāng)?shù)姆奖?,以及提供了不同的主鍵生成策略。 簡(jiǎn)介 Mybatis-Plus是在Mybatis的基礎(chǔ)上,國(guó)人開發(fā)的一款持久層框架。 showImg(https://segmentfault.com/img/bVbvFk4?w=2022&h=862); 并且榮獲...

    AaronYuan 評(píng)論0 收藏0
  • springboot系列】springboot整合獨(dú)立模塊Druid + mybatis-plus

    摘要:申請(qǐng)連接時(shí)執(zhí)行檢測(cè)連接是否有效,做了這個(gè)配置會(huì)降低性能。作者在版本中使用,通過監(jiān)控界面發(fā)現(xiàn)有緩存命中率記錄,該應(yīng)該是支持。允許和不允許單條語句返回多個(gè)數(shù)據(jù)集取決于驅(qū)動(dòng)需求使用列標(biāo)簽代替列名稱。需要驅(qū)動(dòng)器支持。將自動(dòng)映射所有復(fù)雜的結(jié)果。 項(xiàng)目github地址:https://github.com/5-Ason/aso... 具體可看 ./db/db-mysql 模塊 本文主要實(shí)現(xiàn)的是對(duì)...

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

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

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<