1. 程式人生 > >Spring Boot 整合Mybatis+MySql注意事項

Spring Boot 整合Mybatis+MySql注意事項

資料庫連線池之坑

通常我們使用資料庫時會使用第三方的資料庫連線池,在這裡我使用了

   <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid-version}</version>
   </dependency>

這裡存在版本問題報錯,這裡先貼上完整可用的pom檔案範例

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId
>
com.chineseall</groupId> <artifactId>jump-chain-process</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>jump-chain-process</name> <description>Demo project for Spring Boot</description>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <mybatis-springboot-version>1.3.1</mybatis-springboot-version> <mysql-version>5.1.38</mysql-version> <druid-version>1.1.10</druid-version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--資料庫相關--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-springboot-version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-version}</version><!--$NO-MVN-MAN-VER$--> </dependency> <!-- alibaba的druid資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid-version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

這裡注意阿里連結池的版本是1.1.10是沒有問題的

 <druid-version>1.1.10</druid-version>

在使用1.1.2時報錯

Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/autoconfigure/jdbc/metadata/DataSourcePoolMetadataProvider
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_151]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_151]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_151]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    ... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvider
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_151]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_151]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_151]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_151]
    ... 24 common frames omitted

遇到這類問題果斷換版本吧建議使用1.1.10
如果使用其他資料庫連結池出現類似錯誤建議升級版本

Mybatis對映問題

下滑線對映需要進行配置
下面貼出我使用的application.yml以及mybatis.xml配置
application.yml

server:
    tomcat:
        uri-encoding: UTF-8
        max-threads: 1000
        min-spare-threads: 30
    port: 8888

