第二篇 Springboot mybatis generate根据数据库表自动生成实体类、Mapper和Mapper.xml

源码链接:https://pan.baidu.com/s/1iP4UguBufHbcIEv4Ux4wDw
提取码:j6z9

目录结构如下:只需增加一个generatorConfig.xml文件和在pom.xml中配置下Mybatis generator代码生成插件即可

pom.xml中增加如下配置:

pom.xml中相关依赖及build插件部分代码

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

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <!-- maven工程在默认情况下src/main/java目录下的mapper文件是不发布到target目录下的 -->
    <!-- 增加下面resource配置将resources目录下的文件和java目录下的配置文件添加资源映射-->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- Mybatis generator代码生成插件 配置 -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.1</version>
            <configuration>
                <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                <overwrite>true</overwrite>
                <verbose>true</verbose>
            </configuration>
        </plugin>
    </plugins>
</build>

generatorConfig.xml文件内容如下:

<!--数据库驱动包路径 -->
<classPathEntry location="C:\Users\heave\.m2\repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/>

<context id="DB2Tables" targetRuntime="MyBatis3">
    <!--关闭注释 -->
    <commentGenerator>
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>

    <!--数据库连接信息 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/big_strong?characterEncoding=utf-8"
                    userId="root" password="123456">
    </jdbcConnection>

    <!--生成的model 包路径 -->
    <javaModelGenerator targetPackage="big.strong.disheng.user.model" targetProject="src/main/java">
        <property name="enableSubPackages" value="ture"/>
        <property name="trimStrings" value="false"/>
    </javaModelGenerator>

    <!--生成xml mapper文件 路径 -->
    <sqlMapGenerator targetPackage="big.strong.disheng.user.dao" targetProject="src/main/java">
        <property name="enableSubPackages" value="ture"/>
    </sqlMapGenerator>

    <!-- 生成的Dao接口 的包路径 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="big.strong.disheng.user.dao" targetProject="src/main/java">
        <property name="enableSubPackages" value="ture"/>
    </javaClientGenerator>
    <!--对应数据库表名 tableName数据库表名称,domainObjectName生成model类的名称 -->
    <table tableName="role" domainObjectName="Role" enableCountByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        <property name="useActualColumnNames" value="true"/>
    </table>
    <table tableName="permission" domainObjectName="Permission" enableCountByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        <property name="useActualColumnNames" value="true"/>
    </table>
    <table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        <property name="useActualColumnNames" value="true"/>
    </table>
    <table tableName="role_permission" domainObjectName="RolePermission" enableCountByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        <property name="useActualColumnNames" value="true"/>
    </table>
</context>

双击之后控制台会输出

完成

源码链接:https://pan.baidu.com/s/1iP4UguBufHbcIEv4Ux4wDw
提取码:j6z9

posted @   big-strong-yu  阅读(6086)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示