SpringBoot环境中使用MyBatis代码生成工具

一、Maven配置文件中添加如下依赖

<dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>
<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.4</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.38</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>
            </plugin>

二、添加generatorConfig.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 >
    <!-- mysql jar 文件位置 -->
    <classPathEntry location="F:/instrument\maven/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar" />
    <context id="store" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
            <!-- 是否去除所有自动生成的文件的时间戳,默认为false -->
            <!-- <property name="suppressDate" value="false"/> -->
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/g"
                        userId="root"
                        password="root">
        <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://112.35.82.91:3360/benevolenceParking"
                        userId="renrenbang"
                        password="05377975333">-->
        </jdbcConnection>
        <!-- targetPackage:包名称(自定义)  targetProject:项目路径(自定义)   -->
        <javaModelGenerator targetPackage="com.zblx.park.po" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格  -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 配置生成相应的实体Mapper.xml,对于Mapper3.X我们需要把type="XMLMAPPER" -->
        <!-- targetPackage:包名称(自定义)  targetProject:项目路径(自定义)   -->
        <sqlMapGenerator targetPackage="com.zblx.park.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- 配置生成相应的接口类,对应与Mapper.xml中的一系列CRUD方法SQL语句 -->
        <!-- targetPackage:包名称(自定义)  targetProject:项目路径(自定义)   -->
        <javaClientGenerator targetPackage="com.zblx.park.dao" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 用户表-->
        <!--<table schema="benevolenceParking" tableName="parking" domainObjectName="Parking"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"
        >-->
        <table schema="fruit" tableName="parking" domainObjectName="Parking"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"
        />
        <table schema="fruit" tableName="orders" domainObjectName="Orders"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"
        >
        </table>
        <table schema="fruit" tableName="car" domainObjectName="Car"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"
        >
        </table>
        <table schema="fruit" tableName="carlock" domainObjectName="CarLock"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"
        >
        </table>
    </context>
</generatorConfiguration>

转载于:https://blog.51cto.com/14262994/2394331

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 MyBatis 自动生成代码可以大大提高数据库访问层的开发效率。下面是基于 Spring Boot 框架的 MyBatis 自动生成代码的简单使用方法: 1.在 pom.xml 文件添加相关依赖: ```xml <!-- MyBatis 代码生成器 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> ``` 2.配置 MyBatis 代码生成器的插件: 在 pom.xml 文件添加以下插件配置: ```xml <build> <plugins> <!-- MyBatis 代码生成器插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> </dependencies> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> ``` 3.配置 MyBatis 代码生成器的属性: 在 pom.xml 文件添加以下配置: ```xml <properties> <!-- MyBatis 代码生成器配置 --> <mybatis.generator.configFile>src/main/resources/generatorConfig.xml</mybatis.generator.configFile> </properties> ``` 4.编写 MyBatis 代码生成器的配置文件: 在 src/main/resources 目录下创建 generatorConfig.xml 文件,并添加以下内容: ```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> <context id="Mysql" targetRuntime="MyBatis3"> <!-- 数据库连接配置 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis_demo?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai" userId="root" password="123456"/> <!-- 生成 Model 的包名和位置 --> <javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成 Mapper.xml 的包名和位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成 Mapper.java 的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 生成表对应的 Model、Mapper.xml 和 Mapper.java --> <table tableName="user" domainObjectName="User"></table> </context> </generatorConfiguration> ``` 以上配置文件会生成 User.java、UserMapper.xml 和 UserMapper.java 三个文件,并分别放在 com.example.demo.model、mapper 和 com.example.demo.mapper 包下。 5.执行 MyBatis 代码生成器: 在 IDEA 执行 Maven 工具,选择 mybatis-generator-maven-plugin 插件的 generate 目标,即可自动生成代码。 执行完毕后,可以在 IDEA 工程的相应目录下看到生成的代码文件。 以上就是在 Spring Boot 使用 MyBatis 自动生成代码的基本方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值