mybatis-generator插件自动生成mapper及其实体模型配置

先赞后看,养成习惯!!! ^ _ ^ ❤️ ❤️ ❤️
码字不易,大家的支持就是我坚持下去的动力,点赞后不要忘记关注我哦

📕本系列文章为本人在学习路上遇到的问题和解决方法,在这里撰写成文是为了巩固知识和帮助其他友友

个人主页:伯明翰java

如有错误,请您指正批评 ^ _ ^

目录结构

目录结构:创建如下文件夹及配置文件(springBoot)。内容见下

pom文件中引入依赖

在properties标签中加⼊版本号:

java 复制代码
<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>

在build-->plugins标签中加⼊如下配置:

java 复制代码
<!-- 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>

创建generatorConfig.xml

在src/main/resources下创建mybatis⽬录,在mybatis⽬录下创建generatorConfig.xml⽂

件,内容如下:

java 复制代码
<?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:\database\jar\mysql-connector-java5.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/forum_db?
characterEncoding=utf8&amp;useSSL=false"
 userId="root"
 password="123456">
 </jdbcConnection>
 <javaTypeResolver>
 <!-- ⼩数统⼀转为BigDecimal --> 
 <property name="forceBigDecimals" value="false"/>
 </javaTypeResolver>
 <!-- 实体类⽣成位置 --> 
 <javaModelGenerator targetPackage="com.bitejiuyeke.forum.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>
 <!-- DAO类⽣成位置 --> 
 <javaClientGenerator type="XMLMAPPER" 
targetPackage="com.bitejiuyeke.forum.dao" targetProject="src/main/java">
 <property name="enableSubPackages" value="true"/>
 </javaClientGenerator>
 <!-- 配置⽣成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即
可--> 
 <table tableName="t_article" domainObjectName="Article" 
enableSelectByExample="false"
 enableDeleteByExample="false" enableDeleteByPrimaryKey="false" 
enableCountByExample="false"
 enableUpdateByExample="false">
 <!-- 类的属性⽤数据库中的真实字段名做为属性名, 不指定这个属性会⾃动转换 _ 为
驼峰命名规则--> 
 <property name="useActualColumnNames" value="true"/>
 </table>
 <table tableName="t_article_reply" domainObjectName="ArticleReply" 
enableSelectByExample="false"
 enableDeleteByExample="false" enableDeleteByPrimaryKey="false" 
enableCountByExample="false"
 enableUpdateByExample="false">
 <property name="useActualColumnNames" value="true"/>
 </table>
 <table tableName="t_board" domainObjectName="Board" 
enableSelectByExample="false" enableDeleteByExample="false"
 enableDeleteByPrimaryKey="false" enableCountByExample="false" 
enableUpdateByExample="false">
 <property name="useActualColumnNames" value="true"/>
 </table>
 <table tableName="t_message" domainObjectName="Message" 
enableSelectByExample="false"
 enableDeleteByExample="false" enableDeleteByPrimaryKey="false" 
enableCountByExample="false"
 enableUpdateByExample="false">
 <property name="useActualColumnNames" value="true"/>
 </table>
 <table tableName="t_user" domainObjectName="User" 
enableSelectByExample="false" enableDeleteByExample="false"
 enableDeleteByPrimaryKey="false" enableCountByExample="false" 
enableUpdateByExample="false">
 <property name="useActualColumnNames" value="true"/>
 </table>
 </context>
</generatorConfiguration>

运⾏插件⽣成⽂件

在src/main/resources下创建mapper⽬录

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

配置mybatis扫描路径

application.yml中加⼊mybatis配置

配置成功后就可以扫描mapper目录下的所有xml文件

相关推荐
修炼前端秘籍的小帅17 分钟前
精读《JavaScript 高级程序设计 第4版》第6章 集合引用类型(三)Map、WeakMap、Set、WeakSet
开发语言·javascript·ecmascript
@LetsTGBot搜索引擎机器人29 分钟前
打造属于你的 Telegram 中文版:汉化方案 + @letstgbot 搜索引擎整合教程
开发语言·python·搜索引擎·机器人·.net
人工智能的苟富贵39 分钟前
使用 Tauri + Rust 构建跨平台桌面应用:前端技术的新边界
开发语言·前端·rust·electron
lkbhua莱克瓦2440 分钟前
Java基础——常用API2
java·笔记·github·学习方法
j_xxx404_40 分钟前
C++ STL:string类(3)|operations|string类模拟实现|附源码
开发语言·c++
摇滚侠1 小时前
Spring Boot3零基础教程,Lambda 表达式与函数式接口,笔记95
java·spring boot·笔记
好学且牛逼的马1 小时前
【JavaWeb|day19 Web后端进阶 SpringAOP、SpringBoot原理、自定义Starter、Maven高级】
java·spring boot·rpc
码界奇点1 小时前
Java 开发日记MySQL 与 Redis 双写一致性策略挑战与实战解析
java·redis·sql·mysql·java-ee
GHZero1 小时前
Java 之解读String源码(九)
java·开发语言
Swift社区1 小时前
Lombok 不生效 —— 从排查到可运行 Demo(含实战解析)
java·开发语言·安全