Tích hợp Spring6 với JUnit và MyBatis

Kết hợp Spring6 với JUnit

1. Sử dụng JUnit4

Lớp User:

package com.example.spring.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    @Value("Nguyễn Văn A")
    private String ten;

    @Override
    public String toString() {
        return "User{" +
                "ten='" + ten + '\'' +
                '}';
    }

    public String getTen() {
        return ten;
    }

    public void setTen(String ten) {
        this.ten = ten;
    }

    public User() {
    }

    public User(String ten) {
        this.ten = ten;
    }
}

Cấu hình XML: Bật quét thành phần

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.example.spring.bean"/>

</beans>

Test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class TestJunit4 {
    @Autowired
    private User nguoiDung;
    @Test
    public void  test1(){
        System.out.println(nguoiDung.getTen());
    }
}

2. Sử dụng JUnit5:

@SpringJUnitConfig(locations = "classpath:spring.xml")
public class TestJunit5 {
    @Autowired
    private User nguoiDung;
    @Test
    public void  test1(){
        System.out.println(nguoiDung.getTen());
    }
}

Tích hợp Spring6 với MyBatis

Quy trình tích hợp:

  1. Chuẩn bị bảng cơ sở dữ liệu
  2. Tạo module trong IDE và thêm các dependency cần thiết:
    • spring-context
    • spring-jdbc
    • MySQL driver
    • mybatis
    • mybatis-spring
    • Druid connection pool
    • junit
  3. Thiết lập kiến trúc 3 lớp và tạo các package cần thiết
  4. Viết lớp POJO
  5. Viết mapper interface
  6. Viết file cấu hình mapper
  7. Viết interface service và class implementation
  8. Viết file jdbc.properties
  9. Viết file mybatis-config.xml
  10. Viết file spring.xml với cấu hình:
    • Quét thành phần
    • Import file thuộc tính bên ngoài
    • Cấu hình nguồn dữ liệu
    • Cấu hình SqlSessionFactoryBean
    • Cấu hình mapper scanner
    • Cấu hình transaction manager
    • Kích hoạt transaction annotation
  11. Viết test program, thêm transaction và test

Resource trong Spring

Java URL class không đáp ứng được tất cả các yêu cầu truy cập tài nguyên low-level. Spring Resource cung cấp khả năng truy cập tài nguyên low-level mạnh mẽ hơn.

1. Resource interface

Spring Resource interface nằm trong org.springframework.core.io, cung cấp các phương thức quan trọng:

  • getInputStream(): Mở và trả về InputStream để đọc tài nguyên
  • exists(): Kiểm tra tài nguyên có tồn tại hay không
  • isOpen(): Kiểm tra tài nguyên có đang mở stream hay không
  • getDescription(): Trả về mô tả tài nguyên

2. Các lớp triển khai của Resource

UrlResource - Truy cập tài nguyên mạng

Triển khai Resource để truy cập tài nguyên mạng, hỗ trợ đường dẫn URL tuyệt đối:

  • http: - Truy cập tài nguyên dựa trên HTTP protocol
  • ftp: - Truy cập tài nguyên dựa trên FTP protocol
  • file: - Đọc tài nguyên từ hệ thống tệp

Thẻ: Spring6 JUnit mybatis Resource Java Framework

Đăng vào ngày 20 tháng 6 lúc 03:24