首页 > 其他分享 >mybatis-plus与springboot整合

mybatis-plus与springboot整合

时间:2022-11-20 12:02:41浏览次数:43  
标签:mpg springboot static plus mybatis new true public String

一、mybatis开发问题

  1. 需要自己写实体
  2. 需要自己写xml文件和对应的xml中的sql

那是不是存在一种对于通用的功能做很好支持的插件功能:mybatis-plus

二、解决的问题:

  1. 代码生成-生成我们需要的control、service、实体、Mapper、mapper.xml等文件
  2. 自带增删改查的支持,不需要太多的自我配置

三、整合

1)xml-只列出了特殊使用的几个jar包,特别注意版本、不同的版本有些代码有差异

      <!-- 代码生成器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- 模板引擎 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
View Code

 

 

2)代码生成器-使用这个可以直接生成 control、service、实体、Mapper、mapper.xml等文件代码

  

public class GeneratorUtils {
//    public static String  PROJECT_PATH = "";


    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        mpg.setGlobalConfig(GeneratorUtils.globalConfig());
        mpg.setDataSource(GeneratorUtils.dataSourceConfig());
        mpg.setPackageInfo(GeneratorUtils.packageConfig("com.baomidou.ant"));
        mpg.setCfg(GeneratorUtils.injectionConfig());
        mpg.setTemplate(GeneratorUtils.templateConfig());
        //参数是表名称,也可以是表名称数组
        mpg.setStrategy(GeneratorUtils.strategyConfig("test_user"));
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }
    
    public static Scanner scanner = new Scanner(System.in);
    // 获得当前项目的路径,如上图就是 F:stady_program/Mybatis_plus逆向工程
    public final static String PROJECT_PATH = System.getProperty("user.dir");
    public static String partentName = "com.baomidou.ant";

    // 最简单的配置
    public static TemplateConfig templateConfig() {
        TemplateConfig templateConfig = new TemplateConfig();


        return templateConfig.setXml(null);
    }





    // 判断输入的父项目是否存在
    public static boolean parentPackageExits(String project, String parentPackageName){
        StringBuilder path = new StringBuilder(PROJECT_PATH);
        if(!"".equals(project) && project != null){
            path.append("/" + project);
        }
        path.append(("/src/main/java/" + parentPackageName).replace(".","/"));
        File file = new File(path.toString());
        return file.exists();
    }



    public static InjectionConfig injectionConfig(){
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                this.setMap(new HashMap<>()); // 实现InjectionConfig抽象类就需要初始化一个Map集合
            }
        };
        List<FileOutConfig> fileOutConfigList = new ArrayList<>();
        // 根据/templates/mapper.xml.ftl规则在指定的位置生成Mapper文件,可以在多个地方生成。
        String templatePath = "/templates/mapper.xml.vm";
//        String templatePath = "/templates/mapper.xml.ftl";
        fileOutConfigList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 返回Mapper文件的绝对路径
                String path = PROJECT_PATH + "/src/main/resources/mapper/" +
                        tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                return path;
            }
        });
        // 将对Mapper文件的配置添加到文件输出对象中
        injectionConfig.setFileOutConfigList(fileOutConfigList);
        return injectionConfig;
    }


    public static StrategyConfig strategyConfig (String tableName){
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true)// 开启全局大写命名
                .setInclude(tableName)// 设置要映射的表
                .setNaming(NamingStrategy.underline_to_camel)// 下划线到驼峰的命名方式
                .setColumnNaming(NamingStrategy.underline_to_camel)// 下划线到驼峰的命名方式
                .setEntityColumnConstant(true)
                .setEntityTableFieldAnnotationEnable(true) //生成的实体有 对应的注解对应
                .setEntityLombokModel(true)// 是否使用lombok
                .setRestControllerStyle(true)// 是否开启rest风格
                .setTablePrefix("t_") // 去除前缀
                .setControllerMappingHyphenStyle(true); // localhost:8080/hello_a_2

        return strategyConfig;
    }


    public static PackageConfig packageConfig(String fatherPackageName){
        fatherPackageName = partentName;
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(fatherPackageName) // 配置指定项目中各层的名称
                .setController("controller") // Controller层
                .setEntity("entity") // 实体层(pojo层)
                .setMapper("dao") // Dao 层
                .setService("service") // service层
                .setServiceImpl("service.serviceImpl"); // ServiceImp层

        return packageConfig;
    }


    public static GlobalConfig globalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
