使用mybatis插件自動生成代碼以及問題處理


1.pom.xml中加入依賴插件

 1         <!-- mybatis generator 自動生成代碼插件 -->
 2             <plugin>
 3                 <groupId>org.mybatis.generator</groupId>
 4                 <artifactId>mybatis-generator-maven-plugin</artifactId>
 5                 <version>1.3.2</version>
 6                 <configuration>
 7                     <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
 8                     <overwrite>true</overwrite>
 9                     <verbose>true</verbose>
10                 </configuration>
11             </plugin>

 

2. 在項目${basedir}/src/main/resources/generator/目錄下新建generatorConfig.xml文件,內容如下:

generatorConfig.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 <generatorConfiguration>
 6     <!-- 數據庫驅動:選擇本地硬盤上面的數據庫驅動包-->
 7     <classPathEntry  location="E:\mvn\repository\mysql\5.1.2\lib\mysql-connector-java-5.1.25.jar"/>
 8     <context id="DB2Tables"  targetRuntime="MyBatis">
 9         <commentGenerator>
10             <property name="suppressDate" value="true"/>
11             <!-- 是否去除自動生成的注釋 true:是;false:否 -->
12             <property name="suppressAllComments" value="false"/>
13         </commentGenerator>
14         <!--數據庫鏈接URL,用戶名、密碼 -->
15         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/orcl" userId="root" password="root">
16         </jdbcConnection>
17         <javaTypeResolver>
18             <property name="forceBigDecimals" value="false"/>
19         </javaTypeResolver>
20         <!-- 生成模型的包名和位置-->
21         <javaModelGenerator targetPackage="main.java.com.coderzz.model" targetProject="src">
22             <property name="enableSubPackages" value="true"/>
23             <property name="trimStrings" value="true"/>
24         </javaModelGenerator>
25         <!-- 生成映射文件的包名和位置-->
26         <sqlMapGenerator targetPackage="main.resources.com.coderzz.mapping" targetProject="src">
27             <property name="enableSubPackages" value="true"/>
28         </sqlMapGenerator>
29         <!-- 生成DAO的包名和位置-->
30         <javaClientGenerator type="XMLMAPPER" targetPackage="main.java.com.coderzz.mapper" targetProject="src">
31             <property name="enableSubPackages" value="true"/>
32         </javaClientGenerator>
33         <!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名-->
34         <table tableName="person" domainObjectName="Person" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">            </table>
35     </context>
36 </generatorConfiguration>

3. 點擊Run->Edit Configurations

Edit Configurations

4. 新建Maven配置

RUN Configurations

5. 運行

RUN

結果提示以下錯誤,根據錯誤來看,提示No plugin found for prefix 'mybatis-generator' in the current project,說明插件不存在,檢查本地maven倉庫發現,該插件已正常存在,尋找其它解決方法。

