这个Demo的基本结构就是provider+consumer+interface

1.首先创建一个maven工程interface,接口工程的具体结构如下:


2.接着创建提供者provider


2.1   pom文件编写如下代码

<?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">


        <groupId>com.kinglong.springboot</groupId>
        <artifactId>springboot-dubbo-provider</artifactId>
        <version>1.0.0</version>


    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--springboot集成dubbo的依赖-->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!--zookeeper客户端jar依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

        <!--dubbo接口项目依赖-->
        <dependency>
            <groupId>com.kinglong.springboot</groupId>
            <artifactId>springboot-dubbo-interface</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!-- 加载mybatis整合springboot -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- MySQL的jdbc驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>
<!--使用maven生成映射文件的依赖-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/GeneratorMapper.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
       
    </build>


</project>
2.2接着创建springboot的main方法类,因为是集成dubbo框架,所以记得添加注解 @EnableDubboConfiguration

2.3 继续创建resources文件下的主配置文件,因为要采用maven生成映射文件mapper.所以同时创建一个GeneratorMapper.xml的配置文件,其生成映射文件的依赖包已经在pom文件<build>标签中声明过了.


application.properties内容如下:

# WEB服务端口
server.port=9200

# dubbo配置
spring.dubbo.appname=14-springboot-all-dubbo-provider
spring.dubbo.registry=zookeeper://192.168.174.129:2181

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.174.129:3306/springdb?useUnicode=true&characterEncoding=utf8&useSSL=false
GeneratorMapper.xml的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

    <!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径 -->
    <classPathEntry location="E:/JAVA/demo/repository/mysql/mysql-connector-java/5.1.43/mysql-connector-java-5.1.43.jar"/>

    <!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
    <context id="tables" targetRuntime="MyBatis3">

        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.174.129:3306/springdb"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <!-- 生成model类,targetPackage指定model类的包名, targetProject指定生成的model放在eclipse的哪个工程下面-->
        <javaModelGenerator targetPackage="com.kinglong.springboot.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="false" />
        </javaModelGenerator>

        <!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪个工程下面 -->
        <sqlMapGenerator targetPackage="com.kinglong.springboot.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, targetProject指定生成的Mapper接口放在eclipse的哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.kinglong.springboot.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 数据库表名及对应的Java模型类名 -->
        <table tableName="u_user"
               domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
    </context>

</generatorConfiguration>
一切准备就绪之后,就可以生成mapper映射文件了

点击生成之后如下提示:



因为model类在interface中已经创建,所以在这里创建的要删除他.在这里用不到.

因为使用的是注解开发,所以在映射文件中要添加注解@Mapper如果在此处不添加注解,就需要在main方法类Application中添加了,使用起来会繁琐一点.


2.4创建业务实现类UserServiceImpl


具体如下:

package com.kinglong.springboot.service.impl;

import com.kinglong.springboot.model.User;
import com.kinglong.springboot.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

//spring的service注解
@Service
//dubbo的service注解
@com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService{


    @Override
    public User getUser(Integer id) {
        return null;
    }

    @Override
    public int addUser(User user) {
        return 0;
    }

    @Override
    public int updateUser(User user) {
        return 0;
    }

    @Override
    public int deleteUser(Integer id) {
        return 0;
    }

    @Override
    public List<User> getUserByPage(Map<String, Object> paramMap) {
        return null;
    }

    @Override
    public int getUserByTotal() {
        return 0;
    }
}
敲黑板了!!!!重点来了!注意一下,需要在实现类上添加两个@Service注解,一个是dubbo框架的,一个是spring框架的.因为dubbo需要暴露服务,而同时spring容器需要加载管理它,所以添加两个即可,如果不添加,则同样需要在 main方法类Application中添加了,使用起来会繁琐一点.


3.创建消费者consumer


3.1检查pom文件依赖,添加所需依赖,具体如下:

<?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">


        <groupId>com.kinglong.springboot</groupId>
        <artifactId>springboot-dubbo-consumer</artifactId>
        <version>1.0.0</version>


    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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>
     </properties>
         <!--springboot起步依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--springboot集成dubbo的依赖-->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!--zookeeper客户端jar依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

        <!--dubbo接口项目依赖-->
        <dependency>
            <groupId>com.kinglong.springboot</groupId>
            <artifactId>springboot-dubbo-interface</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!--springboot集成Thymeleaf依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.unbescape</groupId>
            <artifactId>unbescape</artifactId>
            <version>1.1.5.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

又一个重点来了!由于springboot推荐使用的前端展示页面为thymeleaf,所以这里添加了thymeleaf的依赖包


3.2 依旧接着创建springboot的main方法类,因为是集成dubbo框架,所以记得添加注解@EnableDubboConfiguration


3.3继续创建resources文件中的主配置文件以及前端展示页面user.html


application.properties内容如下:

# WEB服务端口
server.port=9100

# dubbo配置
spring.dubbo.appname=14-springboot-all-dubbo-provider
spring.dubbo.registry=zookeeper://192.168.174.129:2181

#开发阶段,建议关闭thymeleaf的缓存
spring.thymeleaf.cache=false
#使用遗留的html5以去掉对html标签的校验
spring.thymeleaf.mode=LEGACYHTML5

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
user.html内容如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring boot集成 Thymeleaf</title>
</head>

<body>

<p th:each="user : ${userList}">
    <span th:text="${user.id}"></span>
    <span th:text="${user.nick}"></span>

    <span th:text="${user.phone}"></span>
    <span th:text="${user.email}"></span>
    <span th:text="${user.address}"></span>
</p>

</body>

</html>
3.4创建controller文件,内容如下:

package com.kinglong.springboot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kinglong.springboot.model.User;
import com.kinglong.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("/all/index")
    public String index (Model model,
                         @RequestParam(value="currentPage", required = false) Integer currentPage) {

        int pageSize = 10;

        if (null == currentPage) {
            currentPage = 1;
        }

        int totalRow = userService.getUserByTotal();

        int totalPage = totalRow / pageSize;
        int left = totalRow % pageSize;

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

        int startRow = (currentPage-1) * pageSize;
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("startRow", startRow);
        paramMap.put("pageSize", pageSize);

       List<User> userList = userService.getUserByPage(paramMap);
       
        model.addAttribute("userList", userList);


        return "user";
    }
}
最后,起服测试,我随意填写了个测试数据,效果如下.


总结:这是最基本的springboot集成各框架运行的Demo,熟练应运他之后相信springboot框架也会很熟练的,其实,springboot框架只是在spring框架的基础上再次做了集成,所以,如果spring框架如果用的熟练的话,相信一定会很快地掌握的.



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