springboot mybatis-plus generator自动生成(sqlserver),整合swagger

这里写一篇通过 mybatis generator类,自动生成 sqlserver代码类,通过springboot集成。连接sqlserver的总结。

1、创建springboot的项目
开发工具选择 idea,还是通过快速创建springboot项目的方式来创建一个springboot的web项目。

2、编辑 pom.xml,加入如下依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--添加SQL-server驱动-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.4.1.jre8</version>
        </dependency>
        <!--添加freemarker模板支持  主要为自动生成代码使用-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

    </dependencies>

3、配置generator类

package com.zjars.hxmes.storage.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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;

/**
 * @author wjl
 * @date 2021-08-04 15:02
 **/
public class Generator {


    //数据库配置四要素
    private static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String url = "jdbc:sqlserver://xxx";
    private static String username = "xxx";
    private static String password = "xxx";


    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("wjl");
        gc.setOpen(true);
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");

        gc.setFileOverride(true);
        gc.setSwagger2(true);
//        gc.setActiveRecord(true);
//        gc.setIdType(IdType.ID_WORKER);//设置id的生成策略默认算法
//        gc.setDateType(DateType.ONLY_DATE);//设置日期生成策略
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driverName);
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setDbType(DbType.SQL_SERVER);
        mpg.setDataSource(dsc);

        //包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName("aaa");
        pc.setParent("com.zjars.hxmes.storage");
        pc.setEntity("entity.minor");
        pc.setService("service.minor");
        pc.setServiceImpl("service.minor.impl");
        pc.setMapper("mapper.minor");
        pc.setController(null);
//        pc.setController("controller");
        mpg.setPackageInfo(pc);


        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/resources/mapper/minor"
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null).setController(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
//        strategy.setTablePrefix("t_");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        // 设置逻辑删除键(这个是逻辑删除的操作)
        strategy.setLogicDeleteFieldName("deleted");
        // 指定生成的bean的数据库表名(如果全部生成,这里要注释掉)
        strategy.setInclude("Inventory","Inventoryclass","ComputationUnit",
                "CurrentStock","Warehouse","rdrecord11","rdrecords11");
        //strategy.setSuperEntityColumns("id");
        // 驼峰转连字符
//        strategy.setControllerMappingHyphenStyle(true);
        strategy.setRestControllerStyle(true);
        mpg.setStrategy(strategy);
        // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

4、运行 generator

生成的结构如下:

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Spring Boot 是一个开源的 Java 开发框架,而 MyBatis-Plus 是一个基于 MyBatis 的增强工具库。MyBatis-Plus 提供了很多便捷的功能,使得与数据库的交互更加简单和高效。 要集成 Spring Boot 和 MyBatis-Plus,可以按照以下步骤进行: 1. 添加依赖:在项目的 pom.xml 文件中添加 Spring Boot 和 MyBatis-Plus 的依赖。通常可以在 Maven 中央仓库找到它们的最新版本。 2. 配置数据源:在 application.properties 或 application.yml 文件中配置数据库连接信息,包括数据库 URL、用户名、密码等。 3. 创建实体类:根据需要创建与数据库表相对应的实体类。可以使用注解来标识实体类与数据表的映射关系。 4. 创建 Mapper 接口:创建与实体类对应的 Mapper 接口,用于定义数据库操作的方法。可以使用 MyBatis-Plus 提供的通用 CRUD 方法,也可以根据需求自定义方法。 5. 创建 Service 类:创建 Service 类,用于封装业务逻辑并调用 Mapper 接口中的方法。可以使用注解来标识 Service 类的角色,如 @Service。 6. 配置文件:在 Spring Boot 的配置类中配置 MyBatis-Plus 的相关配置。可以设置数据源、Mapper 扫描路径等。 7. 启动应用程序:根据需要,可以创建一个启动类并添加 @SpringBootApplication 注解。使用 Spring Boot 提供的内嵌容器启动应用程序。 通过以上步骤,就可以完成 Spring Boot 和 MyBatis-Plus 的集成。使用 MyBatis-Plus 提供的丰富功能,可以简化数据库操作,提高开发效率。这个集成方案可以在大多数使用 Spring Boot 的项目中使用,使得与数据库的交互更加方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值