mybatisplus 配置引起的生產問題 no bean ‘sqlSessionFactory‘

t18092838767發表於2020-11-20

問題描述

在專案中整合mybatis-plus以後,專案在開發工具中可以正常啟動,本地和前端愉快的除錯。但是用docker部署啟動時出現問題。報錯資訊如下,這種情況下,是不是有點懵逼了。都除錯好了,快要上線,結果docker部署不了?那應該是jar出的問題

以下摘錄docker 報錯資訊

[org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.IllegalArgumentException: name
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
        ... 65 common frames omitted
Caused by: java.lang.IllegalArgumentException: name
        at sun.misc.URLClassPath$Loader.findResource(URLClassPath.java:693) ~[na:1.8.0_181]
        at sun.misc.URLClassPath.findResource(URLClassPath.java:215) ~[na:1.8.0_181]
        at java.net.URLClassLoader$2.run(URLClassLoader.java:569) ~[na:1.8.0_181]
        at java.net.URLClassLoader$2.run(URLClassLoader.java:567) ~[na:1.8.0_181]
        at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_181]
        at java.net.URLClassLoader.findResource(URLClassLoader.java:566) ~[na:1.8.0_181]
        at org.springframework.boot.loader.LaunchedURLClassLoader.findResource(LaunchedURLClassLoader.java:78) ~[jcz-zhihui-1.0.0-SNAPSHOT.jar:na]
        at java.lang.ClassLoader.getResource(ClassLoader.java:1096) ~[na:1.8.0_181]
        at org.springframework.core.io.ClassPathResource.resolveURL(ClassPathResource.java:155) ~[spring-core-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
        at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:193) ~[spring-core-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
        at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:498) ~[spring-core-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
        at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:298) ~[spring-core-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
        at com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties.getResources(MybatisPlusProperties.java:203) ~[mybatis-plus-boot-starter-3.1.0.jar!/:3.1.0]
        at com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties.lambda$resolveMapperLocations$0(MybatisPlusProperties.java:197) ~[mybatis-plus-boot-starter-3.1.0.jar!/:3.1.0]
        at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:267) ~[na:1.8.0_181]
        at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_181]
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_181]
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_181]
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:545) ~[na:1.8.0_181]
        at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260) ~[na:1.

問題背景

mybatisplus依賴如下版本

    compile('com.baomidou:mybatis-plus-boot-starter:3.1.0')
    compile('com.baomidou:mybatis-plus-generator:3.1.0')

問題分析

報錯提示是沒有sqlSessionFactory,按照springboot的尿性,在啟動時只要正常配置application.yml,使用推薦的連線池,sqlSessionFactory就自動建立了,mybatis也會自動獲取啊。難道mybatisplus和mybatis有點區別,既然缺少sqlSessionFactory,那就看原始碼唄,看怎麼能把 sqlSessionFactory 搞定。

spring:
  datasource:
    url: jdbc:mysql://0000:00/test?nullNamePatternMatchesAll=true&tinyInt1isBit=false&useSSL=false&nullCatalogMeansCurrent=true
    username: 123
    password: 123
    driver-class-name: com.mysql.cj.jdbc.Driver
    hikari:
      minimum-idle: 5                    # 最小空閒連線數量
      idle-timeout: 180000               # 空閒連線存活最大時間,預設600000(10分鐘)
      maximum-pool-size: 10              # 連線池最大連線數,預設是10
      auto-commit: true                  # 此屬性控制從池返回的連線的預設自動提交行為,預設值:true
      pool-name: authPool                # 連線池名
      max-lifetime: 1800000              # 此屬性控制池中連線的最長生命週期,預設1800000即30分鐘
      connection-timeout: 30000          # 資料庫連線超時時間,預設30秒,即30000
mybatis-plus:
  global-config:
    db-config:
      id-type: auto
      field-strategy: not_empty
      table-underline: true
      db-type: mysql
      logic-delete-value: 1
      logic-not-delete-value: 0
  mapper-locations: classpth:/mapper/**Mapper.xml

原始碼分析

就是這塊的坑,自動配置沒搞定,然後就報錯了

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-0fVJ32yn-1605853770251)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1605843175481.png)]

在這裡插入圖片描述

問題解決

1、自己搞個sqlsessionFactory,這種解決辦法是個掩耳盜鈴的摸魚搞法

2、定位到下面位置時,去看了原始碼。好吧,spring boot中,凡是配置還是複製以前的吧,不然坑是填不完的

mapper-locations路徑錯了

    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties.getResources(MybatisPlusProperties.java:203) ~[mybatis-plus-boot-starter-3.1.0.jar!/:3.1.0]
    at com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties.lambda$resolveMapperLocations$0(MybatisPlusProperties.java:197) ~[mybatis-plus-boot-starter-3.1.0.jar!/:3.1.0]

相關文章