当前位置: 首页 > news >正文

Mybatis07-逆向工程

MyBatis 的逆向工程(MyBatis Generator, MBG),帮助你根据数据库表自动生成 Java 实体类、Mapper 接口、XML 映射文件,极大提高开发效率。

 

一、什么是 MyBatis 逆向工程?

MyBatis 逆向工程(MBG)是官方提供的代码生成工具,用于自动从数据库表生成对应的 Java 代码,包括:

自动生成内容示例说明
实体类(Model)User.java 对应 t_user
Mapper 接口UserMapper.java
Mapper XML 文件UserMapper.xml,内含 select/insert/update/delete 语句

 

二、使用方式(标准 Maven 项目)

步骤 1:添加 Maven 插件

<build><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.4.1</version><configuration><!-- 允许覆盖 --><overwrite>true</overwrite></configuration><!-- 插件的依赖 --><dependencies><!-- JDBC 驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency></dependencies></plugin></plugins></build>

 

步骤 2:创建配置文件 generatorConfig.xml

1、配置文件名只能是generatorConfig.xml

2、配置文件只能放到resources文件夹下!

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!--targetRuntime有两个值:MyBatis3Simlpe: 生成的是基础版,只有基本的增删改查MyBatis3:生成的是增强版,除了基本的增删改查之外还有复杂的增删改查--><context id="MyContext" targetRuntime="MyBatis3"><!-- 防止生成重复代码 --><plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /><!-- 注释信息的生成 --><commentGenerator><!-- 是否去掉生成日期 --><property name = "suppressDate" value="true" /><!-- 是否去除注释 --><property name="suppressAllComments" value="true"/></commentGenerator><!-- 数据库连接信息 --><jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/wsbazinga?serverTimezone=UTC"userId="root"password="" /><!-- JavaBean 生成位置 --><javaModelGenerator targetPackage="com.wsbazinga.advance.pojo"targetProject="src/main/java" ><!-- 是否开启了子包 --><property name="enableSubPackages" value="true"/><!-- 是否去除字段名前后空白 --><property name="trimStrings" value="true"/></javaModelGenerator><!-- Mapper XML 文件位置 --><sqlMapGenerator targetPackage="com.wsbazinga.advance.mapper"targetProject="src/main/resources" ><!-- 是否开启了子包 --><property name="enableSubPackages" value="true"/></sqlMapGenerator><!-- Mapper 接口生成位置 --><javaClientGenerator type="XMLMAPPER"targetPackage="com.wsbazinga.advance.mapper"targetProject="src/main/java" ><!-- 是否开启了子包 --><property name="enableSubPackages" value="true"/></javaClientGenerator><!-- 要生成的表 --><table tableName="t_teacher" domainObjectName="Teacher"/></context>
</generatorConfiguration>

【注意】:

若是:targetRuntime="MyBatis3",会生成xxxExample.java文件,在pojo同目录下,条件查询构建器类。

这是因为:MyBatis3 模式默认生成 基于 Example 的动态查询方法,比如:

List<User> selectByExample(UserExample example);
int deleteByExample(UserExample example);

这个 UserExample 类的作用是帮助你以 Java 对象方式动态构建复杂 WHERE 条件语句,替代你手动写 SQL 条件,非常适合多条件拼接查询。

 

Example 类作用演示:

比如你想查询年龄大于 25 且 name 包含 "wang" 的用户,传统写法是写 SQL:

SELECT * FROM user WHERE age > 25 AND name LIKE '%wang%'

 而使用 Example 的写法是:

UserExample example = new UserExample();
UserExample.Criteria criteria = example.createCriteria();
criteria.andAgeGreaterThan(25);
criteria.andNameLike("%wang%");List<User> users = userMapper.selectByExample(example);

QBC风格:query by criteria一种查询方式,比较面向对象,看不到sql语句。 

 

步骤 3:使用

直接双击,即可。

http://www.dtcms.com/a/283463.html

相关文章:

  • 《python语言程序设计》2018版第8章5题编写函数统计特定不重复字符串s2在s1中的出现次数
  • C#获取当前系统账户是否为管理员账户
  • 资源利用率提升50%:Serverless 驱动国诚投顾打造智能投顾新范式
  • 用Amazon Q Developer助力Python快捷软件开发
  • EMS4000/EMS3900/EMS4100/EMS3157/EMS/23157高性能高质量模拟开关芯片
  • Go语言自学笔记(2.3-2.6)
  • C++:vector(2)之vector的模拟实现
  • 使用 SeaTunnel 建立从 MySQL 到 Databend 的数据同步管道
  • [2025CVPR-图象检索方向]CCIN:用于合成图像检索的合成冲突识别和中和模型
  • OWASP Top 10 攻击场景实战
  • 简单易懂,什么是连续分配管理方式
  • Vue3+Ts实现父子组件间传值的两种方式
  • 设计模式之【观察者模式】
  • 多维动态规划题解——不同路径【LeetCode】记忆化搜索
  • ai 编程工具,简单总结
  • 16路串口光纤通信FPGA项目实现指南 - 第二部分(下)
  • Day36 Java方法和流程控制练习 计算器
  • Linux运维新手的修炼手扎之第19天
  • Linux内核设计与实现 第1章:内核简介
  • UDP和TCP的主要区别是什么?
  • --- Bean 的生命周期 ---
  • Redis键过期后会立即删除吗
  • 光环效应(HALO Effect)
  • MySQL高并发高可用架构设计与实现:主从复制与读写分离
  • x86版Ubuntu的容器中运行ARM版Ubuntu
  • 3分钟实战!用DeepSeek+墨刀AI生成智能对话APP原型图
  • Karate(Java)接口自动化测试框架
  • 代码随想录算法训练营第五十天|图论part1
  • 【图像处理基石】如何入门人体关键点检测?
  • 迁移学习--基于torchvision中VGG16模型的实战