【RabbitMQ消息中间件】12.RabbitMQ结合SSM框架-编写仓储系统


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u013517797/article/details/79778241
了解了RabbitMQ的基本知识和几大队列模式,以及Spring-Rabbit开源工程的基本原理后,我们动手来实现在实际工作开发中需要与SSM框架结合使用的工程场景。

该场景模拟以下活动:

货仓管理系统用于对货物的管理,它的每一次进货(insert)和修改(update)、删除(delete)都会向消息中间件推送消息,而销售系统会从消息中间件中获取货物的信息,同步至销售系统的数据库。

首先我们创建货仓管理系统,在Eclipse中创建一个名为“Warehouse_Management”的Maven工程:

然后在pom.xml文件中加入Spring/Spring MVC以及MyBatis框架环境需要的依赖:


  4.0.0
  cn.com.jack.rabbit_test
  Warehouse_Management
  0.0.1-SNAPSHOT
  war
  
    
    UTF-8  
    UTF-8  
      
    4.2.5.RELEASE  
      
    3.2.8  
      
    5.1.29  
      
    1.7.18  
    1.2.17  
   
  
    
      
      
      jstl  
      jstl  
      1.2  
      
  
      
      javax  
      javaee-api  
      7.0  
      
  
      
      
      junit  
      junit  
      4.11  
        
      test  
      
  
      
      
      org.springframework  
      spring-core  
      ${spring.version}  
      
      
      org.springframework  
      spring-web  
      ${spring.version}  
      
      
      org.springframework  
      spring-oxm  
      ${spring.version}  
      
      
      org.springframework  
      spring-tx  
      ${spring.version}  
      
      
      org.springframework  
      spring-jdbc  
      ${spring.version}  
      
      
      org.springframework  
      spring-webmvc  
      ${spring.version}  
      
      
      org.springframework  
      spring-context  
      ${spring.version}  
      
      
      org.springframework  
      spring-context-support  
      ${spring.version}  
      
      
      org.springframework  
      spring-aop  
      ${spring.version}  
      
  
      
      org.springframework  
      spring-test  
      ${spring.version}  
      
    
	    org.aspectj
	    aspectjweaver
	    1.8.10
	
  
      
      
      org.mybatis  
      mybatis  
      ${mybatis.version}  
      
  
      
      
      org.mybatis  
      mybatis-spring  
      1.2.2  
      
  
      
      
      mysql  
      mysql-connector-java  
      ${mysql-driver.version}  
        
    
	
		c3p0
		c3p0
		0.9.1.2
	  
  
      
      
      log4j  
      log4j  
      ${log4j.version}  
      
      
      org.slf4j  
      slf4j-api  
      ${slf4j.version}  
      
      
      org.slf4j  
      slf4j-log4j12  
      ${slf4j.version}  
      
  
      
  
    
      
      commons-fileupload  
      commons-fileupload  
      1.3.1  
      
  
      
      commons-io  
      commons-io  
      2.4  
      
  
      
      commons-codec  
      commons-codec  
      1.9  
      
    
  
  
    
      
        maven-compiler-plugin
        
          1.6
          1.6
        
      
      
      	maven-war-plugin
      	
      		3.0
      	
      
    
  

工程目录如下:


接下来先创建数据库,我使用的是Mysql数据库,操作数据时使用的是sqlyog的图形化工具。我们在sqlyog中创建一个数据库名称叫“warehouse_management”,是货仓管理的数据库:

其中创建一个名为product的表,存储货物信息:

建表语句为:

