新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

SpringBoot中MybatisPlus的使用方法

本篇内容主要讲解“SpringBoot中MybatisPlus的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot中MybatisPlus的使用方法”吧!

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、成都网站设计、外贸营销网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的昌乐网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!


创建个SpringBoot项目

选生所需的依赖:==

SpringBoot中MybatisPlus的使用方法

我把application的后缀改为.yml了,方便些。

SpringBoot中MybatisPlus的使用方法

==pom.xml:==



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.4
         
    
    com.keafmd
    springboot-mybatisplus
    0.0.1-SNAPSHOT
    springboot-mybatisplus
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            MySQL
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    

因为我们配置了数据源,所以需要在application.yml中配置下数据源,不然会起不来,我顺便也改了下端口。

==application.yml:==

server:
  port: 80

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false&&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 18044229

写个HelloController测试下

==HelloController:==

package com.keafmd.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Keafmd
 *
 * @ClassName: HelloController
 * @Description:
 * @author: 牛哄哄的柯南
 * @Date: 2021-04-09 11:11
 * @Blog: https://keafmd.blog.csdn.net/
 */
@RestController
public class HelloController {
 
    @RequestMapping("/hello")
    public String hello(){
        return "keafmd";
    }
}

运行启动类,访问:http://127.0.0.1/hello

SpringBoot中MybatisPlus的使用方法

到此证明SpringBoot没有问题。

使用代码生成器生成代码

添加所需的依赖

pom.xml中添加以下依赖:


   com.baomidou
    mybatis-plus-generator
    test
    3.4.0



    org.freemarker
    freemarker
    test
    2.3.31

由于代码生成器并不会在生产环境使用,只是在开发环境中使用了下。所以我们把代码生成器写在test包中即可,依赖的使用场景也定义成test即可。

CodeGenerator

==CodeGenerator:==

package com.keafmd.mp;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Keafmd
 *
 * @ClassName: CodeGenerator
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-03-23 21:47
 */
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

    /**
     * 

     * 读取控制台内容      * 

     */     public static String scanner(String tip) {         Scanner scanner = new Scanner(System.in);         StringBuilder help = new StringBuilder();         help.append("请输入" + tip + ":");         System.out.println(help.toString());         if (scanner.hasNext()) {             String ipt = scanner.next();             if (StringUtils.isNotBlank(ipt)) {                 return ipt;             }         }         throw new MybatisPlusException("请输入正确的" + tip + "!");     }     public static void main(String[] args) {         // 代码生成器         AutoGenerator mpg = new AutoGenerator();         // 全局配置         GlobalConfig gc = new GlobalConfig();         String projectPath = System.getProperty("user.dir"); //        System.out.println("projectPath = " + projectPath);         gc.setOutputDir(projectPath + "/src/main/java"); //        gc.setOutputDir("D:\\test");         gc.setAuthor("关注公众号:牛哄哄的柯南");         gc.setOpen(false);         // gc.setSwagger2(true); 实体属性 Swagger2 注解         gc.setServiceName("%sService");         mpg.setGlobalConfig(gc);         // 数据源配置         DataSourceConfig dsc = new DataSourceConfig();         dsc.setUrl("jdbc:mysql://localhost:3306/ssm-java1?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");         // dsc.setSchemaName("public");         dsc.setDriverName("com.mysql.cj.jdbc.Driver");         dsc.setUsername("root");         dsc.setPassword("18044229");         mpg.setDataSource(dsc);         // 包配置         PackageConfig pc = new PackageConfig();         pc.setModuleName(null);         pc.setParent("com.keafmd");         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<>();         // 自定义配置会被优先输出         focList.add(new FileOutConfig(templatePath) {             @Override             public String outputFile(TableInfo tableInfo) {                 // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!                 return projectPath + "/src/main/resources/mapper/"                         + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;             }         });         cfg.setFileOutConfigList(focList);         mpg.setCfg(cfg);         // 配置模板         TemplateConfig templateConfig = new TemplateConfig();         templateConfig.setXml(null);         mpg.setTemplate(templateConfig);         // 策略配置         StrategyConfig strategy = new StrategyConfig();         strategy.setNaming(NamingStrategy.underline_to_camel);         strategy.setColumnNaming(NamingStrategy.underline_to_camel);         strategy.setEntityLombokModel(true);         strategy.setRestControllerStyle(true);         strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));         strategy.setControllerMappingHyphenStyle(true);         strategy.setTablePrefix("m_");         mpg.setStrategy(strategy);         mpg.setTemplateEngine(new FreemarkerTemplateEngine());         mpg.execute();     } }

运行代码生成器,在控制台输入想要生成的表

SpringBoot中MybatisPlus的使用方法

这样就会生成一些包及相应的代码,注意CodeGenerator中的相关代码(如数据库的,包名的)需要该成你们需要的。

SpringBoot中MybatisPlus的使用方法

到此,相信大家对“SpringBoot中MybatisPlus的使用方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


当前题目:SpringBoot中MybatisPlus的使用方法
网页链接:http://www.wjwzjz.com/article/ggjghd.html
在线咨询
服务热线
服务热线:028-86922220
TOP