鱷魚筆記★如何在idea(intellij)社區版中使用mybatis中生成代碼


本文目的:

因為idea(intellij)破解費力,且破解不久之后就會被和諧,故比較了社區版和專業版,發現區別不大,於是選擇轉戰社區版idea,但是有些問題需要解決,比如 mybatis代碼生成(如下,只需兩步,輕松生成)當前使用為 springboot 2.5.6

maven 依賴

pom.xml
<plugin>
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-maven-plugin</artifactId>
	<version>1.3.2</version>
	<configuration>
		<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
		<verbose>true</verbose>
		<overwrite>true</overwrite>
	</configuration>
	<executions>
		<execution>
			<id>Generate MyBatis Artifacts</id>
			<goals>
				<goal>generate</goal>
			</goals>
		</execution>
	</executions>
	<dependencies>
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.2</version>
		</dependency>
	</dependencies>
</plugin>

生成配置 xml檔

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>
    <!--location 為本地 mysql jar 包地址,可去maven 倉庫取-->
    <classPathEntry
            location="E:/develop_app/repository/mysql/mysql-connector-java/8.0.27/mysql-connector-java-8.0.27.jar"/>
    <context id="my" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--以下為數據庫鏈接 driverClass  connectionURL userId   password 需修改-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://10.42.216.114:3306/smell?useUnicode=true"
                        userId="root"
                        password="123456"/>
        <!-- 以下為實體類 地址 targetPackage  targetProject -->
        <javaModelGenerator targetPackage="com.tp.strive.smell.model"
                            targetProject="E:\TP\idea\smell_store\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--以下為映射xml文件 路徑地址 targetPackage  targetProject -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="E:\TP\idea\smell_store\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 以下為dao 接口路徑地址 targetPackage  targetProject -->
        <javaClientGenerator targetPackage="com.tp.strive.smell.mapper"
                             targetProject="E:\TP\idea\smell_store\src\main\java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--以下為數據庫表 和實體類的對應關系 tableName 表名 domainObjectName 實體類類名  , mapper接口 和xml文件 會自動命名 -->
        <table tableName="smell_package_out" domainObjectName="SmellPackageOut"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--<columnRenamingRule searchString="^D_"
                                replaceString=""/>-->
        </table>

    </context>
</generatorConfiguration>

執行

點擊執行maven 的compile 功能,即可執行插件功能生成代碼

缺點

一次只能生成一張表,且重復生成的時候會xml檔不會覆蓋,而是會把生成的文檔寫入到原來的文檔中,造成方法重復(以上是本機環境下發生的問題)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM