zookeeper+dubbo+ssm入门案例

记录第一次使用,帮助以后回忆
源码点击这里

环境

  1. jdk:1.8
  2. dubbo-2.6.1
  3. zookeeper-3.6.3

在这里插入图片描述

结构

上文中zookeeper+dubbo入门案例中,提供方消费方中都写了一遍的service接口,造成代码重复,这里建立父工程、jar包
parent - - - - - - - - - - - - - - - 打pom包
interface pojo dao - - - - - - - 打jar包
service web - - - - - - - - - - - 打war包

准备工作

父工程students-parent

很简单,主要提供一些使用的依赖,供后续使用,如ssm的、dubbo的zookeeper的、mysql的,当然还有pojo依赖,
在这里插入图片描述

数据层students-dao

坑:一般配置文件放在resources中,但是mybatis习惯跟放在java放在一起,因为maven的打包机制原因,java包下只打包java文件,所以造成mapper.xml漏打,需要一个配置。
坑2:平常的mybatis是交给spring代理,而spring的启动需要web.xml启动,这里是个jar包没有web.xml,方法:把他交给提供方,提供方启动的时候顺带着把他加载了。(下面提供方有写)
在这里插入图片描述

  1. mapper.xml漏打处理,写在了parent父工程里,一劳永逸
<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
	</build>
  1. dao层
    其实就是mybatis的老一套
    先是一个mapper接口
package org.students.mapper;

import org.students.pojo.Student;

public interface StudentMapper {
	Student queryStudentByStuno(int stuNo);
	void addStudent(Student student);
}

然后mapper.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">
<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.students.mapper.StudentMapper">
	<select id="queryStudentByStuno" 	parameterType="int"  	resultType="org.students.pojo.Student"  >
		select * from student where stuno = #{stuNo}
	</select>
	
	
	<insert id="addStudent" parameterType="org.students.pojo.Student" >
		insert into student(stuno,stuname,stuage) values(#{stuNo},#{stuName},#{stuAge})
	</insert>
	
</mapper>

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/student
username=root
password=123456
maxIdle=1000
maxActive=500

然后再把他们交由spring管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 加载db.properties文件 -->
	<bean  id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>classpath:db.properties</value>
			</array>
		</property>
	</bean>
	<!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
			<property name="driverClassName" value="${driver}"></property>
			<property name="url" value="${url}"></property>
			<property name="username" value="${username}"></property>
			<property name="password" value="${password}"></property>
	</bean>
	
	<!-- conf.xml :  数据源,mapper.xml -->
	<!-- 配置MyBatis需要的核心类:SqlSessionFactory -->
	<!--SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载mapper.xml路径 -->
		<property name="mapperLocations" value="classpath:org/students/mapper/*.xml"></property>
	</bean>

	<!--MyBatisSqlSessionFactory 交给Spring   ssm整合-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	 	<property name="basePackage" value="org.students.mapper"></property>
	 	<!--上面basePackage所在的property的作用:
	 	将org.lanqiao.mapper包中,所有的接口   产生与之对应的 动态代理对象
	 	(对象名 就是 首字母小写的接口名) :studentMapper.querystudentBYNO();
	 	  -->
	 </bean>
</beans>

实体类students-pojo

从内存存到硬盘,或者需要网络传输,记得序列化就行

package org.students.pojo;

import java.io.Serializable;
//如果一个对象 需要 从内存存到硬盘,或者需要网络传输 ,则必须序列化
public class Student  implements Serializable{
	private int stuNo;
	private String stuName ;
	private int stuAge ;
	
	public Student() {
	}
	public Student(int stuNo, String stuName, int stuAge) {
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuAge = stuAge;
	}
	public int getStuNo() {
		return stuNo;
	}
	public void setStuNo(int stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	
}

students-service接口

没什么要注意的,只是接口而已,需要pojo在pom里gav坐标加就行
在这里插入图片描述

package org.students.service;

import org.students.pojo.Student;

public interface StudentService {
	void addStudent(Student student);
	Student queryStudentByStuNo(int stuno);
}

提供方students-service

简述:
在这里插入图片描述

  1. 配置
    没得看,只需要配置dubbo就行,另外一个加载dao中的spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
	<!-- 加载dao中的spring配置文件 -->
	<import resource="classpath:applicationContext-dao.xml"/>
		
		
	<!-- 配置dubbo的应用名称 -->
	<dubbo:application name="students-service" />
	<!-- 配置注册中心地址 -->
	<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.2.129:2181"  />
	
	
	<!-- 配置dubbo扫描包 -->
	<dubbo:annotation package="org.students.service.impl" />
	<context:component-scan base-package="org.students.service.impl"></context:component-scan>
	

</beans>

  1. Java
    提供方:
    Service使用阿里的包:要把服务发布出去,可供消费方dubbo访问到
    Autowired 是因为service在本地
package org.students.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.students.mapper.StudentMapper;
import org.students.pojo.Student;
import org.students.service.StudentService;

import com.alibaba.dubbo.config.annotation.Service;

@Service//alibaba
public class StudentServiceImpl implements StudentService {
	
	@Autowired 
	@Qualifier("studentMapper")
	private StudentMapper studentMapper ; 
	public void addStudent(Student student) {
		studentMapper.addStudent(student);
	}

	public Student queryStudentByStuNo(int stuNo) {
		return studentMapper.queryStudentByStuno(stuNo) ;
	}

}

消费方students-web

简述: 配置视图解析器 ModelAndView 的跳转,配置dubbo和dubbo的扫描包
实现功能:通过远程自动装配提供方的service去实现俩个方法query+add
在这里插入图片描述

  1. 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
		<!-- 将controller中的返回值 打印到浏览器中
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="false">
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
					<constructor-arg value="UTF-8"/>
				</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
 -->
		<!-- 配置视图解析器 -->
	<bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/views/"></property>
			<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 配置dubbo的应用名称 -->
	<dubbo:application name="students-web"/>
	<!-- 配置注册中心地址 -->
	<dubbo:registry address="zookeeper://192.168.2.129:2181" />
	
	
	<!-- 配置dubbo扫描包 -->
	<dubbo:annotation  package="org.students.controller"/>
	<!-- 将控制器所在包 加入IOC容器 -->
	<context:component-scan base-package="org.students.controller"></context:component-scan>
		
</beans>

  1. java文件
package org.students.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.students.pojo.Student;
import org.students.service.StudentService;

import com.alibaba.dubbo.config.annotation.Reference;
@Controller
@RequestMapping("controller")
public class StudentController {
	@Reference //阿里的包和AutoWire作用相同,不过是远程调用自动装配
	private  StudentService studentService ;
	
	@RequestMapping("queryStudentByNo")
	public ModelAndView queryStudentByNo() {
		ModelAndView mv = new ModelAndView("success");
		Student student = studentService.queryStudentByStuNo(1) ;
		mv.addObject("student",student) ;
		return mv;
	}
	
	@RequestMapping("addStudent")
	public String addStudent() {
		Student student = new Student(2,"ls",22);
		studentService.addStudent(student);
		return "success" ;
	}
}

Test

都是写死的方法,简单测试下。。。。
库已有数据
在这里插入图片描述
测试查询http://localhost:8882/controller/queryStudentByNo.action
在这里插入图片描述
测试添加http://localhost:8882/controller/addStudent.action
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值