最全面的SpringBoot教程(五)——整合框架
前言
本文为 最全面的SpringBoot教程(五)——整合框架 相关知识,下边将对SpringBoot整合Junit
,SpringBoot整合Mybatis
,SpringBoot整合Redis
等进行详尽介绍~
📌博主主页:小新要变强 的主页
👉Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~
👉算法刷题路线可参考:算法刷题路线总结与相关资料分享,内含最详尽的算法刷题路线指南及相关资料分享~
👉Java微服务开源项目可参考:企业级Java微服务开源项目(开源框架,用于学习、毕设、公司项目、私活等,减少开发工作,让您只关注业务!)
目录
最全面的SpringBoot教程(五)——整合框架
一、SpringBoot整合Junit
🍀(1)搭建SpringBoot工程 springboot-test,不用任何起步依赖
🍀(2)引入starter-test起步依赖
pom.xml:
<dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies>
🍀(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 { @Autowired UserService userService; @Test public void testAdd() { userService.add(); }}
🍀(5)测试
二、SpringBoot整合Mybatis
🍀(1)搭建SpringBoot工程 springboot-mybatis
🍀(2)引入mybatis起步依赖,添加mysql驱动
pom.xml:
<dependencies> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>2.1.0version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies>
🍀(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; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; }}
🍀(4)编写DataSource和MyBatis相关配置
application.yml:
# datasourcespring: datasource: url: jdbc:mysql://127.0.0.1:3306/springboot?serverTimezone=UTC username: root password: root driver-class-name: com.mysql.jdbc.Driver
🍀(5)纯注解开发 新建接口com.wang.springbootmybatis.mapper
@Mapperpublic interface UserMapper { @Select("select * from t_user") public List<User> findAll();}
🍀(6)测试
@SpringBootTestclass SpringbootMybatisApplicationTests { @Autowired private UserMapper userMapper; @Test void testFindAll() { List<User> all = userMapper.findAll(); System.out.println(all); }}
🍀(7)xml开发:新建接口 com.wang.springbootmybatis.mapper
@Mapperpublic interface UserXmlMapper { public List<User> findAll();}
resources下建立xml文件: UserMapper.xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.wang.mapper.UserXmlMapper"> <select id="findAll" resultType="com.wang.domain.User"> select * from t_user select>mapper>
修改application.yml 新增如下配置:
mybatis: mapper-locations: classpath:mapper/* type-aliases-package: com.wang.springbootmybatis.domain
🍀(8)测试
@Autowiredprivate UserXmlMapper userXmlMapper; @Test void testFindAllByXml() { List<User> all = userXmlMapper.findAll(); System.out.println(all); }
三、SpringBoot整合Redis
🍀(1)搭建SpringBoot工程: springboot-redis
🍀(2)引入redis起步依赖
pom.xml:
<dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-redisartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies>
🍀(3)编写测试类
@SpringBootTestclass SpringbootRedisApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test void testSet() { redisTemplate.boundValueOps("name").set("zhangsan"); } @Test void 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的主机ip port: 6379
后记
👉Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~
👉算法刷题路线可参考:算法刷题路线总结与相关资料分享,内含最详尽的算法刷题路线指南及相关资料分享~
来源地址:https://blog.csdn.net/qq_42146402/article/details/129018860
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341