网站首页 > 技术文章 正文
前言
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-09-29 MybatisPlus—kotlin代码生成 mybatisplus 代码生成器
- 2024-09-29 Spring boot Mybatis 整合 springboot整合mybatis流程
- 2024-09-29 SpringBoot使用Mybatis-FreeMarker
- 2024-09-29 MyBatis自动生成Mapper插件 mybatis 生成mapper
- 2024-09-29 增强Mybatis常用方案 mybatis-plus扩展
- 2024-09-29 网易架构师吐血整理:2分钟看完Mybatis核心知识点
- 2024-09-29 Mybatis的XML映射文件的继承问题 mybatis映射文件的主要元素及作用
- 2024-09-29 Mybatis 自动生成bean mybatis 自动生成bo
- 2024-09-29 深入理解Python生成器(Generators)
- 2024-09-29 如何避免出现 SQL 注入漏洞 怎样避免sql注入
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)