SpringBoot study notes four - Integration

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_38930706/article/details/99306110

SpringBoot connection & database integration frameworks MyBatis

Oracle connection required dependencies:

<dependency>
	<groupId>com.oracle</groupId>
	<artifactId>ojdbc6</artifactId>
	version>11.2.0.1.0</version>
</dependency>

springBoot configure the connection Oracle:

#springboot连接Oracle数据库
spring.datasource.driver-class-name = oracle.jdbc.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@ip:port/key
spring.datasource.username = username
spring.datasource.password = password

We can use SpringBoot JdbcTemplate template comes with database operations:

        String sql = "select * from TICKET where START_STATION = ?";
		RowMapper<TicketDto> rowMapper = new BeanPropertyRowMapper<TicketDto>(TicketDto.class);
		List<TicketDto> ticketDtoResultList = null;
		try {
			ticketDtoResultList = (List<TicketDto>) jdbcTemplate.query(sql, rowMapper,startArea);
		} catch (DataAccessException e) {
			logger.debug("!!!!!!!!!SQL查询出错"+e);
		}

A single record query using queryForObject method;

In fact more of a use MyBatis framework to operate:

Integration MyBatis

The introduction of dependence:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>

Write Mapper interfaces:

package com.mlgg.mapper;

import com.mlgg.my12306.param.TicketDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TicketMapper {

    @Select("select * from TICKET where START_STATION = #{startArea} AND END_STATION = #{distArea} ")
    List<TicketDto> checkTicketByStationAndTime(@Param("startArea") String startArea, @Param("distArea") String distArea, @Param("startTime") String startTime);
}

have a test:

package com.mlgg.service;

import com.mlgg.mapper.TicketMapper;
import com.mlgg.my12306.param.TicketDto;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceApplicationTests {
    @Autowired
    TicketMapper ticketMapper;

	@Test
    public void CheckTicket(){
        List<TicketDto> ResultList = ticketMapper.checkTicketByStationAndTime("西安市", "北京市", "");
        for (TicketDto dto: ResultList
             ) {
            System.out.println(dto);
        }
    }
}

result:

SpringBoot integrate RabbitMQ

The introduction of starter:

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

SpringBoot-RabbitMQ automatic configuration file is automatically created Rabbit --RabbitAutoConfiguration.java Providing the ConnectionFactory factory, the factory created in accordance with the configuration of Rabbit RabbitProperties.java, the sending and receiving messages Rabbit RabbitTemplate.java encapsulated in the package management Rabbit in AmqpAdmin.java in.

Suppose we have the time to integrate the entry RabbitMQ Ha ~

Test messaging middleware

We said earlier, Rabbit message is encapsulated in RabbitTemplate operation, and the management has been handed over to Spring, so when we tested the RabbitTemplate injected into the test class, write the publisher and subscriber test method:

package com.mlgg.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Autowired
    AmqpTemplate amqpTemplate;

    //Publisher
    @Test
    public void contextLoads() {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "这是第一条消息");
        map.put("data", Arrays.asList("hello world"));
        map.put("flag", false);
        amqpTemplate.convertAndSend("exchange.direct","atguigu.news", map);
    }

    @Test
    public void receiveLoads() throws Exception {
        Object o = amqpTemplate.receiveAndConvert("atguigu.news");
        if (o == null) {
            throw new Exception("null");
        } else {
            System.out.println(o.getClass());
            System.out.println(o);
        }
    }

}

Contributors will be serialized and sent messages, we have seen what the message sent in management platform:

Payload is garbled reasons, rabbitMQ default JVM Serialization, we can achieve JSON serialization by overwriting configuration:

package com.mlgg.demo.configuration;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyAMQPConfig {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

The results are as follows:

消费者在控制台根据序列化结果进行反序列化,打印了订阅到的消息,看一下测试的结果:

Guess you like

Origin blog.csdn.net/weixin_38930706/article/details/99306110