MyBatis逆向工程
MyBatis逆向工程 一个表生成一个实体
1. 引入依赖
pom.xml
<!--Mybatis代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.3</version>
<configuration>
<!--配置: 文件的位置 和 文件名 !!! 当前的mybatis-generator.xml在根目录下【src同级】!!!-->
<configurationFile>mybatis-generator.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
2. 创建配置文件mybatis-generator.xml
mybatis-generator.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>
<!--指定连接数据库的JDBC驱动包所在位置,指定到你本机的完成路径
注意:当前的版本要和pom.xml中配置mysql-connector-java版本一致
-->
<classPathEntry
location="C:\Users\abc\.m2\repository\mysql\mysql-connector-java\8.0.29\mysql-connector-java-8.0.29.jar"/>
<!--配置table表信息内容体,targetRuntime指定采用MyBatis3的版本
defaultModelType :flat 该模型为每一张表只生成一个实体类(解决一表生成多个实体)
-->
<context id="tables" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 配置实现序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!--抑制生成注释,由于生成的注释都是英文的,可以不让它生成-->
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/study?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"
userId="root"
password="123456">
<!--解决映射文件出现重复多余字段-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成model类,实体Bean
targetPackage 指定model类的包名
targetProject:生成model类的位置 -->
<javaModelGenerator targetPackage="com.guo.springboot.model"
targetProject="D:\SpringBoot\day15\02-springboot-dubbo-ssm-interfacce\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 实体映射文件 Mapper.xml文件
targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.guo.springboot.mapper"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成MyBatis的Mapper接口类文件, DAO接口
targetPackage指定Mapper接口类的名
targetPackage:指定生成的Mapper接口放的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.guo.springboot.mapper"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--指定数据库-->
<!-- 数据库表名及对应的Java模型类名 -->
<table tableName="student" domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
<!-- <table schema="" tableName="admin"></table>
<table schema="" tableName="product_info"></table>
<table schema="" tableName="product_type"></table>
<table schema="" tableName="users"></table>-->
</context>
</generatorConfiguration>
3. 运行插件
3.1 双击运行
3.2 建立成功——BUILD SUCCESS
3.3 生成一个实体Bean 映射文件 DAO接口
3.3 生成多个实体
defaultModelType属性不设置默认是conditional
此时主键生成一个XXKey对象(key class),
4. 总结
4.1 解决一表生成多个实体
逆向工程生成实体的个数与数据库表设计的复杂度有关系,默认会根据表中主键和blob来确定创建实体的个数,,如果只需要一个实体还是可以通过生成器配置文件来设置的。
MyBatis Generator配置文件context元素有一个defaultModelType属性,这个属性的值会影响实体类(或叫domain类,model类)的生成。
defaultModelType:指定生成对象的样式——支持conditional、 flat、hierarchical3个值:
-
conditional 这是默认值
这个模型与hierarchical模型相似,除了如果一个实体类只包含一个字段,则不会单独生成此实体类。因此,如果一个表的主键只有一个字段,那么不会为该字段生成单独的实体类,会将该字段合并到基本实体类中。 -
flat
该模型为每一张表只生成一个实体类
。所有内容(主键,blob)等全部生成在一个对象中;一般使用这个模型就够了。 -
hierarchical
如果表有主键,
主键生成一个XXKey对象(key class),
Blob等单独生成一个对象,其他简单属性在一个对象中(record class)
MBG会在所有生成的实体类之间维护一个继承关系。
显然这个模型比较复杂。
4.2 解决生成映射文件中重复出现BaseResultMap
Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.guo.springboot.mapper.StudentMapper.BaseResultMap
逆向工程生成的映射文件(StudentMapper.xml)中BaseResultMap重复出现,mapper.xml文件中也会出现关于这些字段的SQL语句
解决办法:在生成器的mybatis-generator.xml配置文件里的数据库连接地址中添加:nullCatalogMeansCurrent参数(<property name="nullCatalogMeansCurrent" value="true"/>
)