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

前言

在这里插入图片描述

本文为 最全面的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:

<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:

# datasource
spring:
  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

@Mapper
public interface UserMapper {

    @Select("select * from t_user")
    public List<User> findAll();
}

(6)测试

@SpringBootTest
class SpringbootMybatisApplicationTests {
    @Autowired
    private UserMapper userMapper;
    
    @Test
    void testFindAll() {
        List<User> all = userMapper.findAll();
        System.out.println(all);
    }
}

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

@Mapper
public 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)测试

@Autowired
private 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)编写测试类

@SpringBootTest
class 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全栈学习技术清单~
算法刷题路线可参考:算法刷题路线总结与相关资料分享,内含最详尽的算法刷题路线指南及相关资料分享~

你可能感兴趣的:(分布式与微服务,spring,boot,java,mybatis)