Dubbo实战案例02【业务实现】
【摘要】
文章目录
业务实现添加用户信息1.实现添加业务的服务1.1dubbo-Mapper1.2 dubbo-user-interface1.3 dubbo-user-service
2.实现添加...
业务实现
添加用户信息
1.实现添加业务的服务
1.1dubbo-Mapper
/**
* UsersMapper接口文件
* @author dengp
*
*/
public interface UsersMapper {
void insertUsers(User user);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
<?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" >
<mapper namespace="com.bobo.mapper.UsersMapper">
<insert id="insertUsers" parameterType="com.bobo.pojo.User">
insert into users(username,userage)values(#{username},#{userage})
</insert>
</mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
注意我们在服务端添加了服务,我们需要在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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
服务端测试
连接成功
2.实现添加业务的业务
2.1 dubbo-user-portal-service
/**
* 用户管理的业务接口
* @author dengp
*
*/
public interface UserService {
void addUser(User user);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
@Service
public class UserServiceImpl implements UserService {
@Resource
private AddUserDubboService addUserService;
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
addUserService.addUser(user);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
PageController
@Controller
public class PageController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/{path}")
public String base(@PathVariable String path){
return path;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
测试
成功查询出数据
删除用户信息
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中获取服务
删除按钮
测试
操作成功~
文章来源: dpb-bobokaoya-sm.blog.csdn.net,作者:波波烤鸭,版权归原作者所有,如需转载,请联系作者。
原文链接:dpb-bobokaoya-sm.blog.csdn.net/article/details/88873245
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)