其他分享
首页 > 其他分享> > mybaits两步生成代码+实例代码

mybaits两步生成代码+实例代码

作者:互联网

最近对自动生成代码有些许研究,上一篇是mybaits-plus,该文章是mybaits自动生成代码说明+实例代码,快点关注吧后期更精彩,如有问题可以关注讨论。

概述:

1、
在mybatis-generator-config.xml 配置相关数据库连接数据库名;
配置需要生成表
ps:重要属性配置在 <table> 节点下
实体列用 false:驼峰 true:与数据表列相同-->
<property name="useActualColumnNames" value="false"/>


2、运行生成代码
右侧找到Maven——Plugins-找到对应mybatis-generator-右键 Run Maven Build 就可以生成相应代码了

      3、实例代码下载:https://files.cnblogs.com/files/liyanbofly/mybaitsgenratecode.rar

下面进行介绍重点是 pom文件和mybatis-generator-config.xml 就可成功生成; 新建项目 配置pom和mybatis-generator-config.xml 及可Easy生成代码了。

1)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lyb.gengeratecode</groupId>
    <artifactId>mybaitsgenratecode</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
    </dependencies>



    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>*.sql</exclude>
                    <exclude>*.md</exclude>
                    <exclude>mybatis-generator-config.xml</exclude>
                </excludes>
            </resource>
        </resources>

        <plugins>
<!--            生成代码插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <dependencies>
                    <!-- 数据库驱动  -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.19</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.4.0</version>
                    </dependency>
                </dependencies>
<!--                指定对应生成代码配置文件-->
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <phase>none</phase>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

2)mybatis-generator-config.xml 放成resources 目录下

<?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="BuildingTables" targetRuntime="Mybatis3" defaultModelType="conditional">
        <!-- defaultModelType="hierarchical" -->
        <property name="javaFileEncoding" value="UTF-8" />
        <property name="suppressTypeWarnings" value="true" />

        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
        <plugin type="org.mybatis.generator.plugins.CachePlugin" />

        <!-- 注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="false" /><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳 -->
        </commentGenerator>

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/elanewhal_xh?serverTimezone=Asia/Shanghai"
                        userId="root" password="sa123" />
        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.lyb.generateCode.model"
                            targetProject="src/main/java">
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
            <property name="enableSubPackages" value="true" />

            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
            <property name="trimStrings" value="true" />
            <property name="rootClass" value="com.hualala.commons.mybatis.item.BaseItem"/>
        </javaModelGenerator>

        <!-- 生成mapxml文件 -->
        <sqlMapGenerator targetPackage="com.hualala.mendianbao.mapper.table"
                         targetProject="src/main/resources">
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成mapxml对应client,也就是接口dao -->
        <javaClientGenerator targetPackage="com.hualala.mendianbao.mapper.table"
                             targetProject="src/main/java" type="XMLMAPPER">
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="auth_relation" domainObjectName="Table" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--            实体列用 false:驼峰 true:与数据表列相同-->
            <property name="useActualColumnNames" value="false"/>
            <generatedKey column="itemID" sqlStatement="MySql" identity="true"/>
            <columnOverride column="action" javaType="java.lang.Integer" />
        </table>
    </context>

</generatorConfiguration>


 

标签:xml,generator,代码,生成,mybaits,实例,mybatis,config
来源: https://www.cnblogs.com/liyanbofly/p/16440550.html