SpringBoot 整合Mybatis 和Dubbo

最近编写了一部分SpringBoot的代码,想整理一个基于SpringBoot 的RPC 远程服务调用框架。

第一步:整合SpringBoot和Mybatis,具体参考:https://blog.csdn.net/zhouzhiwengang/article/details/81677256

第二步:整合SpringBoot 和Dubbo.

1、pom.xml 加载dubbo 依赖的jar 文件。

 <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.7</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2、框架的层次结构如下图所示:

各模块功能分区简单说明下:

api:定义service 接口和实体对象定义。

dao:定义mapper 接口和映射文件关系绑定。

service:service 接口实现,基于mapp接口和映射文件。

web:主要业务逻辑实现层。

扫描二维码关注公众号,回复: 3115138 查看本文章

3、实现远程接口接口声明和实现。

api层:

package com.zzg.api;

import com.zzg.pojo.User;

public interface UserService {
    int deleteByPrimaryKey(Long id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

service层:

/**
 * Copyright (C), 2015-2018, 周氏集团有限公司
 * FileName: UserServiceImpl
 * Author:   Administrator
 * Date:     2018/8/16 1:58
 * Description: 用户Service
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zzg.service.impl;

import com.zzg.api.UserService;
import com.zzg.dao.UserMapper;
import com.zzg.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 〈一句话功能简述〉<br> 
 * 〈用户Service〉
 *
 * @author Administrator
 * @create 2018/8/16
 * @since 1.0.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper mapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return mapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return mapper.updateByPrimaryKey(record);
    }
}

service 服务启动:扫描dao 和加载provider.xml文件

/**
 * Copyright (C), 2015-2018, 周氏集团有限公司
 * FileName: EmsServiceApplication
 * Author:   Administrator
 * Date:     2018/8/16 1:33
 * Description: Ems 服务
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zzg.service;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

/**
 * 〈一句话功能简述〉<br> 
 * 〈Ems 服务〉
 *
 * @author Administrator
 * @create 2018/8/16
 * @since 1.0.0
 */
@SpringBootApplication
@MapperScan("com.zzg.dao")
@ImportResource("classpath:provider.xml")
public class EmsServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EmsServiceApplication.class, args);
        System.out.println("============= SpringBoot Ems Service Start Success =============");
    }
}
<?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: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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="demo-provider" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="localhost:2181" timeout="60000"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />


    <!-- 暴露dubbo服务的方式一-->
    <!-- 使用注解方式暴露接口,会自动扫描package下所有包中dubbo相关的注解,这样就不用在xml中再针对每个服务接口配置dubbo:service interface-->
    <!--<dubbo:annotation package="com.zzg.api"/>-->

    <!-- 暴露dubbo服务的方式二 -->
    <!-- 使用xml配置方式申明暴露一个接口服务,在程序启动的时候会自动注册到zookeeper。
         等同于在类上打@service注解,打了注解就必须要用annotation指定启动扫描路径,使用这种方式,就不需要指定annotation了-->
    <!--<dubbo:service interface="com.example.demo.api.service.UserService" ref="userService"/>-->
    <!-- 具体的实现bean,id与上面的ref要一致-->
    <!--<bean id="userService" class="com.example.demo.provider.api.impl.UserServiceImpl"/>-->
    <!-- 用户服务接口 -->
    <dubbo:service interface="com.zzg.api.UserService" ref="userService" />

    <bean id="userService" class="com.zzg.service.impl.UserServiceImpl"/>
</beans>

web:主要设计业务代码和远程方法调用。

/**
 * Copyright (C), 2015-2018, 周氏集团有限公司
 * FileName: UserController
 * Author:   Administrator
 * Date:     2018/8/16 2:31
 * Description: 用户控制层
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zzg.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zzg.api.UserService;
import com.zzg.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 〈一句话功能简述〉<br> 
 * 〈用户控制层〉
 *
 * @author Administrator
 * @create 2018/8/16
 * @since 1.0.0
 */
@Controller
@RequestMapping(value = "/api/user")
public class UserController {
    @Autowired
    private UserService service;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public int addUser(@RequestBody User user){
        return service.insert(user);
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ResponseBody
    public User findById(@RequestParam Long id){
        return service.selectByPrimaryKey(id);
    }
}

web服务启动:调用远程方法(Consumer.xml)

/**
 * Copyright (C), 2015-2018, 周氏集团有限公司
 * FileName: EmsWebApplication
 * Author:   Administrator
 * Date:     2018/8/16 2:35
 * Description: Ems web控制层
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zzg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ImportResource;

/**
 * 〈一句话功能简述〉<br> 
 * 〈Ems web控制层〉
 *
 * @author Administrator
 * @create 2018/8/16
 * @since 1.0.0
 */
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ImportResource("classpath:consumer.xml")
public class EmsWebApplication {
    public static void main(String[] args) throws Exception{
        SpringApplication.run(EmsWebApplication.class, args);
    }
}

码云地址:https://gitee.com/zhouzhiwengang/ems.git

项目pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zzg</groupId>
    <artifactId>ems</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <mybatis-spring-boot-starter.version>1.3.2</mybatis-spring-boot-starter.version>
        <mysql-connector-java.version>8.0.11</mysql-connector-java.version>
        <pagehelper-spring-boot-starter.version>1.2.5</pagehelper-spring-boot-starter.version>
        <com.alibaba.druid.version>1.1.0</com.alibaba.druid.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot-starter.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pagehelper-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${com.alibaba.druid.version}</version>
        </dependency>
        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.7</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <modules>
        <module>ems-api</module>
        <module>ems-service</module>
        <module>ems-web</module>
        <module>ems-dao</module>
    </modules>


</project>

猜你喜欢

转载自blog.csdn.net/zhouzhiwengang/article/details/81729368