文章目录
业务实现
添加用户信息
1.实现添加业务的服务
1.1dubbo-Mapper
1.2 dubbo-user-interface
1.3 dubbo-user-service
2.实现添加业务的业务
2.1 dubbo-user-portal-service
2.2 dubbo-user-portal
2.3 测试
查询用户信息
1.实现查询用户服务
1.1 dubbo-mapper
1.2 dubbo-user-interface
1.3 dubbo-user-service
2.实现查询用户业务
2.1 dubbo-user-protal-service
2.2 dubbo-user-protal
删除用户信息
1.实现删除用户服务
1.1 dubbo-mapper
1.2 dubbo-user-interface
1.3 dubbo-user-service
2.实现删除用户业务
2.1 dubbo-user-protal-service
2.2 dubbo-user-protal
本文在上篇文章的基础上来实现用户信息的CRUD操作
业务实现
添加用户信息
1.实现添加业务的服务
1.1dubbo-Mapper
/** * UsersMapper接口文件 * @author dengp * */ public interface UsersMapper { void insertUsers(User user); }
/** * UsersMapper接口文件 * @author dengp * */ public interface UsersMapper { void insertUsers(User user); }
1.2 dubbo-user-interface
package com.bobo.dubbo.service; import com.bobo.pojo.User; /** * 将用户的CRUD四个操作我们拆成了4个服务,便于演示dubbo * 实际开发过程中不用 * @author dengp * */ public interface AddUserDubboService { void addUser(User user); }
1.3 dubbo-user-service
package com.bobo.dubbo.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.bobo.dubbo.service.AddUserDubboService; import com.bobo.mapper.UsersMapper; import com.bobo.pojo.User; @Service public class AddUserDubboServiceImpl implements AddUserDubboService { @Resource private UsersMapper userMapper; @Override public void addUser(User user) { // TODO Auto-generated method stub userMapper.insertUsers(user); } }
注意我们在服务端添加了服务,我们需要在Dubbo中注册该服务。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 导入Spring相关的配置文件 --> <import resource="../../applicationContext-dao.xml" /> <import resource="../../applicationContext-service.xml" /> <import resource="../../applicationContext-trans.xml" /> <dubbo:application name="user-provider" /> <dubbo:registry protocol="zookeeper" address="node01:2181,node02:2181,node03:2181"/> <dubbo:protocol name="dubbo" port="20880"/> <!-- 向dubbo中注册的服务 --> <dubbo:service interface="com.bobo.dubbo.service.AddUserDubboService" ref="addUserDubboServiceImpl"/> </beans>
服务端测试
连接成功
2.实现添加业务的业务
2.1 dubbo-user-portal-service
/** * 用户管理的业务接口 * @author dengp * */ public interface UserService { void addUser(User user); }
@Service public class UserServiceImpl implements UserService { @Resource private AddUserDubboService addUserService; @Override public void addUser(User user) { // TODO Auto-generated method stub addUserService.addUser(user); } }
2.2 dubbo-user-portal
UserController
package com.bobo.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.bobo.pojo.User; import com.bobo.service.UserService; @Controller @RequestMapping("/user") public class UserController { @Resource private UserService userService; @RequestMapping("/addUser") public String addUser(User user){ userService.addUser(user); return "success"; } }
Consumer需要指定从dubbo中获取什么服务
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="dubbo-consumer"/> <dubbo:registry protocol="zookeeper" address="node01:2181,node02:2181,node03:2181" /> <!-- 指定获取的服务 --> <dubbo:reference id="addUserDubboService" interface="com.bobo.dubbo.service.AddUserDubboService"/> </beans>
addUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="/user/addUser" method="post"> 用户名:<input type="text" name="username" /><br /> 用户年龄:<input type="text" name="userage" /><br /> <input type="submit" value="提交" /> </form> </body> </html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>dubbo .... 欢迎</h1> <h2><a href="/addUser">添加用户信息</a></h2> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>操作成功</h1> </body> </html>
PageController
@Controller public class PageController { @RequestMapping("/") public String index(){ return "index"; } @RequestMapping("/{path}") public String base(@PathVariable String path){ return path; } }
2.3 测试
报错
在访问
查询用户信息
1.实现查询用户服务
1.1 dubbo-mapper
1.2 dubbo-user-interface
1.3 dubbo-user-service
向dubbo注册服务
2.实现查询用户业务
2.1 dubbo-user-protal-service
2.2 dubbo-user-protal
userController
从dubbo中发现服务
index.jsp
user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table border="1" align="center"> <tr> <th>用户 ID</th> <th>用户姓名</th> <th>用户年龄</th> <th>操作</th> </tr> <c:forEach items="${list }" var="user"> <tr> <td>${user.id }</td> <td>${user.username }</td> <td>${user.userage }</td> <td><a href="">更新</a> <a href="">删除</a></td> </tr> </c:forEach> </table> </body> </html>
测试
成功查询出数据
删除用户信息
1.实现删除用户服务
1.1 dubbo-mapper
1.2 dubbo-user-interface
1.3 dubbo-user-service
注册业务
2.实现删除用户业务
2.1 dubbo-user-protal-service
2.2 dubbo-user-protal
UserController
dubbo中获取服务
删除按钮
测试
操作成功~