spring:
    datasource:
        # 使用druid資料來源
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        druid:
            url: jdbc:mysql://yoururl
            username: root
            password: root
            initial-size: 10
            max-active: 100
            min-idle: 10
            max-wait: 60000
            pool-prepared-statements: true
            max-pool-prepared-statement-per-connection-size: 20
            time-between-eviction-runs-millis: 60000
            min-evictable-idle-time-millis: 300000
            validation-query: SELECT 1 FROM DUAL
            test-while-idle: true
            test-on-borrow: false
            test-on-return: false
            stat-view-servlet:
                enabled: true
                url-pattern: /druid/*
                #login-username: admin
                #login-password: admin
            filter:
                stat:
                    log-slow-sql: true
                    slow-sql-millis: 1000
                    merge-sql: true
                wall:
                    config:
                        multi-statement-allow: true
mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要對應mapper對映xml檔案的所在路徑
  type-aliases-package: com.xxx.xxx.bean  # 注意:對應實體類的路徑
  configLocation: classpath:mybatis.xml #mybatis 配置檔案路徑

mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>



    <!-- 配置mybatis的快取,延遲載入等等一系列屬性 -->
    <settings>

        <!-- 全域性對映器啟用快取 -->
        <setting name="cacheEnabled" value="true"/>

        <!-- 查詢時,關閉關聯物件即時載入以提高效能 -->
        <setting name="lazyLoadingEnabled" value="true"/>

        <!-- 對於未知的SQL查詢,允許返回不同的結果集以達到通用的效果 -->
        <setting name="multipleResultSetsEnabled" value="true"/>

        <!-- 允許使用列標籤代替列名 -->
        <setting name="useColumnLabel" value="true"/>

        <!-- 不允許使用自定義的主鍵值(比如由程式生成的UUID 32位編碼作為鍵值),資料表的PK生成策略將被覆蓋 -->
        <setting name="useGeneratedKeys" value="false"/>

        <!-- 給予被巢狀的resultMap以欄位-屬性的對映支援 FULL,PARTIAL -->
        <setting name="autoMappingBehavior" value="PARTIAL"/>

        <!-- 對於批量更新操作快取SQL以提高效能 BATCH,SIMPLE -->
        <!-- <setting name="defaultExecutorType" value="BATCH" /> -->

        <!-- 資料庫超過25000秒仍未響應則超時 -->
        <!-- <setting name="defaultStatementTimeout" value="25000" /> -->

        <!-- Allows using RowBounds on nested statements -->
        <setting name="safeRowBoundsEnabled" value="false"/>

        <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT 
            local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->
        <setting name="localCacheScope" value="SESSION"/>

        <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values 
            like NULL, VARCHAR or OTHER. -->
        <setting name="jdbcTypeForNull" value="OTHER"/>

        <!-- Specifies which Object's methods trigger a lazy load -->
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>

        <!-- 設定關聯物件載入的形態,此處為按需載入欄位(載入欄位由SQL指 定),不會載入關聯表的所有欄位,以提高效能 -->
        <setting name="aggressiveLazyLoading" value="false"/>

    </settings>



</configuration>

此條為開啟對映

        <setting name="mapUnderscoreToCamelCase" value="true"/>

主從庫之坑

在配置主從庫時若還以平常方式連結資料庫,在啟動專案時會報以下錯誤

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

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

yml

   masterDataSource:  #主庫資料來源
                url: jdbc:mysql://yoururl
                username: root
                password: root
   slaveDataSource:  #從庫資料來源
                url: yoururl
                username: root
                password: root

多資料來源需要進行特殊配置,配置方法這裡以後會補全,(這裡暫只說明錯誤根源)

相關推薦

Spring Boot 整合Mybatis+MySql注意事項

資料庫連線池之坑 通常我們使用資料庫時會使用第三方的資料庫連線池,在這裡我使用了 <dependency> <groupId>com.alibaba</groupId>

Spring boot 整合mybatis通用mapper配置步驟及注意事項

一、新增依賴 二、繼承通用mapper,可以重寫和選擇需要的mapper方法,可以去掉一些不需要的方法(一般直接繼承即可) Mapper3提供的全部的方法,可以檢視Mapper3通用介面大全 三、application.properties配置 四、設定dao路徑 在

spring boot整合mybatis使用c3p0資料來源連線mysql

剛剛接觸springboot,對很多東西都不熟悉,例如,它的註解方式,他的配置方式等;聽說它很牛逼,所以就嘗試著去學習。在基本熟悉springboot的第一個程式之後。想到當時spring整合mybatis時使用了資料來源連線資料庫,所以自己也想嘗試使用c3p0連線資料庫。所以就有了以下

spring boot整合mybatis深坑之c3p0的詳細配置

text context ati reat source ast type fig oot 項目地址:https://gitee.com/zhangjunqing/spring-boot/tree/master/springboot-mybatis-notice 本人在c3

spring boot 整合mybatis

參考 plugins odin system stack name incr xmlns xsd 參考: http://blog.csdn.net/saytime/article/details/74783296 spring boot可以使用全註解的方式進行開發,極大的提

spring boot整合mybatis+mybatis-plus

可靠 nic false system ttr .post 代碼生成 -i filters Spring boot對於我來說是一個剛接觸的新東西,學習過程中,發現這東西還是很容易上手的,Spring boot沒配置時會默認使用Spring data jpa,這東西可以說一個

企業分布式微服務雲SpringCloud SpringBoot mybatis (十三)Spring Boot整合MyBatis

ech 字段 osc 操作 with public assert 連接 ref Spring中整合MyBatis就不多說了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步驟。搜了一下Spring Boot整合MyBatis的文

Spring Boot整合MyBatis學習總結

Spring Boot MyBatis druid數據源 druid sql監控 公司的很多項目都陸陸續續引入了Spring Boot,通過對Spring Boot的接觸了解發現其真的是大大地簡化了開發、簡化了依賴配置,很多功能註解一下就可以實現,真的是太方便了。下面記錄了一個Sp

spring boot整合mybatis

tis ott 最簡 boot.s driver 大連 ins pla configure spring boot本來可以使用jpa進行數據庫操作,但是考慮到jpa的資料比較少,學習成本比較大,不是所有的人都可以十分了解,因此考慮采用mybatis來進行數據庫操作。 1、新

SpringBoot自學教程 | 第四篇:Spring Boot整合mybatis

整合 com 字段 apach param pack image ice rac   引入依賴   1:在pom文件引入mybatis-spring-boot-starter的依賴: 1 <dependency> 2 <groupId>

spring boot 整合mybatis:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

pri ssp path 內容 方案 sta 問題 xmapp not 最近在學習SpringBoot,遇到些異常情況: 1、異常信息 org.apache.ibatis.binding.BindingException: Invalid bound statement (

spring boot 整合mybatis(好用!!!!)

com true pla 12px 保密 center 性別 request context springboot整合mybatis 1.pom依賴 <!-- 引入freeMarker的依賴包. --> <dependency>

Spring Boot】(23)、Spring Boot整合Mybatis

首先新增mybatis依賴: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</

Spring Boot整合MyBatis實戰

一 新建pom <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g

Spring boot 整合MyBatis(1)

一、Spring boot整合MyBatis Mybatis提供了mybatis-spring-boot-starter 目前 1.3.x 是最新版 https://github.com/mybatis/spring-boot-startergit下載地址

Spring Boot 整合 MyBatis(四)

Spring Boot 整合 MyBatis A、ORM框架是什麼? 物件關係對映(Object Relational Mapping,簡稱 ORM)模式是一種為了解決面向物件與關係資料庫存在的 互不匹配的現象技術。簡單的說,ORM 是通過使用描述物件

Spring-boot整合Mybatis踩坑:不能找到@MapperScan標籤

       開發工具:Ideal        使用場景:Demo 問題描述:        Spring

spring boot整合mybatis基於註解開發以及動態sql的使用

  讓我們回憶一下上篇部落格中mybatis是怎樣發揮它的作用的,主要是三類檔案,第一mapper介面,第二xml檔案,第三全域性配置檔案(application.properties),而今天我們就是來簡化mybatis的工作的——利用註解替代xml配置檔案。   先貼出mapper介面程式碼 @

Spring Boot 整合Mybatis非starter時,mapper一直無法注入解決

本來呢,直接使用mybatis-spring-boot-starter還是挺好的,但是我們系統比較複雜,有多個數據源,其中一個平臺自己的資料來源,另外一些是動態配置出來的,兩者完全沒有關係。所以直接使用mybatis-spring-boot-starter就很麻煩了,會報下列錯誤: Caused by

spring-boot整合mybatis-generator

通用Mapper在1.0.0版本的時候增加了MyBatis Generator(以下簡稱MBG)外掛,使用該外掛可以很方便的生成實體類、Mapper介面以及對應的XML檔案。 下面介紹了mybatis-generator在spring-boot中的使用過程 專案依賴 pom.xml 我使