Dubbo小案例

目录

1、建立数据库

2、创建父项目 (Parent)

(1)pom.xml

3、创建pojo项目 

(1)com.example.pojo

Dept:

Emp:

4、创建mapper项目 

(1)pom.xml

(2)application.yml

5、新建api项目 

(1)pom.xml

6、新建provider项目 

(1)pom.xml

(2)application.yml 

(3)com.example

7、实现Dept查询工作 

(1)api:com.example.dubbo.service

(2)provider:com.example.dubbo.service.impl

(3)在mapper项目中补充对数据库的操作

com.example.mapper

mybatis:

(4)新建dept项目 

pom.xml

application.yml 

com.example

com.example.service

com.example.service.impl

com.example.controller 

resources /templates/dept.html  

8、实现部门列表工作、图片上传

(1)api:com.example.dubbo.service 

(2)provider:com.example.dubbo.service.impl

(3)在mapper项目中补充对数据库的操作 

com.example.mapper

mybatis 

(4)新建emp项目

pom.xml

application.yml

com.example

com.example.serever 

com.example.serever.impl 

com.example.controller  

resources /templates/empadd-add.html

(5)补充dept项目代码实现部门员工查看

com.example.service

com.example.service,impl

com.example.controller 

resources /templates/showEmp.html 

resources /templates/dept.html  

9、界面展示


1、建立数据库

create table dept(
id int(11) primary key auto_increment,
name varchar(20)
);

insert into dept values(default,'开发部');
insert into dept values(default,'产品部');

create table emp(
id int(11) primary key auto_increment,
name varchar(20),
photo varchar(200),
did int(11),
CONSTRAINT fk_emp_dept FOREIGN key (did) REFERENCES dept(id)
);

2、创建父项目 (Parent)

(1)pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.1.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.1.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
                <version>2.1.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>4.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.1</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.21</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.6</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

<dependencyManagement>用于统一子项目的依赖的版本号。 

3、创建pojo项目 

(1)com.example.pojo

Dept:

public class Dept implements Serializable {

    private Integer id;

    private String name  ;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Emp:

public class Emp implements Serializable {

    private Integer id ;

    private String name ;

    private String photo ;

    private Integer did ;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", photo='" + photo + '\'' +
                ", did=" + did +
                '}';
    }
}

4、创建mapper项目 

(1)pom.xml

   <dependencies>
        <dependency>
            <artifactId>pojo</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

