天天看點

Springboot+Mybatis+SQL Server自動生成實體類

1、Springboot+Mybatis+SQL Server基礎項目搭建:

參考部落格:

Springboot+Mybatis+Mysql:https://blog.csdn.net/guobinhui/article/details/79289189

Springboot+Mybatis+SQL Server:https://blog.csdn.net/wangshuaiwsws95/article/details/85059207

2、配置Mybatis-generator代碼自動生成器:

本項目配置的時候網上找了很多教程,試了各種方法,但是還是Plugins一直沒辦法出現mybatis-generator,也生成不了代碼,直到使用了下面的配置檔案,希望有用。

(1)在pom.xml檔案中添加Maven插件Mybatis-generator:

注意:<dependensies>中mybatis和sqlserver相關的也得添加上。

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--添加mybatis generator maven插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                    <dependency>
                        <groupId>com.microsoft.sqlserver</groupId>
                        <artifactId>sqljdbc4</artifactId>
                        <version>4.0</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>my batis generator</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>                         <!--配置檔案的路徑-->
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                    <configurationFile>${basedir}/src/main/resources/mybatis_generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
           

(2)在resource檔案夾建立mybatis_generator檔案夾,建立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>

    <!--<properties resource="jdbc.properties"></properties>-->
    <!--資料庫驅動包位置-->
    <classPathEntry location="C:\Users\18829\.m2\repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--資料庫URL、使用者名、密碼-->
        <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                        connectionURL="jdbc:sqlserver://localhost:1433;DatabaseName=eggdemo" userId="sa" password="1q2w3e">
        </jdbcConnection>


        <!--生成模型包的位置 -->
        <javaModelGenerator targetPackage="com.egg.eggdemo.Entity"
                            targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成映射檔案的包名和位置-->
        <sqlMapGenerator targetPackage="Mapping"  targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!--生成映射dao(Mapper)的包名和位置-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.egg.eggdemo.Mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--需要生成那些資料庫(更改tableName和domainObjectName)-->
        <table tableName="users" domainObjectName="Users" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false"                enableSelectByExample="false" selectByExampleQueryId="false"  >
        </table>

    </context>
</generatorConfiguration>
           

(3)配置成功的話,點選IntelliJ IDEA右邊菜單欄Maven,在插件Plugins下面會出現mybatis-generator代碼生成器,輕按兩下運作。

Springboot+Mybatis+SQL Server自動生成實體類

(4)檢視項目結構,會發現自動生成的檔案:實體類Users(對應表),映射類UsersMapper(Dao或者接口),以及映射檔案UsersMapping.xml。其中Controller和sevice是自己建立的,用于實作後續的邏輯業務基于控制。

Springboot+Mybatis+SQL Server自動生成實體類

(5)至此配置完畢,開始後續的項目功能實作。

3、項目運作測試遇到的問題:

錯誤資訊(1):

解決辦法:檢查項目代碼,發現target中有對應的Mapping檔案夾,這個和改過代碼之後的沖突了,删除target中Mapping檔案夾

"C:\Program Files (x86)\Java\jdk1.8.0_73\bin\java.exe" -Dmaven.multiModuleProjectDirectory=C:\ccf_document\zhaohang\eggdemo "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=51559:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1.3 org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building eggdemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ eggdemo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.619 s
[INFO] Finished at: 2019-07-08T19:58:00+08:00
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "nexus" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project eggdemo: C:\ccf_document\zhaohang\eggdemo\src\main\resources\mybatis_generator (拒絕通路。) -> [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
           

錯誤資訊(2):

解決辦法:generatorConfig中table配置沒有對應上,改成如下:

<!--需要生成那些資料庫(更改tableName和domainObjectName)-->
<table tableName="users" domainObjectName="Users" enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false"                enableSelectByExample="false" selectByExampleQueryId="false"  >
</table>      

原來是:

<!--需要生成那些資料庫(更改tableName和domainObjectName)-->
<table tableName="user_post" domainObjectName="UsersDO" enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false"                enableSelectByExample="false" selectByExampleQueryId="false"  >
</table>      
"C:\Program Files (x86)\Java\jdk1.8.0_73\bin\java.exe" -Dmaven.multiModuleProjectDirectory=C:\ccf_document\zhaohang\eggdemo "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=51993:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1.3 org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building eggdemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ eggdemo ---
[INFO] Connecting to the Database
[INFO] Introspecting table user_post
[WARNING] Table configuration with catalog null, schema null, and table user_post did not resolve to any tables
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.041 s
[INFO] Finished at: 2019-07-08T20:13:56+08:00
[INFO] Final Memory: 12M/28M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "nexus" could not be activated because it does not exist.

Process finished with exit code 0
           

錯誤資訊(3):

解決辦法:1)直接修改Dao為Mapper,沒有修改對應的其他檔案和XML檔案;

