SpringBoot整合Mybatis與druid實現(xiàn)流程詳解_第1頁
SpringBoot整合Mybatis與druid實現(xiàn)流程詳解_第2頁
SpringBoot整合Mybatis與druid實現(xiàn)流程詳解_第3頁
SpringBoot整合Mybatis與druid實現(xiàn)流程詳解_第4頁
SpringBoot整合Mybatis與druid實現(xiàn)流程詳解_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

第SpringBoot整合Mybatis與druid實現(xiàn)流程詳解目錄SpringBoot整合junitSpringBoot整合junitSpringBoot整合junit的classesSpringBoot整合Mybatis整合前的準(zhǔn)備整合MybatisSpringBoot整合druid配置前置知識小點整合druid

SpringBoot整合junit

SpringBoot整合junit

①還是一樣,我們首先創(chuàng)建一個SpringBoot模塊。

由于我們并不測試前端,而只是整合junit,所以不用選擇模板,選擇其中的web即可。

完成以后我們打開Pom.xml,會發(fā)現(xiàn)報錯,這里我的版本不能到2.7.5,降版本。

②導(dǎo)入對應(yīng)的starter

查看Pom.xml文件:我們發(fā)現(xiàn)已經(jīng)有了一個Spring-Boot-stater-test,這其實就是SpringBoot已經(jīng)自己整合了junit

xmlversion="1.0"encoding="UTF-8"

projectxmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"

modelVersion4.0.0/modelVersion

parent

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-parent/artifactId

version2.7.4/version

relativePath/!--lookupparentfromrepository--

/parent

groupIdcom.example/groupId

artifactIdSpringBoot-juint/artifactId

version0.0.1-SNAPSHOT/version

nameSpringBoot-juint/name

descriptionSpringBoot-juint/description

properties

java.version1.8/java.version

/properties

dependencies

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-test/artifactId

scopetest/scope

/dependency

/dependencies

build

plugins

plugin

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-maven-plugin/artifactId

/plugin

/plugins

/build

/project

問題隨之而來,既然SpringBoot已經(jīng)整合了junit,那我們還整合啥?答案是不用整合!

因為SpringBoot項目在創(chuàng)建的時候已經(jīng)默認(rèn)整合了junit,至于為什么是這樣,是因為SpringBoot是一個maven項目,而maven在執(zhí)行它的生命周期的時候測試是跳不過去的,它必須執(zhí)行測試。

③測試類添加@SpringBootTest注解修飾

packagecom.example;

importorg.junit.jupiter.api.Test;

importorg.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

