最全面的SpringBoot教程(五)——整合框架
创始人
2024-05-25 21:08:36

前言

在这里插入图片描述

本文为 最全面的SpringBoot教程(五)——整合框架 相关知识,下边将对SpringBoot整合JunitSpringBoot整合MybatisSpringBoot整合Redis等进行详尽介绍~

📌博主主页:小新要变强 的主页
👉Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~
👉算法刷题路线可参考:算法刷题路线总结与相关资料分享,内含最详尽的算法刷题路线指南及相关资料分享~
👉Java微服务开源项目可参考:企业级Java微服务开源项目(开源框架,用于学习、毕设、公司项目、私活等,减少开发工作,让您只关注业务!)


目录

最全面的SpringBoot教程(五)——整合框架

  • 前言
  • 目录
  • 一、SpringBoot整合Junit
  • 二、SpringBoot整合Mybatis
  • 三、SpringBoot整合Redis
  • 后记

在这里插入图片描述

一、SpringBoot整合Junit

🍀(1)搭建SpringBoot工程 springboot-test,不用任何起步依赖

🍀(2)引入starter-test起步依赖

pom.xml:

org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtest

🍀(3)编写service:com.wang.springboottest

@Service 
public class UserService { public void add() { System.out.println("add..........."); } 
}

🍀(4)编写测试类: com.wang.springboottest

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserServiceTest {@AutowiredUserService userService;@Testpublic void testAdd() {userService.add();}
}

🍀(5)测试

二、SpringBoot整合Mybatis

🍀(1)搭建SpringBoot工程 springboot-mybatis

🍀(2)引入mybatis起步依赖,添加mysql驱动

pom.xml:

org.mybatis.spring.bootmybatis-spring-boot-starter2.1.0mysqlmysql-connector-javaorg.springframework.bootspring-boot-starter-testtest

🍀(3)定义表和实体类: com.wang.springbootmybatis.domain

User.java:

public class User {private int id;private String username;private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +'}';}
}

🍀(4)编写DataSource和MyBatis相关配置

application.yml:

# datasource
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/springboot?serverTimezone=UTCusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver

🍀(5)纯注解开发 新建接口com.wang.springbootmybatis.mapper

@Mapper
public interface UserMapper {@Select("select * from t_user")public List findAll();
}

🍀(6)测试

@SpringBootTest
class SpringbootMybatisApplicationTests {@Autowiredprivate UserMapper userMapper;@Testvoid testFindAll() {List all = userMapper.findAll();System.out.println(all);}
}

🍀(7)xml开发:新建接口 com.wang.springbootmybatis.mapper

@Mapper
public interface UserXmlMapper {public List findAll();
}

resources下建立xml文件: UserMapper.xml





修改application.yml 新增如下配置:

mybatis:mapper-locations: classpath:mapper/*type-aliases-package: com.wang.springbootmybatis.domain

🍀(8)测试

@Autowired
private UserXmlMapper userXmlMapper;@Testvoid testFindAllByXml() {List all = userXmlMapper.findAll();System.out.println(all);}

三、SpringBoot整合Redis

🍀(1)搭建SpringBoot工程: springboot-redis

🍀(2)引入redis起步依赖

pom.xml:

 org.springframework.bootspring-boot-starter-data-redisorg.springframework.bootspring-boot-starter-testtest

🍀(3)编写测试类

@SpringBootTest
class SpringbootRedisApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Testvoid testSet() {redisTemplate.boundValueOps("name").set("zhangsan");}@Testvoid testGet() {Object name = redisTemplate.boundValueOps("name").get();System.out.println(name);}
}

🍀(4)启动redis

🍀(5)测试

🍀(6)application.yml 配置redis相关属性

spring:redis:host: 127.0.0.1 # redis的主机ipport: 6379

后记

在这里插入图片描述

👉Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~
👉算法刷题路线可参考:算法刷题路线总结与相关资料分享,内含最详尽的算法刷题路线指南及相关资料分享~

相关内容

热门资讯

阿西吧是什么意思 阿西吧相当于... 即使你没有受到过任何外语培训,你也懂四国语言。汉语:你好英语:Shit韩语:阿西吧(아,씨발! )日...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...