记录第一次使用,帮助以后回忆
源码点击这里
上文中zookeeper+dubbo入门案例中,提供方消费方中都写了一遍的service接口,造成代码重复,这里建立父工程、jar包
parent - - - - - - - - - - - - - - - 打pom包
interface pojo dao - - - - - - - 打jar包
service web - - - - - - - - - - - 打war包
很简单,主要提供一些使用的依赖,供后续使用,如ssm的、dubbo的zookeeper的、mysql的,当然还有pojo依赖,
坑:一般配置文件放在resources中,但是mybatis习惯跟放在java放在一起,因为maven的打包机制原因,java包下只打包java文件,所以造成mapper.xml漏打,需要一个配置。
坑2:平常的mybatis是交给spring代理,而spring的启动需要web.xml启动,这里是个jar包没有web.xml,方法:把他交给提供方,提供方启动的时候顺带着把他加载了。(下面提供方有写)
其实就是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" ?>
db.properties
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/studentusername=rootpassword=123456maxIdle=1000maxActive=500
然后再把他们交由spring管理
<?xml version="1.0" encoding="UTF-8"?>
从内存存到硬盘,或者需要网络传输,记得序列化就行
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配置文件
<?xml version="1.0" encoding="UTF-8"?>
提供方:
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//alibabapublic 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
<?xml version="1.0" encoding="UTF-8"?>
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