SpringBoot學習筆記四——整合

SpringBoot連接數據庫&整合MyBatis框架

連接Oracle需要的依賴:

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

springBoot配置連接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

我們可以使用SpringBoot自帶的JdbcTemplate模板進行操作數據庫:

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

單條記錄查詢使用queryForObject方法;

實際上更多的是使用MyBatis框架進行操作:

整合MyBatis

引入依賴:

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

編寫Mapper接口:

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

測試一下:

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

結果:

SpringBoot整合RabbitMQ

引入starter:

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

SpringBoot-RabbitMQ的自動配置文件——RabbitAutoConfiguration.java 提供了自動創建Rabbit工廠ConnectionFactory,創建工廠根據的Rabbit配置在RabbitProperties.java中,將Rabbit發送和接收消息封裝在RabbitTemplate.java中,將Rabbit的管理封裝在AmqpAdmin.java中。

整合的時候假設我們已經入門了RabbitMQ 哈 ~

測試消息中間件

我們前面說了,Rabbit消息操作封裝在RabbitTemplate中,且已經交給了Spring管理,所以我們在測試的時候將RabbitTemplate注入到測試類中,編寫發佈者和訂閱者測試方法:

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

}

提供者將消息進行序列化併發送,我們在管理平臺上看一下發送的消息:

Payload亂碼的原因是,rabbitMQ默認使用JVM序列化,我們可以通過覆寫配置來實現JSON序列化:

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

結果如下:

消費者在控制檯根據序列化結果進行反序列化,打印了訂閱到的消息,看一下測試的結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章