1. 程式人生 > 實用技巧 >SpringBoot - 2 整合框架

SpringBoot - 2 整合框架

SpringBoot - 2 整合框架

14-SpringBoot整合Junit

  1. 搭建SpringBoot工程

  2. 引入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>
  1. 編寫測試類
/**
 * 測試類
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJunitApplication.class )
public class UserServiceTest {

    @Test
    public void test(){
        System.out.println(111);
    }
}

4.測試

15-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:
    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: com.itheima.springbootmybatis.domain

  # config-location:  # 指定mybatis的核心配置檔案

④定義表和實體類

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 + '\'' +
                '}';
    }
}

⑤編寫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="com.itheima.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();
}

⑥測試

16-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);
    }

}

SpringBoot整合定時任務

5.3.1) 啟用定時任務

@SpringBootApplication
@EnableScheduling
public class SpringbootConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootConfigApplication.class, args);
    }

}

5.3.2) 編寫定時任務類

package com.itheima.springbootconfig;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author liqp
 * @version 1.0
 * @date 2020/8/16
 */
@Component
public class TestScheduling {

    @Scheduled(cron = "* * * * * *") //每秒執行一次
    public void run() {
        System.out.println("itheima");
    }
}

5.3.3)corn表示式詳解

  • 簡述
    Cron表示式是一個字串,字串以5或6個空格隔開,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:
    (1) Seconds Minutes Hours DayofMonth Month DayofWeek Year
    (2)Seconds Minutes Hours DayofMonth Month DayofWeek
  • 結構
    corn從左到右(用空格隔開):秒 分 小時 月份中的日期 月份 星期中的日期 年份
  • 各欄位的含義
欄位 允許值 允許的特殊字元
秒(Seconds) 0~59的整數 , - * / 四個字元
分(Minutes 0~59的整數 , - * / 四個字元
小時(Hours 0~23的整數 , - * / 四個字元
日期(DayofMonth 1~31的整數(但是你需要考慮你月的天數) ,- * ? / L W C 八個字元
月份(Month 1~12的整數或者 JAN-DEC , - * / 四個字元
星期(DayofWeek 1~7的整數或者 SUN-SAT (1=SUN) , - * ? / L C # 八個字元
年(可選,留空)(Year 1970~2099 , - * / 四個字元
  • 字元說明
    每一個域都使用數字,但還可以出現如下特殊字元,它們的含義是:
    (1) :表示匹配該域的任意值。假如在Minutes域使用, 即表示每分鐘都會觸發事件。
    (2)? :只能用在DayofMonth和DayofWeek兩個域。它也匹配域的任意值,但實際不會。因為DayofMonth和DayofWeek會相互影響。例如想在每月的20日觸發排程,不管20日到底是星期幾,則只能使用如下寫法: 13 13 15 20 ?, 其中最後一位只能用?,而不能使用,如果使用*表示不管星期幾都會觸發,實際上並不是這樣。
    (3)-:表示範圍。例如在Minutes域使用5-20,表示從5分到20分鐘每分鐘觸發一次
    (4)/:表示起始時間開始觸發,然後每隔固定時間觸發一次。例如在Minutes域使用5/20,則意味著5分鐘觸發一次,而25,45等分別觸發一次.
    (5),:表示列出列舉值。例如:在Minutes域使用5,20,則意味著在5和20分每分鐘觸發一次。
    (6)L:表示最後,只能出現在DayofWeek和DayofMonth域。如果在DayofWeek域使用5L,意味著在最後的一個星期四觸發。
    (7)W:表示有效工作日(週一到週五),只能出現在DayofMonth域,系統將在離指定日期的最近的有效工作日觸發事件。例如:在 DayofMonth使用5W,如果5日是星期六,則將在最近的工作日:星期五,即4日觸發。如果5日是星期天,則在6日(週一)觸發;如果5日在星期一到星期五中的一天,則就在5日觸發。另外一點,W的最近尋找不會跨過月份 。
    (8)LW:這兩個字元可以連用,表示在某個月最後一個工作日,即最後一個星期五。
    (9)#:用於確定每個月第幾個星期幾,只能出現在DayofMonth域。例如在4#2,表示某月的第二個星期三。
  • 常用表示式例子
    (1)0 0 2 1 * ? * 表示在每月的1日的凌晨2點調整任務
    (2)0 15 10 ? * MON-FRI 表示週一到週五每天上午10:15執行作業
    (3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每個月的最後一個星期五上午10:15執行作
    (4)0 0 10,14,16 * * ? 每天上午10點,下午2點,4點
    (5)0 0/30 9-17 * * ? 朝九晚五工作時間內每半小時
    (6)0 0 12 ? * WED 表示每個星期三中午12點
    (7)0 0 12 * * ? 每天中午12點觸發
    (8)0 15 10 ? * * 每天上午10:15觸發
    (9)0 15 10 * * ? 每天上午10:15觸發
    (10)0 15 10 * * ? * 每天上午10:15觸發
    (11)0 15 10 * * ? 2005 2005年的每天上午10:15觸發
    (12)0 * 14 * * ? 在每天下午2點到下午2:59期間的每1分鐘觸發
    (13)0 0/5 14 * * ? 在每天下午2點到下午2:55期間的每5分鐘觸發
    (14)0 0/5 14,18 * * ? 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發
    (15)0 0-5 14 * * ? 在每天下午2點到下午2:05期間的每1分鐘觸發
    (16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44觸發
    (17)0 15 10 ? * MON-FRI 週一至週五的上午10:15觸發
    (18)0 15 10 15 * ? 每月15日上午10:15觸發
    (19)0 15 10 L * ? 每月最後一日的上午10:15觸發
    (20)0 15 10 ? * 6L 每月的最後一個星期五上午10:15觸發
    (21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最後一個星期五上午10:15觸發
    (22)0 15 10 ? * 6#3 每月的第三個星期五上午10:15觸發