SpringBoot与Mybatis,Redis,ActiveMQ的整合

本文详细介绍如何在SpringBoot项目中集成Mybatis、Redis和ActiveMQ,包括配置依赖、设置缓存、创建消息队列、生产者与监听器等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面我已经写过SpringBoot与Mybatis在IDEA的开发工具下的集成,今天我们还在IDEA的工具下做SpringBoot与Mybatis,Redis,ActiveMQ的集成。当然,这次的项目是在之前springboot与mybatis的项目的基础上再进一步。接下来进入正题。

要求:

开发工具:IDEA
jdk:1.8
注:本次采用全注解方式进行开发。

1.创建springboot项目

因为之前已经在写springboot与mybatis的集成项目中已经创建过,所以我就直接在之前的项目基础上进行开发。

2.修改pom.xml文件
 添加Redis依赖
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-redis</artifactId>
 <version>1.4.4.RELEASE</version>
<dependency>
3.指定Redis缓存主机地址

spring.redis.port=6379
spring.redis.host=localhost

4.添加缓存注解
 @SpringBootApplication
 @MapperScan("com.mapper.*")
 @EnableCaching//开启缓存
public class Application1 extends SpringBootServletInitializer {
public static void main(String[] args) {
    SpringApplication.run(Application1.class,args);
}
  @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application1.class);
  }
}

在业务逻辑类的getAllUsers()方法上添加@Cacheable注解来支持缓存

 @Service
public class UserService {
 @Autowired
private UserMapper userMapper;
public User findById(int id) {
    //System.out.println(id);
    return userMapper.findById(id);
}
@Cacheable(value = "UserCache",key = "'user.findAllUsers'")
public List<User> findAllUsers(){

    return userMapper.findAllUsers();
}
}

注意:@Cacheable注解中的Key属性值除了需要被英文双引号引用外,还需要添加英文单引号。

5.实体类实现可序列化接口

为了便于数据的传输,需要将实体User实现序列化接口Serializable.

public class User implements Serializable {
private static final long serialversionUID=1L;
private int id;
private String username;
private String adress;
private String sex;
//getter与setter
}

完成上面的步骤后,SpringBoot,Mybatis与Redis的集成就做完了。
接下来做与ActiveMQ的集成。

与ActiveMQ得集成

1.修改pom.xml文件
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2.创建消息队列对象

在启动类中编写一个创建消息队列得方法

@Bean
public Queue queue(){
return new ActiveMQQueue("active.queue");
}

@Bean注解用于定义一个Bean

3.创建消息生产者

创建一个队列消息的控制器类QueueController,并在类中编写发送消息的方法。

  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.jms.core.JmsMessagingTemplate;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  import javax.jms.Destination;
  import javax.jms.Queue;
  @RestController
  public class QueueController {
  @Autowired
  private JmsMessagingTemplate jmsMessagingTemplate;
 @Autowired
  private Queue queue;
/**
 * 消息生产者
 */
@RequestMapping("/send")
public void send(){
    //指定消息发送的目的地及内容
    this.jmsMessagingTemplate.convertAndSend(this.queue,"新发送的消息!");
}
}

在上面的代码中,send方法通过imsMessagingTemplate的convertAndSend()方法指定了消息发送的目的地为Queue对象,所发送的内容为"新发送的消息!"

4.创建消息监听器

创建一个客户控制器类CustomerController,并在类中编写监听和读取消息的方法。

import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
/**
 * 客户控制器
 */
@JmsListener(destination = "active.queue")
public void readActiveQueue(String message){
    System.out.println("接收到"+message);
}
}

在上面的代码中,@JmsListener是Spring4.1所提供的用于监听JMS消息的注解。该注解的属性destination用于指定要监听得目的地。我们这个案例中监听的是active.queue中的消息。

5.启动项目,测试应用

在浏览器中输入http://localhost:8080/send后,在IDEA的控制台将显示所接受到的消息。运行结果
到此,我们的集成就做完了。在后继的学习中,我还会将自己总结的经验拿出来给大家看。谢谢大家的观看!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值