springboot搭建通用mapper代码自动生成器

通用mapper代码自动生成器

  • pom包引入
  • pom内build的引用
  • xml编写
  • 代码生成
  • 相关博客

1.采用tk.mybatis.mapper包

pom包引入

<dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>${mapper.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <groupId>org.mybatis.spring.boot</groupId>
                </exclusion>
           </exclusions>
 </dependency>

pom内build的引用

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            &lt;!&ndash; mybatis generator自动生成代码 &ndash;&gt;
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId> mysql</groupId>
                        <artifactId> mysql-connector-java</artifactId>
                        <version>5.1.39</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.5.2</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    &lt;!&ndash;允许移动生成的文件 &ndash;&gt;
                    <verbose>true</verbose>
                    &lt;!&ndash; 是否覆盖 &ndash;&gt;
                    <overwrite>true</overwrite>
                    &lt;!&ndash; 自动生成的配置 &ndash;&gt;
                    <configurationFile>
                        src/main/resources/mybatis-generator.xml</configurationFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

xml编写

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--数据库驱动jar -->
    <!--<classPathEntry-->
    <!--location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.45" />-->

    <context id="Tables" targetRuntime="MyBatis3">
        <!--去除注释 -->
        <!--<commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>-->

        <!--指定通用baseMapper-->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.bosssoft.hr.plugin.BaseMapper"/>
            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
            <property name="caseSensitive" value="false"/>
        </plugin>

        <!--数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost/exam" userId="root"
                        password="123456">
        </jdbcConnection>
        <!--默认false Java type resolver will always use java.math.BigDecimal if
            the database column is of type DECIMAL or NUMERIC. -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->
        <javaModelGenerator targetPackage="com.bosssoft.hr.beans"
                            targetProject="C:\workspaceIdea\boss-exam-online\boss-exam-online-pojo\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--生成SQLMAP文件 -->
        <sqlMapGenerator targetPackage="mybatis"
                         targetProject="C:\workspaceIdea\boss-exam-online\boss-exam-online-dao\src\main\resources">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.bosssoft.hr.mapper"
                             targetProject="C:\workspaceIdea\boss-exam-online\boss-exam-online-dao\src\main\java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->
        <table tableName="sys_answer" domainObjectName="SysAnswer" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />

    </context>
</generatorConfiguration>

代码生成

idea工具通过右侧的maven project 打开如下 双击mybatis-generator:generate
如图
springboot搭建通用mapper代码自动生成器_第1张图片

相关博客

https://blog.csdn.net/qq_22638399/article/details/83539934

你可能感兴趣的:(springboot搭建通用mapper代码自动生成器)