classSpringBootJuintApplicationTests{

@Test

voidcontextLoads(){

}

④使用自動裝配添加要測試的對象

這里測試啥呢?測試一下dao層

一、編寫Dao層接口:

packagecom.example.Dao;

publicinterfaceUserDao{

publicvoidselectAll();

}

二、編寫Dao層接口的實現(xiàn)類:值得說明的是,一般情況下,因為Dao層的mapper需要用到反射,一般是沒有實現(xiàn)類的,這里只是為了測試方便?。?!

packagecom.example.Dao.impl;

importcom.example.Dao.UserDao;

importorg.springframework.stereotype.Repository;

@Repository//把這個類交給Spring容器管理

publicclassImplUserDaoimplementsUserDao{

@Override

publicvoidselectAll(){

System.out.println("selectAll.......");

}

三、在測試類中使用@Autowired進行注入

packagecom.example;

importcom.example.Dao.UserDao;

importorg.junit.jupiter.api.Test;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

classSpringBootJuintApplicationTests{

@Autowired

UserDaouserDao;

@Test

voidcontextLoads(){

userDao.selectAll();

}

四、執(zhí)行測試

SpringBoot整合junit的classes

在上面整合的junit并不完整,為什么這樣說,請看:

現(xiàn)在SpringBoot的引導(dǎo)類和測試類的引導(dǎo)類都在同級目錄下,現(xiàn)在我要把測試類的引導(dǎo)類移動到com包下

執(zhí)行測試,輸出:Unabletofinda@SpringBootConfiguration,youneedtouse@ContextConfigurationor@SpringBootTest(classes=...)withyourtest

:說的是它找不到SpringBoot的配置類,需要你使用@ContextConfiguration或者@SpringBootTest為你的測試類指定SpringBoot的配置類

為什么出現(xiàn)以下情況,原來是@SpringBootTest會默認(rèn)從當(dāng)前包及其子包下尋找SpringBoot的配置類,當(dāng)我們把測試類中的SpringBootJuintApplicationTests移動到com包下時,它就找不到對應(yīng)的SpringBoot的配置類,因為它這時不在引導(dǎo)類包及其子包下,也就無法從spring容器中獲取對應(yīng)bean,則無法進行注入。

解決方法:在@SpringBootTest注解中指定引導(dǎo)類或者使用@ContextConfiguration

@SpringBootTest(classes=SpringBootJuintApplication.class)

@ContextConfiguration(classes=SpringBootJuintApplication.class)

SpringBoot整合Mybatis

整合前的準(zhǔn)備

值得說明的是,這里整合Mybatis用的是注解的方式

一、創(chuàng)建數(shù)據(jù)庫數(shù)據(jù)

DROPTABLEIFEXISTS`user`;

CREATETABLE`user`(

`username`varchar(20)CHARACTERSETutf8COLLATEutf8_esperanto_ciNULLDEFAULTNULL,

`password`varchar(20)CHARACTERSETutf8COLLATEutf8_esperanto_ciNULLDEFAULTNULL

)ENGINE=InnoDBCHARACTERSET=utf8COLLATE=utf8_esperanto_ciROW_FORMAT=Dynamic;

INSERTINTO`user`VALUES('zhangsan','775033');

INSERTINTO`user`VALUES('lisi','330678');

SETFOREIGN_KEY_CHECKS=1;

二、創(chuàng)建工程、新建對應(yīng)實體類

SpringBoot方便的一部分原因就是,用什么導(dǎo)入就勾選什么技術(shù)。

或者手動加入:

xmlversion="1.0"encoding="UTF-8"

projectxmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"

modelVersion4.0.0/modelVersion

parent

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-parent/artifactId

version2.7.4/version

relativePath/!--lookupparentfromrepository--

/parent

groupIdcom.example/groupId

artifactIdSpringBoot-Mybatis/artifactId

version0.0.1-SNAPSHOT/version

nameSpringBoot-Mybatis/name

descriptionSpringBoot-Mybatis/description

properties

java.version1.8/java.version

/properties

dependencies

dependency

groupIdorg.mybatis.spring.boot/groupId

artifactIdmybatis-spring-boot-starter/artifactId

version2.2.2/version

/dependency

!--/artifact/mysql/mysql-connector-java--

dependency

groupIdmysql/groupId

artifactIdmysql-connector-java/artifactId

version8.0.21/version

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-test/artifactId

scopetest/scope

/dependency

/dependencies

build

plugins

plugin

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-maven-plugin/artifactId

/plugin

/plugins

/build

/project

創(chuàng)建實體類對象:

packagecom.example.entity;

publicclassUser{

privateStringusername;

privateStringpassword;

@Override

publicStringtoString(){

return"User{"+

"username='"+username+'\''+

",password='"+password+'''+

'}';

publicStringgetUsername(){

returnusername;

publicvoidsetUsername(Stringusername){

this.username=username;

publicStringgetPassword(){

returnpassword;

publicvoidsetPassword(Stringpassword){

this.password=password;

publicUser(){

publicUser(Stringusername,Stringpassword){

this.username=username;

this.password=password;

}

整合Mybatis

一、上面已經(jīng)導(dǎo)入了對應(yīng)技術(shù)用到的坐標(biāo),現(xiàn)在要配置數(shù)據(jù)源,在哪配置呢,在SpringBoot的配置文件:

spring:

datasource:

driver-class-name:com.mysql.jdbc.Driver

url:jdbc:mysql://localhost:3306/springboot

username:root

password:******

二、編寫dao層接口()

packagecom.example.dao;

importcom.example.entity.User;

importorg.apache.ibatis.annotations.Mapper;

importorg.apache.ibatis.annotations.Select;

importjava.util.List;

@Mapper

publicinterfaceUserDao{

@Select("selectusername,passwordfromuser")

publicListUserselectAll();

}

三、編寫測試

packagecom.example.springbootmybatis;

importcom.example.dao.UserDao;

importorg.junit.jupiter.api.Test;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

classSpringBootMybatisApplicationTests{

@Autowired

UserDaouserDao;

@Test

voidcontextLoads(){

System.out.println(userDao.selectAll());

}

測試:

報了以上兩個錯誤,這是我們希望看到的,為什么?

原因:在上面的驅(qū)動中我們使用的是MySQL8.X版本的,在8及以上的MySQL驅(qū)動中,SpringBoot強制我們進行時區(qū)設(shè)置,并且要用:com.mysql.cj.jdbc.Driver

解決:這里我們只需要在配置文件中添加失去設(shè)置和更改Driver即可

spring:

datasource:

driver-class-name:com.mysql.cj.jdbc.Driver

url:jdb

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論