MyBatis Generator快速生成实体类和映射文件的方法

 更新时间:2023年10月23日 17:25:49   作者:陈亦康  
这篇文章主要介绍了MyBatis Generator快速生成实体类和映射文件的方法,通过示例代码介绍了MyBatis Generator 的使用,本文结合示例代码给大家介绍的非常详细,需要的朋友可以参考下

一、MyBatis Generator 的使用

1.1、生成类和映射文件

1.1.1、在 pom.xml 中引入依赖

在 properties 标签中加入版本号.

<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>

在 build => plugins 标签中加入如下配置

            <!-- mybatis ⽣成器插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis-generator-plugin-version}</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <!-- 相关配置 -->
                <configuration>
                    <!-- 打开⽇志 -->
                    <verbose>true</verbose>
                    <!-- 允许覆盖 -->
                    <overwrite>true</overwrite>
                    <!-- 配置⽂件路径 -->
                    <configurationFile>
                        src/main/resources/mybatis/generatorConfig.xml
                    </configurationFile>
                </configuration>
            </plugin>

上述配置中需要注意的是 “配置文件路径”,这个路径就是用来生成 实体类和映射文件 配置规则的位置.

1.1.2、根据 configurationFile 标签中配置的路径 创建 generatorConfig.xml 文件

这个文件就是用来描述生成规则的.

根据路径(src/main/resources/mybatis),在 mybatis 目录下创建 generatorConfig.xml 文件.

Ps:下述配置文件中需要修改的有 数据库连接、实体类和映射文件的路径、数据库表名

<?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>
    <!-- 驱动包路径,location中路径替换成⾃⼰本地路径 -->
    <classPathEntry location="D:\class\source\mysql-connector-java-5.1.49.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 禁⽤⾃动⽣成的注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!-- 连接配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/javanav_db?
characterEncoding=utf8&amp;useSSL=false"
                        userId="root"
                        password="1111">
        </jdbcConnection>
        <javaTypeResolver>
            <!-- ⼩数统⼀转为BigDecimal -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 实体类⽣成位置 -->
        <javaModelGenerator targetPackage="com.example.cyk.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 接口⽣成位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.example.cyk.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 配置⽣成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即
       可-->
        <table tableName="j_article" domainObjectName="Article"
               enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
               enableCountByExample="false"
               enableUpdateByExample="false">
            <!-- 类的属性⽤数据库中的真实字段名做为属性名, 不指定这个属性会⾃动转换 _ 为
           驼峰命名规则-->
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_article_reply" domainObjectName="ArticleReply"
               enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
               enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_board" domainObjectName="Board"
               enableSelectByExample="false" enableDeleteByExample="false"
               enableDeleteByPrimaryKey="false" enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_message" domainObjectName="Message"
               enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
               enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_user" domainObjectName="User"
               enableSelectByExample="false" enableDeleteByExample="false"
               enableDeleteByPrimaryKey="false" enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

注意:

驱动包路径是自己本地仓库的路径

但一定注意!! 需要在非中文的目录下,因此你可以把这个驱动包拷贝出来,放到一个非中文的目录中即可.

1.1.3、自动生成类 和 映射文件

重新加载Maven项⽬,在Plugins节点下出现mybatis-generator,双击运⾏,在对应的目录下⽣成相应的类与映射⽂件:

接着你就可以看到对应的生成了

1.1.4、在 Insert 标签中添加获取主键值的选项

在生成的 xml 文件中,给每一个 insert 标签都添加以下属性:useGeneratedKeys="true" keyProperty="id"

<!-- useGeneratedKeys = true -->
<!-- keyProperty = 主键字段--> 
 
<!-- 当插⼊⼀条数据后,可以通过user.getId()获取到⾃动⽣成的Id值,如果⽅法中需要⽴即获取Id值,加⼊这个配置 --> 
<insert id="insert" parameterType="com.example.cyk.model.User" 
useGeneratedKeys="true" keyProperty="id" >

Ps:这个选项也可以自动生成,但是不理想(有些问题)

1.1.5、扫描配置:添加 @Mapper 注解 / 添加扫描注解

有两种方式配置扫描 Mapper 接口.

1)给每个 mapper 包下的 mapper 接口都添加 @Mapper 注解.

2)给启动类上 或者 新建一个配置类(有 @Configuration 注解)加上 @MapperScan("com.example.cyk.mapper") 注解.

1.1.6、配置 mybatis

在 yml 文件中配置

mybatis:
  mapper-locations: classpath:mapper/**/*Mapper.xml

1.1.7、测试

@SpringBootTest
public class TestMapper {
 
    @Autowired
    private UserMapper userMapper;
 
    @Test
    public void select() {
        User user = userMapper.selectByPrimaryKey(1L);
        System.out.println(user);
    }
 
}

到此这篇关于MyBatis Generator如何快速生成实体类和映射文件的文章就介绍到这了,更多相关MyBatis Generator生成 实体类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java字符串中提取数字的方法

    java字符串中提取数字的方法

    Java中提取字符串中的数字,可以使用正则表达式或非正则表达式的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-06-06
  • 推荐几款非常实用的IDEA插件小结

    推荐几款非常实用的IDEA插件小结

    这篇文章主要介绍了推荐几款非常实用的IDEA插件小结,解决你开发中可望而又不好找的插件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • 非常实用的java自动答题计时计分器

    非常实用的java自动答题计时计分器

    这篇文章主要为大家详细介绍了非常实用的java自动答题计时计分器的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • java 如何把byte转化为KB、MB、GB的方法

    java 如何把byte转化为KB、MB、GB的方法

    这篇文章主要介绍了java 如何把byte转化为KB、MB、GB的方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • Java序列化与反序列化的实例分析讲解

    Java序列化与反序列化的实例分析讲解

    今天小编就为大家分享一篇关于Java序列化与反序列化的实例分析讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 关于Spring中的@Configuration中的proxyBeanMethods属性

    关于Spring中的@Configuration中的proxyBeanMethods属性

    这篇文章主要介绍了关于Spring中的@Configuration中的proxyBeanMethods属性,需要的朋友可以参考下
    2023-07-07
  • Sentinel热门词汇限流的实现详解

    Sentinel热门词汇限流的实现详解

    这篇文章主要介绍了使用Sentinel对热门词汇进行限流的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • SpringBoot如何通过自定义注解实现权限检查详解

    SpringBoot如何通过自定义注解实现权限检查详解

    这篇文章主要给大家介绍了关于SpringBoot如何通过自定义注解实现权限检查的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Java异常处理操作 Throwable、Exception、Error

    Java异常处理操作 Throwable、Exception、Error

    这篇文章主要介绍了Java异常处理操作 Throwable、Exception、Error,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Mybatis执行插入语句后并返回主键ID问题

    Mybatis执行插入语句后并返回主键ID问题

    这篇文章主要介绍了Mybatis执行插入语句后并返回主键ID问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03

最新评论