CREATE TABLE `product` (
   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
   `pname` varchar(200) COLLATE utf8_bin NOT NULL COMMENT '货物名称',
   `price` double NOT NULL COMMENT '价格',
   `pdesc` text COLLATE utf8_bin COMMENT '货物描述',
   `weight` int(11) DEFAULT NULL COMMENT '重量',
   `model` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT '型号规格',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

  然后是MyBtais的配置文件“sqlMapConfig.xml”,因为MyBatis交给了Spring托管,所以这里仅配置了一个别名:

<?xml version="1.0" encoding="UTF-8"?>  
  
  
      
      
      
      
      
          
          
      
    
      
      
 

然后在src/mian/resource下创建spring的配置文件“beans.xml”:

<?xml version="1.0" encoding="UTF-8"?>

			
			
		
		
		
		
		
		
		
			
			
			
			
			
			
			
			
			
			
			
			
			
		
		
		
		
			
			
			
			
		
		
		
		
			
		
		
		
		
			
				
				
				
				
				
				
				
				
			
		
		
		
			
			
		
		
		
	    
	

其中首先加载数据库配置的属性文件“db.properties”,然后设置包扫描的路径,用于扫描DAO或者Service的暴露以及注入注解 ,之后设置DataSource数据源,将“db.properties”中的参数动态设置进去。然后设置MyBatis的SessionFactory会话工厂,用与处理系统与数据库之间的交互。下面是事务管理和事务通知的配置,事务通知以切面的方式切入Service层中。

然后创建spring mvc的配置文件“springmvc-action.xml”:

<?xml version="1.0" encoding="UTF-8"?>

	
	
	
	
	
	
		
		
	
	
	
	
	
	
	
 
 
	
	
		
	
	
	  
	  
	  
	

  

在springmvc配置文件中,我们配置了扫描controller包注解的配置、视图解析器、注解驱动、文件上传解析器(其实没用到)。因为我们做的小实例,不做登录系统,也不做权限控制,所以这里拦截器配置注释掉,忽略。

最后配置的是日志的配置文件“log4j.properties”:

# Global logging configuration  
log4j.rootLogger=DEBUG, stdout  
# Console output...  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n 

因为我们是Web工程,要把Maven改为Web结构。邮件工程点击properties,进入“project Facets”中设置Web属性:

可以看到maven生成了Web相关结构目录:

 我们在WEB-INF下创建“page”文件夹,用于放置jsp视图(上面配置文件配置的就是这个目录)。

然后我们在src/main/java下创建一些包,用于放置各种类型的类:

下面在“com.warehouse_management.mapper”包下编写一个mapper配置,用户货物数据的增删改查:

<?xml version="1.0" encoding="UTF-8"?>
  
 
 
  
    
	
		
		
		
		
		
		
		
		
	
	
	
	
	
	
	
	
	
		insert into product
		(PNAME,PRICE,PDESC,WEIGHT,MODEL)
		values
		(
			#{name,jdbcType=VARCHAR},
			#{price,jdbcType=DOUBLE},
			#{desc},
			#{weight,jdbcType=INTEGER},
			#{model,jdbcType=VARCHAR}
		)
	
	
	
	
		update product
		
	 	   pname=#{name},
		   price=#{price},
		   pdesc = #{desc},
		   weight=#{weight},
		   model=#{model}
		
		where id=#{id}
	
	
	
	
		delete from product
		where id=#{id}
	
 

 然后在com.warehouse_management.po包下创建名为“Product”的实体类:

package com.warehouse_management.po;
 
public class Product {
	private int id;
	private String name;
	private Double price;
	private String desc;
	private Integer weight;
	private String model;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public Integer getWeight() {
		return weight;
	}
	public void setWeight(Integer weight) {
		this.weight = weight;
	}
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
}

  然后我们分别在Dao包下编写Dao的基础类,使用SqlSession调用相关的SQL配置的公用封装:
接口:

package com.warehouse_management.dao;
 
import java.io.Serializable;
import java.util.List;
import java.util.Map;
 
//泛型类,基础的DAO接口  
public interface BaseDao {  
  public T selectById(int id);//只查询一个,常用于修改  
  public List selectAll(Map map);//根据条件查询多个结果
  public void insert(T entity);//插入,用实体作为参数  
  public void update(T entity);//修改,用实体作为参数  
  public void deleteById(int id);//按id删除,删除一条
} 

实现类:

package com.warehouse_management.dao.impl;
 
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import com.warehouse_management.dao.BaseDao;
 
public abstract class BaseDaoImpl extends SqlSessionDaoSupport implements BaseDao{  
    @Autowired  
    //mybatis-spring 1.0无需此方法;mybatis-spring1.2必须注入。  
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){  
        super.setSqlSessionFactory(sqlSessionFactory);  
    }  
      
    private String ns;      //命名空间  
    public String getNs() {  
        return ns;  
    }  
    public void setNs(String ns) {  
        this.ns = ns;  
    }  
  
    public List selectAll(Map map) {  
        List oList = this.getSqlSession().selectList(ns + ".selectAll", map);  
        return oList;  
    }  
    public T selectById(int id) {  
        return this.getSqlSession().selectOne(ns + ".selectById", id);  
    }  
  
    public void insert(T entity) {  
        this.getSqlSession().insert(ns + ".insert", entity);  
    }  
  
    public void update(T entity) {  
        this.getSqlSession().update(ns + ".update", entity);  
    }  
  
    public void deleteById(int id) {  
        this.getSqlSession().delete(ns + ".deleteById", id);  
    } 
}  

  然后在Dao与Service下创建Product相关的增删改查类:
