idea使用mybatis-generator自动生成代码

一、pom.xml

    org.mybatis.generator
    mybatis-generator-maven-plugin
    1.3.2
    
        
            mysql
            mysql-connector-java
            5.1.46
        

    

    
        
        src/main/resources/generatorConfig.xml
        true
        true
    

二、generatorConfig.xml配置文件


        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

   
        
        
                                connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useUnicode=true&autoReconnect=true&failOverReadOnly=false"
                        userId="root"
                        password="root" />

        
       

        
       

        
       

        
       


   


三、生成代码
使用maven插件快速生成,点击右边栏的 Maven Projects --> mybatis-generator:generate 即可生成。


备注:
过程中可能存在的问题如下。
错误1:工程引入junit的版本版本与mybatis-generator依赖的版本不一致:
D:\java\jdk8\jdk1.8.0_101\bin\java -Dmaven.home=D:\maven\apache-maven-3.0.5 -Dclassworlds.conf=D:\maven\apache-maven-3.0.5\bin\m2.conf "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.2\lib\idea_rt.jar=57646:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.2\bin" -Dfile.encoding=UTF-8 -classpath D:\maven\apache-maven-3.0.5\boot\plexus-classworlds-2.4.jar org.codehaus.classworlds.Launcher -Didea.version=2017.2.2 -s D:\maven\settings3.xml org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building store_partner_server 1.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ store_partner_server ---
Downloading: http://maven.aliyun.com/nexus/content/groups/public/junit/junit/4.10/junit-4.10.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.987s
[INFO] Finished at: Tue Sep 04 18:53:23 CST 2018
[INFO] Final Memory: 9M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project store_partner_server: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Plugin org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2 or one of its dependencies could not be resolved: Could not transfer artifact junit:junit:jar:4.10 from/to alimaven (http://maven.aliyun.com/nexus/content/groups/public/): archiva-maven-storage-prod.oss-cn-beijing.aliyuncs.com: Unknown host archiva-maven-storage-prod.oss-cn-beijing.aliyuncs.com -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[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/PluginResolutionException

Process finished with exit code 1

解决方法:

    junit
    junit
    4.10
    test

错误2:generatorConfig.xml配置文件中的数据库配置的&符号引起

D:\java\jdk8\jdk1.8.0_101\bin\java -Dmaven.home=D:\maven\apache-maven-3.0.5 -Dclassworlds.conf=D:\maven\apache-maven-3.0.5\bin\m2.conf "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.2\lib\idea_rt.jar=59275:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.2\bin" -Dfile.encoding=UTF-8 -classpath D:\maven\apache-maven-3.0.5\boot\plexus-classworlds-2.4.jar org.codehaus.classworlds.Launcher -Didea.version=2017.2.2 -s D:\maven\settings3.xml org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building store_partner_server 1.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ store_partner_server ---
[ERROR] XML Parser Error on line 10: 对实体 "useUnicode" 的引用必须以 ';' 分隔符结尾。
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.474s
[INFO] Finished at: Tue Sep 04 19:05:57 CST 2018
[INFO] Final Memory: 8M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project store_partner_server: XML Parser Error on line 10: 对实体 "useUnicode" 的引用必须以 ';' 分隔符结尾。 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[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/MojoExecutionException

Process finished with exit code 1

解决方法:
把数据库配置中的&修改为&

错误3:generatorConfig.xml文件提示 URI is not registered 错误

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" 文件中这段为红色,鼠标放上去可看到 URI is not registered 错误提示。

解决办法:鼠标点击该段,左边会出现小红灯,点击向下箭头,选择 Fetch external resource 即可解决。
 

你可能感兴趣的:(mybatis)