2)在啟動類添加注解@MapperScan("com.egg.eggdemo.Mapper")

"C:\Program Files (x86)\Java\jdk1.8.0_73\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=53209:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_73\jre\lib\rt.jar;C:\ccf_document\zhaohang\eggdemo\target\classes;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.1.6.RELEASE\spring-boot-starter-web-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-starter\2.1.6.RELEASE\spring-boot-starter-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot\2.1.6.RELEASE\spring-boot-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.6.RELEASE\spring-boot-autoconfigure-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.1.6.RELEASE\spring-boot-starter-logging-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\18829\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\18829\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;C:\Users\18829\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;C:\Users\18829\.m2\repository\org\slf4j\jul-to-slf4j\1.7.26\jul-to-slf4j-1.7.26.jar;C:\Users\18829\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\18829\.m2\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.1.6.RELEASE\spring-boot-starter-json-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.9\jackson-databind-2.9.9.jar;C:\Users\18829\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\18829\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;C:\Users\18829\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;C:\Users\18829\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;C:\Users\18829\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.6.RELEASE\spring-boot-starter-tomcat-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.21\tomcat-embed-core-9.0.21.jar;C:\Users\18829\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.21\tomcat-embed-el-9.0.21.jar;C:\Users\18829\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.21\tomcat-embed-websocket-9.0.21.jar;C:\Users\18829\.m2\repository\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;C:\Users\18829\.m2\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\Users\18829\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\18829\.m2\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;C:\Users\18829\.m2\repository\org\springframework\spring-web\5.1.8.RELEASE\spring-web-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-beans\5.1.8.RELEASE\spring-beans-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-webmvc\5.1.8.RELEASE\spring-webmvc-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-aop\5.1.8.RELEASE\spring-aop-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-context\5.1.8.RELEASE\spring-context-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-expression\5.1.8.RELEASE\spring-expression-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar;C:\Users\18829\.m2\repository\net\bytebuddy\byte-buddy\1.9.13\byte-buddy-1.9.13.jar;C:\Users\18829\.m2\repository\org\springframework\spring-core\5.1.8.RELEASE\spring-core-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-jcl\5.1.8.RELEASE\spring-jcl-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-starter-data-jpa\2.1.6.RELEASE\spring-boot-starter-data-jpa-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-starter-aop\2.1.6.RELEASE\spring-boot-starter-aop-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\org\aspectj\aspectjweaver\1.9.4\aspectjweaver-1.9.4.jar;C:\Users\18829\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.1.6.RELEASE\spring-boot-starter-jdbc-2.1.6.RELEASE.jar;C:\Users\18829\.m2\repository\com\zaxxer\HikariCP\3.2.0\HikariCP-3.2.0.jar;C:\Users\18829\.m2\repository\org\springframework\spring-jdbc\5.1.8.RELEASE\spring-jdbc-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\javax\transaction\javax.transaction-api\1.3\javax.transaction-api-1.3.jar;C:\Users\18829\.m2\repository\javax\xml\bind\jaxb-api\2.3.1\jaxb-api-2.3.1.jar;C:\Users\18829\.m2\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;C:\Users\18829\.m2\repository\org\hibernate\hibernate-core\5.3.10.Final\hibernate-core-5.3.10.Final.jar;C:\Users\18829\.m2\repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;C:\Users\18829\.m2\repository\org\javassist\javassist\3.23.2-GA\javassist-3.23.2-GA.jar;C:\Users\18829\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\18829\.m2\repository\org\jboss\jandex\2.0.5.Final\jandex-2.0.5.Final.jar;C:\Users\18829\.m2\repository\org\dom4j\dom4j\2.1.1\dom4j-2.1.1.jar;C:\Users\18829\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.0.4.Final\hibernate-commons-annotations-5.0.4.Final.jar;C:\Users\18829\.m2\repository\org\springframework\data\spring-data-jpa\2.1.9.RELEASE\spring-data-jpa-2.1.9.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\data\spring-data-commons\2.1.9.RELEASE\spring-data-commons-2.1.9.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-orm\5.1.8.RELEASE\spring-orm-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-tx\5.1.8.RELEASE\spring-tx-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\springframework\spring-aspects\5.1.8.RELEASE\spring-aspects-5.1.8.RELEASE.jar;C:\Users\18829\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.3.2\mybatis-spring-boot-starter-1.3.2.jar;C:\Users\18829\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.3.2\mybatis-spring-boot-autoconfigure-1.3.2.jar;C:\Users\18829\.m2\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;C:\Users\18829\.m2\repository\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;C:\Users\18829\.m2\repository\org\mybatis\generator\mybatis-generator-core\1.3.5\mybatis-generator-core-1.3.5.jar;C:\Users\18829\.m2\repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar" com.egg.eggdemo.EggdemoApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-07-08 21:05:24.931  INFO 2656 --- [           main] com.egg.eggdemo.EggdemoApplication       : Starting EggdemoApplication on DESKTOP-SKGKAKO with PID 2656 (C:\ccf_document\zhaohang\eggdemo\target\classes started by 18829 in C:\ccf_document\zhaohang\eggdemo)
2019-07-08 21:05:24.934  INFO 2656 --- [           main] com.egg.eggdemo.EggdemoApplication       : No active profile set, falling back to default profiles: default
2019-07-08 21:05:25.659  WARN 2656 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.egg.eggdemo]' package. Please check your configuration.
2019-07-08 21:05:25.702  INFO 2656 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-07-08 21:05:25.722  INFO 2656 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8ms. Found 0 repository interfaces.
2019-07-08 21:05:26.090  INFO 2656 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$abccdb7d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-07-08 21:05:26.358  INFO 2656 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-07-08 21:05:26.378  INFO 2656 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-07-08 21:05:26.378  INFO 2656 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-08 21:05:26.505  INFO 2656 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-07-08 21:05:26.505  INFO 2656 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1507 ms
2019-07-08 21:05:26.715  INFO 2656 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-07-08 21:05:26.879  INFO 2656 --- [           main] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Driver does not support get/set network timeout for connections. (com.microsoft.sqlserver.jdbc.SQLServerConnection.getNetworkTimeout()I)
2019-07-08 21:05:26.903  INFO 2656 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-07-08 21:05:26.958  INFO 2656 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
	name: default
	...]
