网站首页 > 技术文章 正文
前言
Mybatis-generator 可以生成关于 mybatis 的模板代码。在数据库表复杂的情况下,使用 mybatis-generator 生成 model 等工程必要类无疑是极其方便的。
生成的内容
最简单的:Model类、Dao接口、基于主键的基础查询mapper语句。
具体步骤
1、添加Maven依赖
mybatis-generator 其实是一个开发插件,运行插件就可以生成模板代码。
<plugin>
<!--自动生成的文件只限定于数据库表,对于复杂查询和Query不应该写在自动生成代码中-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
</configuration>
</plugin>
2、mybatis-generator
虽然看着比较长,但是比较简单,该有的配置项都在这里了。
<?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="DB2Tables" targetRuntime="MyBatis3">
<!--生成POJO时是否自动生成注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="xxxxxxxx"
userId="xxxx" password="xxxx">
</jdbcConnection>
<!--数据库BigDecimals字段在java中定义-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.xxxxxx.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/xxxxx">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类mapper.java存放位置-->
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.xxxxx.dao.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名
domainObjectName 是生成的model类名-->
<table tableName="xxxxx"
domainObjectName="xxxxxx"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
<!--添加属性useActualColumnNames为true,那么生成的对象字段就跟表一样-->
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
3、通过运行maven插件,生成模板文件
在 mybatis-generator.xml 文件中,右键单击,按图所示,找到 generator 插件,点击运行。
之后,程序就会在配置文件配置的地方,自动生成我们所需要的模板代码了。
猜你喜欢
- 2024-10-26 MybatisPlus —注解汇总(mybatis中注解)
- 2024-10-26 MyBatis使用需谨慎,看看这里有没有你曾踩到过的坑
- 2024-10-26 最新版本的MyBatis Plus 代码生成器使用指南
- 2024-10-26 解决mybatis动态生成sql错误的问题
- 2024-10-26 基于 MyBatis 的动态 SQL 技术详解
- 2024-10-26 mybatis插入获取主键的方式和原理
- 2024-10-26 Mybatis执行多条语句/批量更新方式
- 2024-10-26 MyBatis-Plus扫盲啦(mybatis plus vo)
- 2024-10-26 一文带你搞定mybatis的映射配置文件
- 2024-10-26 什么是mybatis-plus,你没用过吧,我刚学的,...
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 501℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 484℃MySQL service启动脚本浅析(r12笔记第59天)
- 463℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 462℃启用MySQL查询缓存(mysql8.0查询缓存)
- 442℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 422℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 418℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)