SpringBoot integrates third-party technologies

Table of contents

1. SpringBoot integrates JUnit

1.1 Spring integrates JUnit

1.2 SpringBoot integrates JUnit

2. Realize SSM integration based on SpringBoot

2.1 Spring integrates MyBatis

2.2 SpringBoot integrates MyBatis

 2.3 Case - SpringBoot implements ssm integration


1. SpringBoot integrates JUnit

1.1 Spring integrates JUnit

1.2 SpringBoot integrates JUnit

[Step 1] Add and integrate junit start-up dependencies (you can check directly)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

[Step 2] Write a test class, which is automatically generated by default

@SpringBootTest
class Springboot07JunitApplicationTests {
    @Autowired
    private BookService bookService;

    @Test
    public void testSave() {
        bookService.save();
    }
}

2. Realize SSM integration based on SpringBoot

  • SpringBoot integrates Spring (does not exist)

  • SpringBoot integrates SpringMVC (does not exist)

  • SpringBoot integrates MyBatis (mainly)

2.1 Spring integrates MyBatis

(1)SpringConfig

  • importJdbcConfig

  • Import MyBatisConfig

@Configuration @ComponentScan("com.itheima") @PropertySource("classpath:jdbc.properties")

@Import({JdbcConfig.class, MyBatisConfig.class})

public class SpringConfig {

}

(2)JDBCConfig

  • Define data sources (load properties configuration items: driver, url, username, password)

#jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=itheima

public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource getDataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

(3)MyBatisConfig

  • Define the SqlSessionFactoryBean

  • Define mapping configuration

@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) {
    SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
    ssfb.setTypeAliasesPackage("com.itheima.domain");
    ssfb.setDataSource(dataSource);
    return ssfb;

@Bean
public MapperScannerConfigurer getMapperScannerConfigurer() {
    MapperScannerConfigurer msc = new MapperScannerConfigurer();
    msc.setBasePackage("com.itheima.dao");
    return msc;

2.2 SpringBoot integrates MyBatis

①: Create a new module, select Spring initialization, and configure basic information about the module

 ②: Select the technology set that the current module needs to use (MyBatis, MySQL)

③: Set data source parameters

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

Precautions:

When the SpringBoot version is lower than 2.4.3 (not included), and the Mysql driver version is greater than 8.0, you need to configure the time zone in the url connection string, or configure the time zone on the MySQL database side to solve this problem

jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC

④: Define the data layer interface and mapping configuration

 @Mapper
public interface UserDao {
    @Select("select * from tbl_book where id=#{id}")
    Book getById(Integer id);
}

⑤: Inject the dao interface into the test class to test the function  

@SpringBootTest
class Springboot08MybatisApplicationTests {
    @Autowired
    private BookDao bookDao;

    @Test
    public void testGetById() {
        Book book = bookDao.getById(1);
        System.out.println(book);
    }
}

 2.3 Case - SpringBoot implements ssm integration

[Step 1] Create a SpringBoot project and add druid dependencies

 <!-- todo 1 添加druid连接池依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>

[Step 2] Copy various resources of springmvc_11_page project (main java class, page, test class)

[Step 3] Delete all configurations in the config package, and add @Mapper annotation to the BookDao interface

 //todo 3 Add the @Mapper annotation to the BookDao interface and let SpringBoot create a proxy object for the interface
@Mapper
public interface BookDao {     //... }

[Step 4] Modify application.properties to application.yml, configure port number and connection parameters  

 server:
  port: 80
# todo 4 Configure database connection parameters
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

[Step 5] Modify the BookServiceTest configuration class and configure it  

// todo 5 modify the unit test class, add @SpringBootTest primary key, fix @Test annotation guide package
@SpringBootTest
public class BookServiceTest {

    @Autowired
    private BookService bookService;

    @Test
    public void testGetById(){         Book book = bookService.getById(2); //Passing parameter 1 will throw an exception         System.out.println(book);     }     @Test     public void testGetAll(){         List<Book> all = bookService.getAll();         System.out.println(all);     } }








[Step 6] Provide the index.html page in the static directory and jump to "pages/books.html"

<script>
   location.href="pages/books.html"
</script>

 Finally: Run the boot class to access

Guess you like

Origin blog.csdn.net/m0_69057918/article/details/131168319