SpringBoot整合Mybatis与druid实现流程详解_第1页
SpringBoot整合Mybatis与druid实现流程详解_第2页
SpringBoot整合Mybatis与druid实现流程详解_第3页
SpringBoot整合Mybatis与druid实现流程详解_第4页
SpringBoot整合Mybatis与druid实现流程详解_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第SpringBoot整合Mybatis与druid实现流程详解目录SpringBoot整合junitSpringBoot整合junitSpringBoot整合junit的classesSpringBoot整合Mybatis整合前的准备整合MybatisSpringBoot整合druid配置前置知识小点整合druid

SpringBoot整合junit

SpringBoot整合junit

①还是一样,我们首先创建一个SpringBoot模块。

由于我们并不测试前端,而只是整合junit,所以不用选择模板,选择其中的web即可。

完成以后我们打开Pom.xml,会发现报错,这里我的版本不能到2.7.5,降版本。

②导入对应的starter

查看Pom.xml文件:我们发现已经有了一个Spring-Boot-stater-test,这其实就是SpringBoot已经自己整合了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已经整合了junit,那我们还整合啥?答案是不用整合!

因为SpringBoot项目在创建的时候已经默认整合了junit,至于为什么是这样,是因为SpringBoot是一个maven项目,而maven在执行它的生命周期的时候测试是跳不过去的,它必须执行测试。

③测试类添加@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层接口的实现类:值得说明的是,一般情况下,因为Dao层的mapper需要用到反射,一般是没有实现类的,这里只是为了测试方便!!!

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();

}

四、执行测试

SpringBoot整合junit的classes

在上面整合的junit并不完整,为什么这样说,请看:

现在SpringBoot的引导类和测试类的引导类都在同级目录下,现在我要把测试类的引导类移动到com包下

执行测试,输出:Unabletofinda@SpringBootConfiguration,youneedtouse@ContextConfigurationor@SpringBootTest(classes=...)withyourtest

:说的是它找不到SpringBoot的配置类,需要你使用@ContextConfiguration或者@SpringBootTest为你的测试类指定SpringBoot的配置类

为什么出现以下情况,原来是@SpringBootTest会默认从当前包及其子包下寻找SpringBoot的配置类,当我们把测试类中的SpringBootJuintApplicationTests移动到com包下时,它就找不到对应的SpringBoot的配置类,因为它这时不在引导类包及其子包下,也就无法从spring容器中获取对应bean,则无法进行注入。

解决方法:在@SpringBootTest注解中指定引导类或者使用@ContextConfiguration

@SpringBootTest(classes=SpringBootJuintApplication.class)

@ContextConfiguration(classes=SpringBootJuintApplication.class)

SpringBoot整合Mybatis

整合前的准备

值得说明的是,这里整合Mybatis用的是注解的方式

一、创建数据库数据

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;

二、创建工程、新建对应实体类

SpringBoot方便的一部分原因就是,用什么导入就勾选什么技术。

或者手动加入:

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

创建实体类对象:

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

一、上面已经导入了对应技术用到的坐标,现在要配置数据源,在哪配置呢,在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());

}

测试:

报了以上两个错误,这是我们希望看到的,为什么?

原因:在上面的驱动中我们使用的是MySQL8.X版本的,在8及以上的MySQL驱动中,SpringBoot强制我们进行时区设置,并且要用:com.mysql.cj.jdbc.Driver

解决:这里我们只需要在配置文件中添加失去设置和更改Driver即可

spring:

datasource:

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

url:jdb

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论