行业资讯

MyBatis‑Generator 逆向工程(SSM Maven 聚合项目)

发布时间:2026/8/25 5:04:37
MyBatis‑Generator 逆向工程(SSM Maven 聚合项目) 一、项目模块结构说明采用 Maven 聚合架构父工程统一管理版本号聚合所有子模块。core 核心模块model、dao、service、mapper 映射文件、工具类逆向工程写在此模块。web 模块前台 / 后台依赖 core只写 Controller 和页面不复写底层 Dao、Service。MyBatis‑GeneratorMBG职责读取 MySQL 数据表自动生成实体 Model、Mapper 接口、Mapper.xml持久层代码不会生成 Service 层分页、多表 SQL 仍需手动写。二、Maven 依赖配置core 模块 pom.xml 引入逆向工程依赖版本由父工程统一管控。dependency groupIdorg.mybatis.generator/groupId artifactIdmybatis-generator-core/artifactId version1.3.6/version /dependency三、generatorConfig.xml 配置文件路径core/src/main/resources/generatorConfig.xml?xml version1.0 encodingUTF-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 idmybatisGenerator !--关闭自动注释-- commentGenerator property namesuppressAllComments valuetrue / /commentGenerator !--数据库连接配置-- jdbcConnection driverClasscom.mysql.jdbc.Driver connectionURLjdbc:mysql://localhost:3306/test?serverTimezoneCTTamp;useUnicodetrueamp;characterEncodingutf-8 userIdroot passwordroot /jdbcConnection javaTypeResolver property nameforceBigDecimals valuefalse / /javaTypeResolver !--生成实体类输出位置-- javaModelGenerator targetPackagecn.tx.model targetProject.\core\src\main\java property nameenableSubPackages valuefalse / property nametrimStrings valuetrue / /javaModelGenerator !--Mapper XML输出位置-- sqlMapGenerator targetPackagemapper targetProject.\core\src\main\resources property nameenableSubPackages valuefalse / /sqlMapGenerator !--Mapper接口输出位置-- javaClientGenerator typeXMLMAPPER targetPackagecn.tx.dao targetProject.\core\src\main\java property nameenableSubPackages valuefalse / /javaClientGenerator !--配置逆向表关闭Example复杂条件查询-- table tableNamesonger enableCountByExamplefalse enableUpdateByExamplefalse enableDeleteByExamplefalse enableSelectByExamplefalse selectByExampleQueryIdfalse / /context /generatorConfiguration四、Java 执行逆向工程core/src/test/java/cn/tx/util/Generator.javapackage cn.tx.util; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.util.ArrayList; import java.util.List; public class Generator { public void generator() throws Exception { ListString warnings new ArrayList(); boolean overwrite true; //多模块重点相对路径以父工程作为根目录 File configFile new File(core/src/main/resources/generatorConfig.xml); ConfigurationParser parser new ConfigurationParser(warnings); Configuration config parser.parseConfiguration(configFile); DefaultShellCallback callback new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { Generator generatorSqlmap new Generator(); generatorSqlmap.generator(); } }运行 main ()自动生成 model 实体、mapper 接口、mapper.xml 映射文件。五、MBG 常见踩坑汇总相对路径报错Maven 聚合项目IDEA 运行配置Working directory设置为父工程目录否则找不到配置 xml。时区异常jdbc 连接 url 必须配置serverTimezoneCTT高版本 mysql 驱动缺少会抛异常。文件覆盖风险overwritetrue会直接覆盖旧 mapper.xml手写的 SQL 会丢失执行前务必备份 xml。Linux 下 MySQL 区分表名大小写table tableNamexxx表名必须和数据库完全一致。生成代码后执行mvn clean compile编译生成 target 下 class 字节码否则报ClassNotFoundException。六、小结MyBatis‑Generator 是 MyBatis 官方工具只负责数据库表 → 持久层代码。只生成 model、mapper 接口、mapper.xmlService 层、业务 SQL 不会生成。Maven 多模块项目绝大多数异常根源是相对路径与编译缓存报错优先执行mvn clean。