mysql数据库中mybaties逆向工程生成实体类,以及xml文件中出现重复resultmap解决办法。

1.创建springBoot项目,生成mybatis-generator.xml文件
1在这里插入图片描述
2.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>
<context id="DB2Tables" targetRuntime="MyBatis3">
	<commentGenerator>
		<property name="suppressAllComments" value="true"/>
	</commentGenerator>
	<jdbcConnection driverClass="com.mysql.jdbc.Driver"
		connectionURL="jdbc:mysql://localhost:3306/v13?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC
" userId="root" password="123">
	</jdbcConnection>
	<javaTypeResolver>
		<property name="forceBigDecimals" value="false" />
	</javaTypeResolver>
	<!-- 设置Java类生成的位置 -->
	<javaModelGenerator targetPackage="com.q.v13.entity"
		targetProject=".\src\main\java">
		<property name="enableSubPackages" value="true" />
		<property name="trimStrings" value="true" />
	</javaModelGenerator>

	<sqlMapGenerator targetPackage="com.q.v13.mapper"
		targetProject=".\src\main\resources">
		<property name="enableSubPackages" value="true" />
	</sqlMapGenerator>

	<javaClientGenerator type="XMLMAPPER"
		targetPackage="com.q.springboothello.mapper" targetProject=".\src\main\resources">
		<property name="enableSubPackages" value="true" />
	</javaClientGenerator>

	<table tableName="t_product_type"
		enableCountByExample="false" enableUpdateByExample="false"
		enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
	<table tableName="t_product"
		   enableCountByExample="false" enableUpdateByExample="false"
		   enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
	<table tableName="t_product_desc"
		   enableCountByExample="false" enableUpdateByExample="false"
		   enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>

3.在测试类中写main方法,完成生成entity和mapper文件.

		public class MBG {
	public static void main(String[] args) throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("mybatis-generator.xml");
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
		}
	}

4.注意四点:

1.java和source目录要清空
2.链接数据库依赖要和数据库版本统一
3.数据库时间格式

在这里插入图片描述

4.资源路径

在这里插入图片描述

二:以及xml文件中出现重复resultmap解决办法。

产生原因:链接到数据库的当前用户名下有多个相同的表名;它会扫描所有的数据库找到指定名称的tableName然后生成xml文件。

解决办法:百度很多方法说在指定表的时候加schema字段指定用户名。其实没有用,因为数据库连接的时候的用户名已经指定:	<jdbcConnection driverClass="com.mysql.jdbc.Driver"
		connectionURL="jdbc:mysql://localhost:3306/v13?useUnicode=true&amp;characterEncoding=UTF-" userId="root" password="123">
		在这儿已经指定为root

有效方法:在mysql数据库新建用户test,只给需要操作的数据库的权限到当前用户,然后用新创建的用户链接数据库生成实体类<jdbcConnection driverClass="com.mysql.jdbc.Driver"
		connectionURL="jdbc:mysql://localhost:3306/v13?useUnicode=true&amp;characterEncoding=UTF-" userId="test" password="test">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值