前言
最近跟进一个传智健康的项目,他的技术栈使用的是ssm,注册中心是zookeeper,远程调用使用的是dubbo。技术栈比较老套,但还是想去了解一下!
核心代码
控制层
注意@Reference
注解的包,是dubbo的!这里他通过maven,把interface放到pom里了!
package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 检查项管理
*
* @author 朝花不迟暮
* @version 1.0
* @date 2020/12/16 22:19
*/
@RestController
@RequestMapping("/checkitem")
public class CheckItemController
{
@Reference
private CheckItemService checkItemService;
@RequestMapping("/add")
public Result add(@RequestBody CheckItem checkItem)
{
try
{
checkItemService.add(checkItem);
} catch (Exception e)
{
e.printStackTrace();
return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
}
return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
}
}
服务层
package com.itheima.service;
import com.itheima.pojo.CheckItem;
/**
* @author 朝花不迟暮
* @version 1.0
* @date 2020/12/16 22:24
*/
public interface CheckItemService
{
void add(CheckItem checkItem);
}
提供者
这里的提供者是接口的实现方,我这里只阐述核心代码,具体的会放到码云里,代码也很简单!
package com.itheima.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.CheckItemDao;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
/**
* 加了事务注解之后,@Service注解就需要明确实现的是哪个接口
* @author 朝花不迟暮
* @version 1.0
* @date 2020/12/16 22:39
*/
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService
{
@Autowired
private CheckItemDao checkItemDao;
@Override
public void add(CheckItem checkItem)
{
checkItemDao.add(checkItem);
}
}
这里实现类里面的@Service
注解不再是传统的那个,注意包是dubbo下面的。第二个点是如果开始事务的话,那么要明确自己实现的是哪个接口!