Dao层接口:

package com.warehouse_management.dao;
 
import java.util.List;
import java.util.Map;
import com.warehouse_management.po.Product;
 
public interface ProductDao extends BaseDao{  
	public Product selectById(int id);//只查询一个,常用于修改  
    public List selectAll(Map map);//根据条件查询多个结果
    public void insert(Product commodties);//插入,用实体作为参数  
    public void update(Product commodties);//修改,用实体作为参数  
    public void deleteById(int id);//按id删除,删除一条
}

  Dao层实现:

package com.warehouse_management.dao.impl;
 
import org.springframework.stereotype.Repository;
import com.warehouse_management.dao.ProductDao;
import com.warehouse_management.po.Product;
 
@Repository //为了包扫描的时候这个Dao被扫描到  
public class ProductDaoImpl extends BaseDaoImpl implements ProductDao{  
    public ProductDaoImpl(){  
        //设置命名空间  
        super.setNs("com.warehouse_management.mapper.ProductMapper");  
    }
    //在下面可以实现自己的查询方法

  Service层接口

package com.warehouse_management.service;
 
import java.util.List;
import java.util.Map;
import com.warehouse_management.po.Product;
 
public interface ProductService{  
	public Product selectById(int id);//只查询一个,常用于修改  
    public List selectAll(Map map);//根据条件查询多个结果
    public void insert(Product commodties);//插入,用实体作为参数  
    public void update(Product commodties);//修改,用实体作为参数  
    public void deleteById(int id);//按id删除,删除一条
}

  Service层实现:

package com.warehouse_management.service.impl;
 
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.warehouse_management.dao.ProductDao;
import com.warehouse_management.po.Product;
import com.warehouse_management.service.ProductService;
 
@Service  //为了包扫描的时候这个Service被扫描到  
public class ProductServiceImpl implements ProductService{
 
    @Autowired  
    private ProductDao productDao;
	
	@Override
	public Product selectById(int id) {
		return productDao.selectById(id);
	}
 
 
	@Override
	public List selectAll(Map map) {
		return productDao.selectAll(map);
	}
 
 
	@Override
	public void insert(Product product) {
		productDao.insert(product);
	}
 
 
	@Override
	public void update(Product product) {
		productDao.update(product);
	}
 
 
	@Override
	public void deleteById(int id) {
		productDao.deleteById(id);
	}
 
 
}

  然后在Controller创建相关服务响应:

package com.warehouse_management.controller;
 
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.warehouse_management.po.Product;
import com.warehouse_management.service.ProductService;
 
@Controller
public class ProductController { 
	
	@Autowired
	private ProductService productService;
	//创建该类的日志对象
	Log log = LogFactory.getLog(this.getClass());  
	
	
	//跳转至列表页面
	@RequestMapping("/product/home.action")  
    public String list(Model model){ 
		List productList = productService.selectAll(null);
		model.addAttribute("productList",productList);//货物列表
		return "/product/home.jsp";//转向首页
	}
	
	@RequestMapping("/product/toAdd.action")  
    public String toAdd(Model model){   
		return "/product/add.jsp";//转向添加页
    } 
	
	@RequestMapping("/product/add.action")  
    public String add(Model model,Product product){   
		productService.insert(product);
		//重新刷新至分页列表页首页
		return list(model);
    }  
	
