MyBatis逆向工程(Maven)(解决因同名表生成mapper重复问题)

文章目录

    • 依赖的jar包
    • maven的逆向工程插件
    • 配置文件(generatorConfig.xml)
    • 问题:MyBatis Generator 生成器把其他数据库的同名表生成下来的问题
    • 配置文件2:pojo实现序列化和toString

依赖的jar包


	 <dependency>
         <groupId>mysqlgroupId>
             <artifactId>mysql-connector-javaartifactId>
             <version>8.0.13version>
      dependency>


        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.5version>
        dependency>

maven的逆向工程插件

<build>
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.7version> 
                <configuration>
                    
                    <configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
                    <overwrite>trueoverwrite>
                    <verbose>trueverbose>
                configuration>
                 <dependencies>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>8.0.13version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

配置文件(generatorConfig.xml)




<generatorConfiguration>

    
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="false" />
            
            <property name="suppressDate" value="true"/>
            
            <property name="addRemarkComments" value="true"/>
        commentGenerator>

        
        <jdbcConnection
                driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/test1"
                userId="root"
                password="123">
            
            <property name="serverTimezone" value="UTC"/>
            
            
            <property name="nullCatalogMeansCurrent" value="true"/>
            
            <property name="useSSL" value="false"/>
        jdbcConnection>

        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetProject=".\src" targetPackage="mbg.bean">
            
            <property name="enableSubPackages" value="false" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>

        
        <sqlMapGenerator targetProject=".\src" targetPackage="mbg.dao">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER" targetProject=".\src" targetPackage="mbg.dao">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>



        
        <table  tableName="role_sources" domainObjectName="RoleSources">table>
		......
		配置要生成的表
		......



    context>
generatorConfiguration>

问题:MyBatis Generator 生成器把其他数据库的同名表生成下来的问题

在生成完成后,有的表的mapper.xml会有重复的代码。是因为在其他库里有同名的表,导致他生成了两遍。
:“Result Maps collection already contains value for com.xxx.dao.tb_userMapper.BaseResultMap”

解决方法是在生成器的配置文件里的数据库连接地址中添加下列参数:

nullCatalogMeansCurrent=true

问题解决办法引用:https://blog.csdn.net/smallnetter/article/details/94593605

加到配置文件中:


<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/security"
                userId="root"
                password="root">
    
    <property name="serverTimezone" value="UTC"/>
    
    
    <property name="nullCatalogMeansCurrent" value="true"/>
jdbcConnection>

配置文件2:pojo实现序列化和toString




<generatorConfiguration>
	<context id="testTables" targetRuntime="MyBatis3">

		
		<property name="javaFileEncoding" value="UTF-8" />
		
		<property name="javaFormatter"
			value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
		
		<property name="xmlFormatter"
			value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />

		
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

		
		<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />


		<commentGenerator>
			
			<property name="suppressAllComments" value="true" />

		commentGenerator>

		
		<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/youlexuandb" userId="root" password="123">
			
			<property name="serverTimezone" value="UTC"/>
			
			
			<property name="nullCatalogMeansCurrent" value="true"/>
			
			<property name="useSSL" value="false"/>
		jdbcConnection>

		
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		javaTypeResolver>

		
		<javaModelGenerator targetPackage="com.offcn.pojo"
			targetProject=".\src">
			
			<property name="enableSubPackages" value="false" />
			
			<property name="trimStrings" value="true" />
		javaModelGenerator>
		
		
		<sqlMapGenerator targetPackage="com.offcn.mapper"
			targetProject=".\resource">
			
			<property name="enableSubPackages" value="false" />
		sqlMapGenerator>
		
		
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.offcn.mapper" targetProject=".\src">
			
			<property name="enableSubPackages" value="false" />
		javaClientGenerator>

		
		<table schema="" tableName="tb_address">table>
		<table schema="" tableName="tb_areas">table>
		<table schema="" tableName="tb_brand">table>
		<table schema="" tableName="tb_cities">table>
		<table schema="" tableName="tb_content">table>
		<table schema="" tableName="tb_content_category">table>
		<table schema="" tableName="tb_freight_template">table>
		<table schema="" tableName="tb_goods">table>
		<table schema="" tableName="tb_goods_desc">table>
		<table schema="" tableName="tb_item">table>
		<table schema="" tableName="tb_item_cat">table>
		<table schema="" tableName="tb_order">table>
		<table schema="" tableName="tb_order_item">table>
		<table schema="" tableName="tb_pay_log">table>
		<table schema="" tableName="tb_provinces">table>
		<table schema="" tableName="tb_seckill_goods">table>
		<table schema="" tableName="tb_seckill_order">table>
		<table schema="" tableName="tb_seller">table>
		<table schema="" tableName="tb_specification">table>
		<table schema="" tableName="tb_specification_option">table>
		<table schema="" tableName="tb_type_template">table>
		<table schema="" tableName="tb_user">table>

	context>

generatorConfiguration>

你可能感兴趣的:(mybatis学习)