接下来我们来编写一个测试接口,测试整个项目能否成功运行
首先我们在数据库的“user”表中增加一条记录,作为测试使用;

这里我们做这样一个测试,编写一个方法去通过userid,去查询用户的信息,并且将信息返回到页面
逆向工程中已经为我们提供了selectUserByPrimarykey()的方法,所以只要调用此方法就可以。
现在我们在ycshop-manager-interfaces模块创建包cn.yuechenc.ycshop.manager.interfaces;
在此包里面创建接口:

package cn.yuechenc.ycshop.manager.interfaces;import cn.yuechenc.pojo.User;public interface UserService { public User selectUserByPrimarykey(String userid);}

然后在ycshop-manager-service模块下创建包cn.yuechenc.ycshop.manager.service.impl
在此包里创建UserServiceImpl实现了,实现UserService接口

package cn.yuechenc.ycshop.manager.service.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import cn.yuechenc.manager.dao.mapper.UserMapper;import cn.yuechenc.pojo.User;import cn.yuechenc.ycshop.manager.interfaces.UserService;@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectUserByPrimarykey(String userid) { return userMapper.selectByPrimaryKey(userid); }}

此时我们的service层就算完成了,接下来,我们对修改过得项目进行install,
此时我们会发现,在对ycshop-manager进行install的时候回报一个错,如图:

此处请参考下面的解决方法:
Maven Install报错:Perhaps you are running on a JRE rather than a JDK?
解决之后,对ycshop-manager工程进行build,看到下图,表示已经成功将接口暴露到dubbo服务了

下面,我们就需要在ycshop-manager-web工程中去接收测试能否获取到接口服务

编写controller

在web模块下编写UserController

package cn.yuechenc.ycshop.manager.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import cn.yuechenc.pojo.User;import cn.yuechenc.ycshop.manager.interfaces.UserService;@Controller@RequestMapping("/user")public class UserController { @Autowired private UserService userService; @RequestMapping("/getUser") @ResponseBody private User getUser(){ return userService.selectUserByPrimarykey("1a"); }}

此处在注入service是会注入不进来,是因为之前搭建工程是没有在web工程的pom文件中加入对接口的依赖,补充:

<!-- 对 dao 的依赖 --> <dependency> <groupId>cn.yuechenc</groupId> <artifactId>ycshop-manager-dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 对接口的依赖 --> <dependency> <groupId>cn.yuechenc</groupId> <artifactId>ycshop-manager-interfaces</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>

现在保持依次[build]
ycshop-manager
和ycshop-manager-web
在浏览器输入http://localhost:8080/user/getUser进行访问,报如图错误

是因为dubbo服务之间通行时会将信息序列化之后以流的形式传输,所以就要求传输的对象是可以序列化的,此处只需让我们的pojo类实现Serializable接口即可

在此install项目并运行,并且访问http://localhost:8080/user/getUser
可以在浏览器中看到如下信息

到这里,我们所有的后台项目环境就算是搭建好了,接下来就是业务逻辑的开发
我们下节见。