//        GlobalConfig gc = new GlobalConfig();

        // 输出文件路径
        globalConfig.setOutputDir(PROJECT_PATH + "/src/main/java")
                .setAuthor("Time Travel")// 设置作者名字
                .setOpen(false)// 是否打开资源管理器
                .setFileOverride(true)// 是否覆盖原来生成的
                .setIdType(IdType.AUTO)// 主键策略
                .setBaseResultMap(true)// 生成resultMap
                .setDateType(DateType.ONLY_DATE) // 设置时间格式,采用Date
                .setServiceName("%sService");// 生成的service接口名字首字母是否为I,这样设置就没有I
        //setBaseColumnList(true)  XML中生成基础列
        return globalConfig;
    }


    public static DataSourceConfig dataSourceConfig(){
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/test11?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai")
                .setUsername("root1")
                .setPassword("root1")
                .setDriverName("com.mysql.jdbc.Driver").setDbType(DbType.MYSQL);

        return dataSourceConfig;
    }
 
}
View Code

 

标签:mpg,springboot,static,plus,mybatis,new,true,public,String
From: https://www.cnblogs.com/lean-blog/p/16908148.html

相关文章

  • springboot 启动脚本
    springboot项目打包有是一个jar包,需要启动,如下命令启动命令:nohupjava-jarapp.jar>./log.out2>&1&但使用脚本会更加方便,脚本如下:#!/bin/bash#这里可替换为你自己......
  • springboot整合redis详解
    springboot整合redis1.首先创建springboot工程2.配置pom.xml文件<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns......
  • MyBatis
    MyBatis-第一章 ORM(ObjectRelationalMapping) 设计模式,先有思想,后有实现对象关系映射,是一种数据持久化技术。它在对象模型和关系型数据库之间建立起对应关系,并且......
  • MyBatis2
    MyBatis-第二章Dao接口引入1.修改mapper文件的namespace,对应接口类的全路径2.创建BookDao.java接口类3.接口方法名对应mapper的SQLid4.定义反参和入参(如有)......
  • MyBatis3
    MyBatis-第三章 log4j日志记录可以记录不同级别的日志信息,以备排错和后续信息参考1.配置log4j.properties配置文件,放置在根目录下日志全局设置:log4j.rootLogger=lev......
  • Mybatis 入门实战(2)--简单使用
    本文主要介绍Mybatis的实际使用,相关的环境及软件信息如下:Mybatis3.5.11。1、工程整体结构这里使用Maven来构建样例工程,工程目录结构如下:2、引入依赖<dependency......
  • Element plus 时间选择器 和下拉列表 的值的获取
    <el-form-itemlabel="时间"><divclass="block"><spanclass="demonstration"></span><el-dat......
  • mybatis学习第六部分:Mybatis注解开发
    6.1  MyBatis的常⽤注解这⼏年来注解开发越来越流⾏,Mybatis也可以使⽤注解开发⽅式,这样我们就可以减少编写Mapper映射⽂件了。我们先围绕⼀些基本的CRUD来学习,再学习......
  • mybatis学习第五部分:Mybatis复杂映射开发
    5.1⼀对⼀查询5.1.1⼀对⼀查询的模型⽤户表和订单表的关系为,⼀个⽤户有多个订单,⼀个订单只从属于⼀个⽤户⼀对⼀查询的需求:查询⼀个订单,与此同时查询出该订单所属的⽤......
  • mybatis学习第四部分: Mybatis配置⽂件深⼊
    4.1  核⼼配置⽂件SqlMapConfig.xml4.1.1 MyBatis核⼼配置⽂件层级关系   4.2  MyBatis常⽤配置解析1)environments标签数据库环境的配置,⽀持多环境配置......