zoukankan      html  css  js  c++  java
  • 【工具】maven插件自动生成mapper文件

    maven插件自动生成mapper文件

    @

    第一步,新建maven plugin工程

    idea 选择 file -> new project -> maven
    新建项目
    后面一直next,注意maven plugin 工程的命名最好是 xxx-maven-plugin.

    添加依赖

    下面写maven插件必须的依赖。

      <dependency>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-plugin-api</artifactId>
          <version>2.0</version>
        </dependency>
    
        <dependency>
          <groupId>org.apache.maven.plugin-tools</groupId>
          <artifactId>maven-plugin-annotations</artifactId>
          <version>3.1</version>
        </dependency>
    

    因为要依赖mybatis-plus-generator来生成mapper等文件,所以还需要添加下面等依赖。

        <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-generator</artifactId>
          <version>3.3.0</version>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <scope>runtime</scope>
          <version>8.0.15</version>
        </dependency>
        <dependency>
          <groupId>org.apache.velocity</groupId>
          <artifactId>velocity-engine-core</artifactId>
          <version>2.1</version>
        </dependency>
    

    实现功能

    
    @Mojo( name = "mybatis-generate",
            defaultPhase = LifecyclePhase.PROCESS_SOURCES)
    public class MyMojo extends AbstractMojo {
    
        /**
         * 要生成的表名.
         */
        @Parameter(defaultValue = "${tables}", required = true)
        private String tables;
    
        /**
         * 数据库连接
         */
        @Parameter(defaultValue = "${url}", required = true)
        private String url;
    
        /**
         * 驱动名称
         */
        @Parameter(defaultValue = "${driverName}", required = true)
        private String driverName;
    
        /**
         * 用户名称
         */
        @Parameter(defaultValue = "${username}", required = true)
        private String username;
    
        /**
         * 密码
         */
        @Parameter(defaultValue = "${password}", required = true)
        private String password;
    
        /**
         * 父包名
         */
        @Parameter(defaultValue = "${parentModuleName}", required = true)
        private String parentModuleName;
    
        /**
         * 密码
         */
        @Parameter(defaultValue = "${author}", required = false)
        private String author;
    
    
        private static final String DEFAULT_AUTHOR = "SYSTEM GENERATE";
    
        private Log log =  getLog();
    
    
        public void execute() throws MojoExecutionException {
    
            if(this.isNullOrEmpty(tables)){
                getLog().error("tables is null or empty");
                throw new MojoExecutionException("tables is null or empty");
            }
    
            String[] tableArr = tables.split(",");
            if(Objects.isNull(tableArr) || tableArr.length <= 0){
                getLog().error("tables is null or empty");
                throw new MojoExecutionException("tables is null or empty");
            }
    
            AutoGenerator mpg = new AutoGenerator();
            //1、全局配置
            GlobalConfig gc = new GlobalConfig();
            String projectPath = System.getProperty("user.dir");
            gc.setOutputDir(projectPath + "/src/main/java");
            gc.setAuthor(this.isNullOrEmpty(author) ? DEFAULT_AUTHOR : author);
            gc.setOpen(false);
            gc.setFileOverride(true);
            gc.setServiceName("I%sService");
            gc.setBaseResultMap(true);
            mpg.setGlobalConfig(gc);
    
            //2、数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl(url);
            dsc.setDriverName(driverName);
            dsc.setUsername(username);
            dsc.setPassword(password);
            mpg.setDataSource(dsc);
    
            // 3、包配置
            PackageConfig pc = new PackageConfig();
            pc.setParent(parentModuleName);
            mpg.setPackageInfo(pc);
    
            // 4、策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
            strategy.setEntityLombokModel(true);
            strategy.setInclude(tableArr);
            mpg.setStrategy(strategy);
    
            //5、执行
            mpg.execute();
        }
        
        public boolean isNullOrEmpty(CharSequence cs) {
            return cs == null || cs.length() == 0;
        }
    }
    
    

    使用

    maven install到本地或者deploy到远程,然后在需要使用的项目pom.xml文件中添加依赖。

    <plugins>
    	<plugin>
    		<groupId>org.example</groupId>
    		<artifactId>mybatisplus-maven-plugin</artifactId>
    		<version>1.0-SNAPSHOT</version>
    		<configuration>
    			<tables>sp_user</tables>
    			<url>
    				jdbc:mysql://127.0.0.1:3306/myshop?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull&amp;useSSL=false&amp;allowPublicKeyRetrieval=true
    			</url>
    			<driverName>com.mysql.cj.jdbc.Driver</driverName>
    			<username>root</username>
    			<password>a123456</password>
    			<parentModuleName>com.lkb.myshop</parentModuleName>
    			<author>lkb</author>
    		</configuration>
    	</plugin>
    </plugins>
    

    这个插件很简陋,在很多地方可以完善,例如:数据库信息的获取方面,生成文件的选择方面等等。后续优化完成后再完善一波。

    完整代码 请查看
    源码链接

    欢迎关注我的公众号 -- 大白和小倔
  • 相关阅读:
    markdown编辑器使用教程
    【android】shape的使用
    检测android机器是否有GPS模块
    java.io.FileNotFoundException: res/drawable/title_bar_shadow.9.png
    【Arcgis for android】程序运行出错原因分析及解决(超详细)
    百度地图android客户端的AndroidMainfest.xml的学习和android版本号
    Android proguard代码混淆
    Posting data to a HttpHandler greater then ~29MB gives a 404 error
    工具和软件收藏
    C#实现在Winform中嵌入Word和Excel
  • 原文地址:https://www.cnblogs.com/catlkb/p/14632276.html
Copyright © 2011-2022 走看看