问题描述
之前用mybatis generator的自动生成代码的插件没有出现任何问题,但是前几天做作业时,通过generator插件自动生成代码,结果Springboot项目都启动不起来,结果通过日志一看是mybatis自动生成的映射文件出现了错误,出现了重复的节点:
<resultMap id="BaseResultMap" type="com.boss.redis.entity.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="pwd" jdbcType="VARCHAR" property="pwd" />
<result column="phone" jdbcType="CHAR" property="phone" />
<result column="address" jdbcType="VARCHAR" property="address" />
</resultMap>
<resultMap id="BaseResultMap" type="com.boss.redis.entity.User">
<result column="U_NO" jdbcType="INTEGER" property="uNo" />
<result column="U_USERNAME" jdbcType="VARCHAR" property="uUsername" />
<result column="U_PASSWORD" jdbcType="VARCHAR" property="uPassword" />
<result column="U_NICKNAME" jdbcType="VARCHAR" property="uNickname" />
<result column="U_ID" jdbcType="VARCHAR" property="uId" />
<result column="U_TYPE" jdbcType="INTEGER" property="uType" />
<result column="U_BALANCE" jdbcType="REAL" property="uBalance" />
</resultMap>
莫名其妙,第二个结点的表字段我都没有建立过。
问题解决
通过网上的查询发现很多人和我遇到了相同的问题,其解决方法是在generatorConfig.xml文件的<jdbcConnection>标签加入<property name="nullCatalogMeansCurrent" value="true" />标签。
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" userId="root" password="huang123">
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
官方解释
MySql does not properly support SQL catalogs and schema. If you run the create schema command in MySql, it actually creates a database - and the JDBC driver reports it back as a catalog. But MySql syntax does not support the standard catalog..table SQL syntax.
For this reason, it is best to not specify either catalog or schema in generator configurations. Just specify table names and specify the database in the JDBC URL.
If you are using version 8.x of Connector/J you may notice that the generator attempts to generate code for tables in the MySql information schemas (sys, information_schema, performance_schema, etc.) This is probably not what you want! To disable this behavior, add the property "nullCatalogMeansCurrent=true" to your JDBC URL.