欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Spring框架家族】SpringBoot整合第三方

程序员文章站 2024-01-28 12:46:52
...

SpringBoot整合Junit

  • 搭建SpringBoot工程
  • 引入starter-test起步依赖
     	<dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
  • 编写测试类
    /**
     * 测试类
     */
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = SpringbootJunitApplication.class )
    public class UserServiceTest {
    
        @Test
        public void test(){
            System.out.println(111);
        }
    }

SpringBoot整合mybatis

  • 搭建SpringBoot工程

  • 引入mybatis起步依赖,添加mysql驱动

    	<dependencies>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <!--<scope>runtime</scope>-->
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
  • 编写DataSource和MyBatis相关配置

application.yml

    # datasource
    spring:
      datasource:
      	# serverTimezone=UTC 指定标准时区
        url: jdbc:mysql:///springboot?serverTimezone=UTC
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    
    # mybatis
    mybatis:
      mapper-locations: classpath:mapper/*Mapper.xml # mapper映射文件路径
      type-aliases-package: cs.wy.springbootmybatis.domain
    
      # config-location:  # 指定mybatis的核心配置文件
  • 定义表和实体类
    public class User {
        private int id;
        private String username;
        private String password;
    
    	get...set...
  • 编写dao和mapper文件/纯注解开发

  • 编写dao

    @Mapper
    @Repository
    public interface UserXmlMapper {
    
        public List<User> findAll();
    }

mapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="cs.wy.springbootmybatis.mapper.UserXmlMapper">
        <select id="findAll" resultType="user">
            select * from t_user
        </select>
    </mapper>

纯注解开发

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

SpringBoot整合redis

  • 搭建SpringBoot工程

  • 引入redis起步依赖

      	<dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
  • 配置redis相关属性
    spring:
      redis:
        host: 127.0.0.1 # redis的主机ip
        port: 6379
  • 注入RedisTemplate模板,编写测试方法
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringbootRedisApplicationTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void testSet() {
            //存入数据
            redisTemplate.boundValueOps("name").set("zhangsan");
        }
    
        @Test
        public void testGet() {
            //获取数据
            Object name = redisTemplate.boundValueOps("name").get();
            System.out.println(name);
        }
    
    }