C:\java\jdk1.8.0_112\bin\java -Dmaven.multiModuleProjectDirectory=E:\Projects\war-fx-mock--bak "-Dmaven.home=D:\Program Files\apache-maven-3.3.9" "-Dclassworlds.conf=D:\Program Files\apache-maven-3.3.9\bin\m2.conf" -Didea.launcher.port=7536 "-Didea.launcher.bin.path=D:\Program Files\IntelliJIDEA\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\apache-maven-3.3.9\boot\plexus-classworlds-2.5.2.jar;D:\Program Files\IntelliJIDEA\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=2016.2.5 -s "D:\Program Files\apache-maven-3.3.9\conf\settings-nexus3_5_2.xml" -Dmaven.repo.local=E:\mvn\repository mybatis-generator:generate -e
[INFO] Error stacktraces are turned on.
[WARNING] 
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'pluginRepositories' (position: START_TAG seen ...</servers>  \n\n     <pluginRepositories>... @33:26)  @ D:\Program Files\apache-maven-3.3.9\conf\settings-nexus3_5_2.xml, line 33, column 26
[WARNING] 
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.mobanker.mock:mock-service:war:1.0.0-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework.boot:spring-boot-starter-web:jar -> duplicate declaration of version (?) @ com.mobanker.mock:mock-service:[unknown-version], E:\Projects\war-fx-mock--bak\mock-service\pom.xml, line 81, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] fx-mock
[INFO] mock-common
[INFO] mock-core
[INFO] mock-service
Downloading: http://test.nexus.ql.corp/repository/public/org/codehaus/mojo/maven-metadata.xml
Downloading: http://test.nexus.ql.corp/repository/public/org/apache/maven/plugins/maven-metadata.xml
Downloaded: http://test.nexus.ql.corp/repository/public/org/codehaus/mojo/maven-metadata.xml (22 KB at 80.7 KB/sec)
Downloaded: http://test.nexus.ql.corp/repository/public/org/apache/maven/plugins/maven-metadata.xml (14 KB at 49.4 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] fx-mock ............................................ SKIPPED
[INFO] mock-common ........................................ SKIPPED
[INFO] mock-core .......................................... SKIPPED
[INFO] mock-service ....................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.617 s
[INFO] Finished at: 2018-02-08T16:21:02+08:00
[INFO] Final Memory: 23M/230M
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix 'mybatis-generator' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (E:\mvn\repository), central (http://test.nexus.ql.corp/repository/public/)] -> [Help 1]
org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException: No plugin found for prefix 'mybatis-generator' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (E:\mvn\repository), central (http://test.nexus.ql.corp/repository/public/)]
    at org.apache.maven.plugin.prefix.internal.DefaultPluginPrefixResolver.resolve(DefaultPluginPrefixResolver.java:93)
    at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.findPluginForPrefix(MojoDescriptorCreator.java:265)
    at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.getMojoDescriptor(MojoDescriptorCreator.java:219)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments(DefaultLifecycleTaskSegmentCalculator.java:103)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments(DefaultLifecycleTaskSegmentCalculator.java:83)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:89)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException

Process finished with exit code 1
Error Info

6. 點擊Maven Projects->Plugins,發現已經存在mybatis-generator插件,右鍵點擊該插件,選擇運行Run Maven Build,執行成功,代碼自動生成:

 
 1 C:\java\jdk1.8.0_112\bin\java -Dmaven.multiModuleProjectDirectory=E:\Projects\war-fx-mock\mock-service "-Dmaven.home=D:\Program Files\apache-maven-3.3.9" "-Dclassworlds.conf=D:\Program Files\apache-maven-3.3.9\bin\m2.conf" -Didea.launcher.port=7537 "-Didea.launcher.bin.path=D:\Program Files\IntelliJIDEA\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\apache-maven-3.3.9\boot\plexus-classworlds-2.5.2.jar;D:\Program Files\IntelliJIDEA\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=2016.2.5 -s "D:\Program Files\apache-maven-3.3.9\conf\settings-nexus3_5_2.xml" -Dmaven.repo.local=E:\mvn\repository org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate
 2 [WARNING] 
 3 [WARNING] Some problems were encountered while building the effective settings
 4 [WARNING] Unrecognised tag: 'pluginRepositories' (position: START_TAG seen ...</servers>  \n\n     <pluginRepositories>... @33:26)  @ D:\Program Files\apache-maven-3.3.9\conf\settings-nexus3_5_2.xml, line 33, column 26
 5 [WARNING] 
 6 [INFO] Scanning for projects...
 7 [WARNING] 
 8 [WARNING] Some problems were encountered while building the effective model for com.mobanker.mock:mock-service:war:1.0.0-SNAPSHOT
 9 [WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework.boot:spring-boot-starter-web:jar -> duplicate declaration of version (?) @ com.mobanker.mock:mock-service:[unknown-version], E:\Projects\war-fx-mock\mock-service\pom.xml, line 81, column 21
10 [WARNING] 
11 [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
12 [WARNING] 
13 [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
14 [WARNING] 
15 [INFO]                                                                         
16 [INFO] ------------------------------------------------------------------------
17 [INFO] Building mock-service 1.0.0-SNAPSHOT
18 [INFO] ------------------------------------------------------------------------
19 [INFO] 
20 [INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ mock-service ---
21 [INFO] Connecting to the Database
22 Thu Feb 08 16:21:50 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
23 log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector).
24 log4j:WARN Please initialize the log4j system properly.
25 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
26 [INFO] Introspecting table person
27 [INFO] Generating Record class for table person
28 [INFO] Generating Mapper Interface for table person
29 [INFO] Generating SQL Map for table person
30 [INFO] Saving file PersonMapper.xml
31 [INFO] Saving file Person.java
32 [INFO] Saving file PersonMapper.java
33 [INFO] ------------------------------------------------------------------------
34 [INFO] BUILD SUCCESS
35 [INFO] ------------------------------------------------------------------------
36 [INFO] Total time: 2.973 s
37 [INFO] Finished at: 2018-02-08T16:21:51+08:00
38 [INFO] Final Memory: 17M/171M
39 [INFO] ------------------------------------------------------------------------
40 
41 Process finished with exit code 0

 

 

 
 


免責聲明!

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



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