栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

zookeeper+dubbo+ssm入门案例

zookeeper+dubbo+ssm入门案例

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

环境
    jdk:1.8dubbo-2.6.1zookeeper-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,方法:把他交给提供方,提供方启动的时候顺带着把他加载了。(下面提供方有写)

    mapper.xml漏打处理,写在了parent父工程里,一劳永逸

		
			
				src/main/java
				
					**/*.xml
				
				false
			
			
				src/main/resources
			
		
	
    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





	
		select * from student where stuno = #{stuNo}
	
	
	
	
		insert into student(stuno,stuname,stuage) values(#{stuNo},#{stuName},#{stuAge})
	
	

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管理





	
		
			
				classpath:db.properties
			
		
	
	
	
			
			
			
			
	
	
	
	
	
	
		
		
		
	

	
	
	 	
	 	
	 	
	 

实体类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

简述:

    配置
    没得看,只需要配置dubbo就行,另外一个加载dao中的spring配置文件


	
	
	
		
		
	
	
	
	
	
	
	
	
	
	



    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

    配置


		
		
	
			
			
	

	
	
	
	
	
	
	
	
	
	
		


    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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/730965.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号