springboot + dubbo + mybatis + redis整合案例

第一、首先创建一个服务接口maven项目

   放一些服务的接口(UserService)与实体类(User),在本地仓库安装(install)一下接口服务

目录结构

springboot + dubbo + mybatis + redis整合案例_第1张图片

User类就是简单的实体类

springboot + dubbo + mybatis + redis整合案例_第2张图片

UserService类就是一个接口类,放一些接口方法

springboot + dubbo + mybatis + redis整合案例_第3张图片

点击maven install,安装接口服务,注意idea在工具的右侧,eclipse右键项目 run as

idea工具如下图

springboot + dubbo + mybatis + redis整合案例_第4张图片

第二、需要创建服务提供者springboot项目,

服务提供者主要作用是实现接口服务项目中的接口,进行业务逻辑的处理,对数据库进行相关操作

springboot + dubbo + mybatis + redis整合案例_第5张图片

创建好之后 需要在pox添加dubbo的依赖支持以及zookeeper客户端依赖,然后启动好zookeeper,,还需添加mybatis,redis,mysql等的起步依赖





	com.alibaba.spring.boot
	dubbo-spring-boot-starter
	2.0.0



    com.101tec
    zkclient
    0.10
       
          
             log4j
             log4j
           
           
              org.slf4j
              slf4j-log4j12
           
        
  


     com.bjpowernode.springboot
     10-springboot-all-interface
     0.0.1-SNAPSHOT



	org.mybatis.spring.boot
	mybatis-spring-boot-starter
	1.3.1



	mysql
	mysql-connector-java



	org.springframework.boot
	spring-boot-starter-data-redis

mapper类是跟mybatis的整合,用来对数据库进行操作处理的接口功能(增删改查)

springboot + dubbo + mybatis + redis整合案例_第6张图片

userMapper.xml主要是对数据库进行操作的sqlspringboot + dubbo + mybatis + redis整合案例_第7张图片实现,

UserServiceImpl是对之前install好服务接口(UserService)进行实现的实现类,主要用于对业务的处理

springboot + dubbo + mybatis + redis整合案例_第8张图片

application.properties文件是配置文件,主要是dubbo,redis,mysql的配置信息

springboot + dubbo + mybatis + redis整合案例_第9张图片

上面一些简单的业务就实现了,但是你在启动的时候需要让springboot知道要引用dubbo的注解,所以在启动类上得加上开启扫描dubbo的注解

springboot + dubbo + mybatis + redis整合案例_第10张图片

提供者部分代码已经写完了

第三、创建一个服务消费者consumer项目(springboot项目)

服务消费者只是调用服务提供者的服务,比较简单

pom文件同服务提供者,只是把数据库和redis的依赖给去掉了,因为他不需要操作数据,操作数据这些事只需要交给提供者去做就可以了

package com.bjpowernode.springboot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.springboot.model.User;
import com.bjpowernode.springboot.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @描述 :
 * @作者 :
 * @创建时间 :
 * @修改描述 :
 */

@Controller
public class UserController {

    @Reference
    private UserService userService;


    @RequestMapping("/index")
    public String index(Model model,
            @RequestParam(value = "curPage",required = false)Integer curPage){

       int pageSize = 5;

       //当前页
        if (null == curPage || curPage <1){
            curPage = 1;
        }
        //总行数
       int totalRows = userService.getUserByTotal();
        //计算分多少页
       int totalPages = totalRows / pageSize;
       //余数
       int left = totalRows % pageSize;

       if (left > 0){
           totalPages = totalPages + 1;
       }

       if (curPage>totalPages){
           curPage = totalPages;
       }

       //计算查询的开始行
       int startRow = (curPage - 1)*pageSize;
       Map paramMap = new ConcurrentHashMap<>();
       paramMap.put("startRow",startRow);
       paramMap.put("pageSize",pageSize);

       List userList=userService.getUserByPage(paramMap);

       model.addAttribute("userList",userList);
       model.addAttribute("curPage",curPage);
       model.addAttribute("totalPages",totalPages);
       return "index";

    }

    @RequestMapping("/user/toAddUser")
    public String toAddUser(){
        return "addUser";
    }

    /**
     * 添加或者修改
     * @param user
     * @return
     */
    @RequestMapping("/user/addUser")
    public String addUser( User user){

        Integer id = user.getId();
        if (null==id){
            //添加用户
            userService.addUser(user);
        }else {
            //修改用户
            userService.updateUser(user);
        }

        return "redirect:/index";
    }

    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/user/toUpdate")
    public String toUpdate(Model model,@RequestParam("id") Integer id){

        User user = userService.getUserById(id);

        model.addAttribute("user",user);

        return "addUser";
    }

    /**
     *
     * @param id
     * @return
     */
    @RequestMapping("/user/delete")
    public String delete(@RequestParam("id") Integer id){

        userService.deleteUser(id);

        return "redirect:/index";
    }



}

启动的时候需要让springboot知道要引用dubbo的注解,所以在启动类上得加上开启扫描dubbo的注解,application.properties文件中也需要配置dubbo,需要表明你的消费者名称

以上就是简单的配置了。

你可能感兴趣的:(java类案例)