工作中使用 mybatis 时我们需要根据数据表字段创建 pojo 类, mapper 文件以及 dao 类, 并且需要配置它们之间的依赖关系, 这样的工作很琐碎和重复, mybatis 官方也发现了这个问题, 因此给我们提供了 mybatis generator 工具来帮我们自动创建 pojo 类, mapper 文件以及 dao 类并且会帮我们配置好它们的依赖关系.
实际上, 最非常流行 MyBatis-Plus 中内置了代码生成器: 采用代码或者 Maven 插件可快速生成 Mapper , Model , Service , Controller 层代码, 支持模板引擎, 有超多自定义配置等, 在这主要介绍 Mybatis 的自动生成步骤.
插件依赖
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- <plugin>
- <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>6.0.6</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/generatorConfig.xml
- </configurationFile>
- </configuration>
- </plugin>
- </plugins>
- </build>
注意: MySQL-connector-java 的版本问题, 如果你的驱动是 com.MySQL.cj.jdbc.Driver, 你就需要 6.0.x 的版本. 如果是 com.MySQL.jdbc.Driver 则是 5.1.x 的版本.
配置 generatorConfig.xml
<?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>
- <!-- 导入属性配置 -->
- <properties resource="datasource.properties"></properties>
- <!-- context 是逆向工程的主要配置信息 -->
- <!-- id:name -->
- <!-- targetRuntime: 设置生成的文件适用于那个 mybatis 版本 -->
- <context id="default" targetRuntime="MyBatis3">
- <!-- 生成的 Java 文件的编码 -->
- <property name="javaFileEncoding" value="UTF-8"/>
- <!-- optional, 旨在创建 class 时, 对注释进行控制 -->
- <commentGenerator>
- <property name="suppressDate" value="true"/>
- <property name="suppressAllComments" value="true"/>
- </commentGenerator>
- <!--jdbc 的数据库连接 -->
- <jdbcConnection
- driverClass="${db.driverClassName}"
- connectionURL="${db.url}"
- userId="${db.username}"
- password="${db.password}">
- </jdbcConnection>
- <!-- 非必需, 类型处理器, 在数据库类型和 java 类型之间的转换控制 -->
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
- <!-- Model 模型生成器, 用来生成含有主键 key 的类, 记录类 以及查询 Example 类
- targetPackage 指定生成的 model 生成所在的包名
- targetProject 指定在该项目下所在的路径
- -->
- <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
- <javaModelGenerator targetPackage="com.ke.likehouse.model" targetProject="./src/main/java">
- <!-- 是否允许子包, 即 targetPackage.schemaName.tableName -->
- <property name="enableSubPackages" value="false"/>
- <!-- 是否对 model 添加 构造函数 -->
- <property name="constructorBased" value="true"/>
- <!-- 是否对类 CHAR 类型的列的数据进行 trim 操作 -->
- <property name="trimStrings" value="true"/>
- <!-- 建立的 Model 对象是否 不可改变 即生成的 Model 对象不会有 setter 方法, 只有构造方法 -->
- <property name="immutable" value="false"/>
- </javaModelGenerator>
- <!--mapper 映射文件生成所在的目录 为每一个数据库的表生成对应的 SqlMap 文件 -->
- <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
- <sqlMapGenerator targetPackage="mybatis/mappers" targetProject="./src/main/resources">
- <property name="enableSubPackages" value="false"/>
- </sqlMapGenerator>
- <!-- 客户端代码, 生成易于使用的针对 Model 对象和 XML 配置文件 的代码
- type="ANNOTATEDMAPPER", 生成 Java Model 和基于注解的 Mapper 对象
- type="MIXEDMAPPER", 生成基于注解的 Java Model 和相应的 Mapper 对象
- type="XMLMAPPER", 生成 SQLMap XML 文件和独立的 Mapper 接口
- -->
- <!-- targetPackage:mapper 接口 dao 生成的位置 -->
- <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
- <javaClientGenerator type="XMLMAPPER" targetPackage="com.ke.likehouse.dao" targetProject="./src/main/java">
- <!-- enableSubPackages: 是否让 schema 作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- </javaClientGenerator>
- <!-- 生成的表 -->
- <!--domainObjectName: 生成的 domain 类的名字, 如果不设置, 直接使用表名作为 domain 类的名字; 可以设置为 somepck.domainName, 那么会自动把 domainName 类再放到 somepck 包里面;-->
- <!--enableInsert(默认 true): 指定是否生成 insert 语句;-->
- <!--enableSelectByPrimaryKey(默认 true): 指定是否生成按照主键查询对象的语句 (就是 getById 或 get);-->
- <!--enableSelectByExample(默认 true):MyBatis3Simple 为 false, 指定是否生成动态查询语句;-->
- <!--enableUpdateByPrimaryKey(默认 true): 指定是否生成按照主键修改对象的语句 (即 update);-->
- <!--enableDeleteByPrimaryKey(默认 true): 指定是否生成按照主键删除对象的语句 (即 delete);-->
- <!--enableDeleteByExample(默认 true):MyBatis3Simple 为 false, 指定是否生成动态删除语句;-->
- <!--enableCountByExample(默认 true):MyBatis3Simple 为 false, 指定是否生成动态查询总条数语句 (用于分页的总条数查询);-->
- <!--enableUpdateByExample(默认 true):MyBatis3Simple 为 false, 指定是否生成动态修改语句 (只修改对象中不为空的属性);-->
- <table tableName="agent" domainObjectName="Agent"
- enableCountByExample="false"
- enableUpdateByExample="false"
- enableDeleteByExample="false"
- enableSelectByExample="false"
- selectByExampleQueryId="false">
- </table>
- <!-- geelynote mybatis 插件的搭建 -->
- </context>
- </generatorConfiguration>
提供 datasource.properties
- db.driverClassName = com.MySQL.cj.jdbc.Driver
- db.url = jdbc:MySQL://localhost:3306/twelve?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
- db.username = root
db.password = 你的密码
执行 maven 命令
方式一: 通过 IDEA 的 MAVEN 工具执行
方式二: 通过 MAVEN 命令
配置命令: mybatis-generator:generate -e
然后 Run 就好了:
可能出现的 BUG
如果你复制粘贴了代码却出现稀奇古怪的 BUG, 很有可能是:
你的 maven 的配置文件问题
引用的 MySQL-connector-java 与 driverClassName 版本不匹配
如果你的驱动是 com.MySQL.cj.jdbc.Driver, 你就需要 6.x.x 的版本. 如果是 com.MySQL.jdbc.Driver 则是 5.x.x 的版本.
来源: https://www.cnblogs.com/keeya/p/11247408.html