	@RequestMapping("/product/toEdit.action")  
    public String toEdit(Model model,Integer id){   
		if(id!=null){
			model.addAttribute("product", productService.selectById(id));
		}
		return "/product/edit.jsp";//转向编辑页
    } 
	
	@RequestMapping("/product/edit.action")  
    public String edit(Model model,Product product){   
		productService.update(product);
		//重新刷新至分页列表页首页
		return list(model);
    } 
 
 
    	@RequestMapping("/product/delete.action")  
    public String delete(Model model,Integer id){   
		productService.deleteById(id);
		//重新刷新至分页列表页首页
		return list(model);
    } 
}

分别是加载所有商品、新增跳转、执行新增、编辑跳转、进行编辑的服务

然后在WEB-INF下创建一个“pages”文件夹,在下面创建“product”文件夹,在下面放置商品列表、新增、编辑的Jsp文件:
home.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


  
    仓储管理系统首页
  
  
  
    

仓储管理系统


序号货物名称价格货物描述 重量型号规格操作
${status.index+1}${item.name } ${item.price}${item.desc } ${item.weight}${item.model} 编辑| 删除
搜索结果为空!

add.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


  
    新增
    <script type="text/javascript">
    	function checkAdd(){
    	   var addName = document.getElementById("addName").value;
    	   var addPrice = document.getElementById("addPrice").value;
    	   var addWeight = document.getElementById("addWeight").value;
           if(addName==null||addName==""){
               alert("用户名不能为空!");
               return false;
           }
           if(addPrice==null||addPrice==""){
               alert("手机号不能为空!");
               return false;
           }
           var myreg = /^[-\+]?\d+(\.\d+)?$/;
           if(!myreg.test(addPrice)||!myreg.test(addWeight)) 
           { 
               alert("价格或重量请输入数字!"); 
               return false; 
           }
           return true;
	   }
    </script>
  
  
  
    
商品名称:
价格:
描述:
重量:
型号规格:

  edit.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


  
    新增
    <script type="text/javascript">
    	function checkAdd(){
    	   var addName = document.getElementById("addName").value;
    	   var addPrice = document.getElementById("addPrice").value;
    	   var addWeight = document.getElementById("addWeight").value;
           if(addName==null||addName==""){
               alert("用户名不能为空!");
               return false;
           }
           if(addPrice==null||addPrice==""){
               alert("手机号不能为空!");
               return false;
           }
           var myreg = /^[-\+]?\d+(\.\d+)?$/;
           if(!myreg.test(addPrice)||!myreg.test(addWeight)) 
           { 
               alert("价格或重量请输入数字!"); 
               return false; 
           }
           return true;
	   }
    </script>
  
  
  
    
商品名称:
价格:
描述:
重量:
型号规格:

  最后在默认的index.jsp添加一个跳转路径,默认执行home服务,这样进入系统直接可以看到列表界面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


  
    index
  
  
    <script language="JavaScript">
	 window.location.href = "product/home.action";
	</script>
  

  最后在WEB-INF下创建web.xml,配置spring mvc的拦截器:

<?xml version="1.0" encoding="UTF-8"?>

	
	
		contextConfigLocation
		/WEB-INF/classes/beans.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:springmvc-action.xml
		
	
	
	
		springmvc
		/
	
	
	
	
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
	
	
		CharacterEncodingFilter
		/*
	
	
  
    index.jsp
  

最后,别忘记在编译路径中添加Maven依赖的编译路径:


为了测试数据读取,我们在数据库中预先添加一些数据:

将应用部署至Tomcat:

然后启动Tomcat,我这里端口是80,所以不用输入端口号,在浏览器中访问“http://localhost/Warehouse_Management/”:

可以看到,我们的仓储系统已经完全创建完毕。

下一篇我们在该工程的基础上集成RabbitMQ,然后实现在新增和编辑、删除的时候向消息队列推送信息。

转载请注明出处:https://blog.csdn.net/acmman/article/details/79778241
————————————————
版权声明:本文为CSDN博主「光仔December」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/acmman/article/details/79778241