我们使用 SQLite 来进行对 Exchange, Queue, Binding 的硬盘保存
对 Message 就保存在硬盘的文本中
SQLite 封装
这里是在 application.yaml 中来引进对 SQLite 的封装
spring:
datasource:
url: jdbc:sqlite:./data/meta.db
username:
password:
driver-class-name: org.sqlite.JDBC
mybatis:
mapper-locations: classpath:mapper/**Mapper.xml
先进行建表操作:
使用一个接口类和对应的 xml 就可以进行对数据库的操作
然后对增删查进行封装
完整代码:
SQliteMapper:
package com.example.mq.mqserver.mapper;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.MSGQueue;
import java.util.List;
@Mapper //数据持久层
public interface SQLiteMapper {
// 建表
void createExchangeTable();
void createQueueTable();
void createBindingTable();
// 增删查
int insertExchange(Exchange exchange);
int deleteExchange(String exchangeName);
List<Exchange> selectAllExchange();
int insertQueue(MSGQueue queue);
int deleteQueue(String queueName);
List<MSGQueue> selectAllQueue();
int insertBinding(Binding binding);
int deleteBinding(Binding binding);
List<Binding> selectAllBinding();
}
SQLiteMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mq.mqserver.mapper.SQLiteMapper">
<update id="createExchangeTable">
create table if not exists exchange (name varchar(50) primary key, durable boolean, type int);
</update>
<update id="createQueueTable">
create table if not exists queue(name varchar(50) primary key, durable boolean);
</update>
<update id="createBindingTable">
create table if not exists binding(exchangename varchar(50), queuename varchar(50), durable boolean, bindingkey varchar(50));
</update>
<insert id="insertExchange">
insert into exchange values(#{name}, #{durable}, #{type});
</insert>
<delete id="deleteExchange">
delete from exchange where name = #{exchangeName};
</delete>
<select id="selectAllExchange" resultType="com.example.mq.mqserver.core.Exchange">
select * from exchange;
</select>
<insert id="insertQueue">
insert into queue values(#{name}, #{durable});
</insert>
<delete id="deleteQueue">
delete form queue where name = #{queueName};
</delete>
<select id="selectAllQueue" resultType="com.example.mq.mqserver.core.MSGQueue">
select * from queue;
</select>
<insert id="insertBinding">
insert into binding values(#{exchangeName}, #{queueName}, #{durable}, #{bindingKey});
</insert>
<delete id="deleteBinding">
delete from binding where exchangename = #{exchangeName} and queueName = #{queueName};
</delete>
<select id="selectAllBinding" resultType="com.example.mq.mqserver.core.Binding">
select * from binding;
</select>
</mapper>
然后我们创建一个类来对整合上述的数据库操作
先在 mqserver 中创建一个 datacenter 包, 这个包用来进行所有数据的管理
然后在包里创建一个 DataBaseManager 的类进行数据库操作的整合
DataBaseManager
checkDBExists() 查找数据库是不是存在
createTable() 之前构造好的方法
public boolean checkDBExists(){
File file = new File("./data/meta.db");
if(file.exists()){
return true;
}
return false;
}
private void createTable(){
sqLiteMapper.createExchangeTable();
sqLiteMapper.createQueueTable();
sqLiteMapper.createBindingTable();
}
init() 对数据库进行初始化
// 针对数据库进行初始化
// 有 data 文件夹的话就不用创建
// 没有就进行创建
public void init(){
if(!checkDBExists()){
// 创建文件夹
File dataDir = new File("./data");
dataDir.mkdirs();
// 创建数据表
createTable();
System.out.println("[DataBaseManager] 数据库初始化完成");
}else {
System.out.println("[DatBaseManager] 数据库已经存在");
}
}
然后是增删查:
这里把整个类全部放上来
public class DataBaseManager {
@Autowired
SQLiteMapper sqLiteMapper;
// 针对数据库进行初始化
// 有 data 文件夹的话就不用创建
// 没有就进行创建
public void init(){
if(!checkDBExists()){
// 创建文件夹
File dataDir = new File("./data");
dataDir.mkdirs();
// 创建数据表
createTable();
System.out.println("[DataBaseManager] 数据库初始化完成");
}else {
System.out.println("[DatBaseManager] 数据库已经存在");
}
}
public boolean checkDBExists(){
File file = new File("./data/meta.db");
if(file.exists()){
return true;
}
return false;
}
private void createTable(){
sqLiteMapper.createExchangeTable();
sqLiteMapper.createQueueTable();
sqLiteMapper.createBindingTable();
}
public void deleteDB(){
// 先删除里面的文件
File file = new File("./data/meta.db");
boolean ret = file.delete();
if(ret){
System.out.println("[DataBaseManager] 删除数据库文件成功!");
}else {
System.out.println("[DataBaseManager] 删除数据库文件失败!");
}
// 再删除文件夹
File dataDir = new File("./data");
ret = dataDir.delete();
if(ret){
System.out.println("[DataBaseManager] 删除数据库目录成功");
}else {
System.out.println("[DataBaseManager] 删除数据库目录失败");
}
}
// 交换机操作
public int insertExchange(Exchange exchange){
return sqLiteMapper.insertExchange(exchange);
}
public int deleteExchange(String stingName){
return sqLiteMapper.deleteExchange(stingName);
}
public List<Exchange> selectAllExchanges(){
return sqLiteMapper.selectAllExchange();
}
// 队列操作
public int insertQueue(MSGQueue queue){
return sqLiteMapper.insertQueue(queue);
}
public int deleteQueue(String queueName){
return sqLiteMapper.deleteQueue(queueName);
}
public List<MSGQueue> selectAllQueue(){
return sqLiteMapper.selectAllQueue();
}
// 队列操作
public int insertBinding(Binding binding){
return sqLiteMapper.insertBinding(binding);
}
public int deleteBinding(Binding binding){
return sqLiteMapper.deleteBinding(binding);
}
public List<Binding> selectAllBinding(){
return sqLiteMapper.selectAllBinding();
}
}