2019-07-08 21:05:27.024  INFO 2656 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.10.Final}
2019-07-08 21:05:27.025  INFO 2656 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-07-08 21:05:27.241  INFO 2656 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-07-08 21:05:27.374  INFO 2656 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.SQLServer2012Dialect
2019-07-08 21:05:27.572  INFO 2656 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-07-08 21:05:27.580  WARN 2656 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'usersService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'UsersService': Unsatisfied dependency expressed through field 'usersMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.egg.eggdemo.mapper.UsersMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-07-08 21:05:27.580  INFO 2656 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-07-08 21:05:27.583  INFO 2656 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2019-07-08 21:05:27.585  INFO 2656 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2019-07-08 21:05:27.588  INFO 2656 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-07-08 21:05:27.598  INFO 2656 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-08 21:05:27.724 ERROR 2656 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field usersMapper in com.egg.eggdemo.Service.UsersService required a bean of type 'com.egg.eggdemo.mapper.UsersMapper' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.egg.eggdemo.mapper.UsersMapper' in your configuration.


Process finished with exit code 1
           

錯誤資訊(4):

解決辦法:出現這個問題的原因比較多,我遇到的一般都是和資料庫相關,連接配接資料庫出現問題會顯示500錯誤(内部伺服器錯誤)。本項目中是因為UserMapper.XML中insert和insertSelective的相關配置都出現了兩次(重新運作mybatis-generator會在UserMapper中重新生成一次),删掉就可以了。

Springboot+Mybatis+SQL Server自動生成實體類

備注:問題解決時沒有記錄,之後有些記不太清了,可能有些解決辦法不太準确,望大家批評指正。

繼續閱讀