SpringBoot整合MyBatis,连接MySQL8.0遇坑经验

MyBatis逆向工程以后,启动tomcat直接报错,但是MySQL5.7是没有任何问题的

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with

Application启动类也加了对应扫描包的注解,applicaiton.yml的mapper-locations属性也没错。网上查了很多原因,都没有解决。

问题在于

GeneratorMapper文件的jdbcConnection标签的connectionURL属性要修改,这个和5.7版是不同的

其中<classPathEntry location="/Users/driver/mysql-connector-java-8.0.20.jar"> 表示指定本地的驱动jar包的位置

最重点是

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mall?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=GMT&amp;nullCatalogMeansCurrent=true"
                        userId="root"
                        password="123456">

在MySQL8.0中jdbc的url必须指明这些参数,否则一旦启动tomcat直接报错。另外在xml文件中不允许出现&,所以用&amp;来代替

<?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>
    <classPathEntry location="/Users/driver/mysql-connector-java-8.0.20.jar"></classPathEntry>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mall?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=GMT&amp;nullCatalogMeansCurrent=true"
                        userId="root"
                        password="123456">
        </jdbcConnection>

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

        <!-- 指定javabean生成的位置 -->
        <javaModelGenerator targetPackage="com.example.model" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 指定sql映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.example.mapper"  targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 指定dao接口生成的位置,mapper接口 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 指定每个表的生成策略 -->
        <table tableName="good" domainObjectName="Good"></table>
        <table tableName="user" domainObjectName="User"></table>
        <table tableName="order" domainObjectName="Order"></table>

    </context>
</generatorConfiguration>

因此application.yml也要跟着改了,着重看数据源的url属性,填好这些必要参数

server:
  port: 3001
spring:
  http:
    encoding:
      charset: UTF-8
      force: true
      enabled: true
  datasource:
    username: root
    password: Server1067010240
    url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT&nullCatalogMeansCurrent=true
mybatis:
  mapper-locations: classpath:com/example/mapper/*.xml

猜你喜欢

转载自blog.csdn.net/Xeon_CC/article/details/108873028