(2)application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mydubbo?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mybatis/*.xml
  type-aliases-package: com.example.pojo

5、新建api项目 

(1)pom.xml

    <dependencies>
        <dependency>
            <artifactId>pojo</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

6、新建provider项目 

(1)pom.xml

   <dependencies>
        <dependency>
            <artifactId>mapper</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <artifactId>api</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
    </dependencies>

(2)application.yml 

dubbo:
  application:
    name: dubbo-provider
  registry:
    address: zookeeper://192.168.1.129:2181
#加载其它配置文件,加载其它的application-*.yml文件
#多个名称之间用逗号分隔
spring:
  profiles:
    active: mybatis

(3)com.example

@SpringBootApplication
@EnableDubbo
@MapperScan("com.example.mapper")
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}

7、实现Dept查询工作 

(1)api:com.example.dubbo.service

public interface DeptDubboService {
    public List<Dept> findAllDept();
}

(2)provider:com.example.dubbo.service.impl

@Service
public class DeptDubboServiceImpl implements DeptDubboService{
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAllDept() {
        return deptMapper.findAll();
    }
}

(3)在mapper项目中补充对数据库的操作

com.example.mapper

public interface DeptMapper {
    public List<Dept> findAll();
}

mybatis:

<mapper namespace="com.example.mapper.DeptMapper">
    <select id="findAll" resultType="com.example.pojo.Dept">
        select id , name from dept ;
    </select>
</mapper>

(4)新建dept项目 

pom.xml

  <dependencies>
        <dependency>
            <artifactId>api</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
    </dependencies>

application.yml 

dubbo:
  application:
    name: dubbo-dept-consumer
  registry:
    address: zookeeper://192.168.1.129:2181

com.example

@SpringBootApplication
@EnableDubbo
public class DeptApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeptApplication.class,args);
    }
}

com.example.service

public interface DeptService {
    public List<Dept> findAll();
}

com.example.service.impl

@Service
public class DeptServiceImpl implements DeptService {
    @Reference
    private DeptDubboService deptDubboService;
    @Override
    public List<Dept> findAll() {
        return deptDubboService.findAllDept();
    }

}

在dept项目中新建接口,在其实现类中注入api接口对象,调用api中接口方法。

通过dept中接口的实现类调用api的方法。 

com.example.controller 

@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping("/dept")
    public String showDept(Model model){
        model.addAttribute("list",deptService.findAll());
        return "dept";
    }

}

注入了deptService接口对象。

model.addAttribute:向前台传递数据,数据名为:list,数据内容为:deptService.findAll()。

resources /templates/dept.html  

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table border="1" width="500">
        <tr>
            <th>编号</th>
            <th>部门名称</th>
            <th>查看</th>
        </tr>
        <tr th:each="dept : ${list}">
            <td th:text="${dept.id}"></td>
            <td th:text="${dept.name}"></td>
            <td> <a th:href="#">查看</a> </td>
        </tr>
    </table>
</body>
</html>     

8、实现部门列表工作、图片上传

(1)api:com.example.dubbo.service 

public interface EmpDubboService {
    public int insertEmp(Emp emp);
    public List<Emp> findEmpByDeptID(Integer did);
}

(2)provider:com.example.dubbo.service.impl

@Service
public class EmpDubboServiceImpl implements EmpDubboService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public int insertEmp(Emp emp) {
        return empMapper.insertEmp(emp);
    }

    @Override
    public List<Emp> findEmpByDeptID(Integer did) {
        return empMapper.findEmpByDeptId(did);
    }
}

(3)在mapper项目中补充对数据库的操作 

com.example.mapper

public interface EmpMapper {
    public int insertEmp(Emp emp);
    public List<Emp> findEmpByDeptId(Integer did);
}

mybatis 

<mapper namespace="com.example.mapper.EmpMapper">

    <insert id="insertEmp" parameterType="com.example.pojo.Emp">
        insert into emp (name,photo,did) values (#{name},#{photo},#{did})
    </insert>

    <select id="findEmpByDeptId" parameterType="int" resultType="com.example.pojo.Emp">
        select id,name photo from emp where did = #{did}
    </select>

</mapper>

(4)新建emp项目

pom.xml

   <dependencies>
        <dependency>
            <artifactId>api</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
    </dependencies>

application.yml

dubbo:
  application:
    name: dubbo-emp-consumer
  registry:
     address: zookeeper://192.168.1.129:2181

server:
  port: 8081

com.example

@SpringBootApplication
@EnableDubbo
public class EmpApplication {
    public static void main(String[] args) {
        SpringApplication.run(EmpApplication.class , args);
    }
}

com.example.serever 

public interface EmpService {
    public List<Dept> showAll() ;
    public int insert(Emp emp, MultipartFile file);
}

com.example.serever.impl 

@Service
public class EmpServiceImpl implements EmpService {

    @Reference
    private DeptDubboService deptDubboService;

    @Reference
    private EmpDubboService empDubboService ;

    @Override
    public List<Dept> showAll() {
        return deptDubboService.findAllDept();
    }

    @Override
    public int insert(Emp emp, MultipartFile file) {
        try {
            //通过spring容器获取HttpServletRequest对象
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            //通过HttpServletRequest对象 获取图片上传的路径
            String path = request.getServletContext().getRealPath("/img");
            System.out.println("path == " + path);
            //为了上传到服务器中的图片的名称不重复 编写随机数
            long currentTimeMills = System.currentTimeMillis();
            Random random = new Random();

            String fileName = currentTimeMills + "" + random.nextInt(1000);
            String oldName = file.getOriginalFilename() ;
            //通过图片的原名称获取图片的后缀名
            fileName += oldName.substring(oldName.lastIndexOf("."));

            File pathFile = new File(path);
            //第一次上传图片 检查目录是否存在 如果不存在 创建响应目录
            if(!pathFile.exists())
            {
                pathFile.mkdirs();
            }
            //图片上传
            file.transferTo(new File(path , fileName));

            //封住emp对象 把图片路径封装到emp对象中
            emp.setPhoto("http://localhost:8081/img/"+fileName);

            //把Emp保存到数据库中
            return empDubboService.insertEmp(emp);

        } catch (IOException e) {
            e.printStackTrace();
        }


        return 0;
    }
}

com.example.controller  

@Controller
public class EmpController {

    @Autowired
    private EmpService empService ;

    @GetMapping("/empadd")
    public String empAdd(Model model)
    {
        model.addAttribute("list" , empService.showAll());

        return "emp-add";
    }

    @PostMapping("/add")
    public String add(Emp emp , MultipartFile file)
    {
        empService.insert(emp , file);
        return "emp-add";
    }
}

resources /templates/empadd-add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <form action="/add" method="post" enctype="multipart/form-data">

        姓名:<input type="text" name="name"/><br/>
        头像:<input type="file" name="file"/><br/>
        部门:
        <select name="did">
              <option th:each="dept : ${list}"  th:value="${dept.id}" th:text="${dept.name}"></option>
        </select>
        <input type="submit" value="新增"/>
    </form>
</body>
</html>

(5)补充dept项目代码实现部门员工查看

com.example.service

public interface DeptService {
    public List<Dept> findAll();
    public List<Emp> findEmpByDeptId(Integer did);
}

com.example.service,impl

@Service
public class DeptServiceImpl implements DeptService {
    @Reference
    private DeptDubboService deptDubboService;
    @Reference
    private EmpDubboService empDubboService;
    @Override
    public List<Dept> findAll() {
        return deptDubboService.findAllDept();
    }

    @Override
    public List<Emp> findEmpByDeptId(Integer did) {
        return empDubboService.findEmpByDeptID(did);
    }
}

com.example.controller 

@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping("/dept")
    public String showDept(Model model){
        model.addAttribute("list",deptService.findAll());
        return "dept";
    }
    @GetMapping("/showEmp")
    public String showEmp(Integer did,Model model){
        model.addAttribute("list",deptService.findEmpByDeptId(did));
        return "showEmp";
    }
}

resources /templates/showEmp.html 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table border="1" width="500">

        <tr>
            <th>姓名</th>
            <th>头像</th>
        </tr>

        <tr th:each="emp : ${list}">
            <td th:text="${emp.name}"></td>
            <td><img th:src="${emp.photo}" width="80"/></td>
        </tr>
    </table>
</body>
</html>

resources /templates/dept.html  

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <table border="1" width="500px">

       <tr>
           <th>编号</th>
           <th>部门名称</th>
           <th>查看</th>
       </tr>

        <tr th:each="dept : ${list}">
            <td th:text="${dept.id}"></td>
            <td th:text="${dept.name}"></td>
            <td><a th:href="@{/showEmp(did=${dept.id})}">查看</a></td>
        </tr>
    </table>
</body>
</html>

9、界面展示

重新启动项目照片会丢失。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值