多语言展示
当前在线:792今日阅读:3今日分享:40

SpringCloud微服务案例,实现部门服务提供者

SpringCloud微服务案例,实现部门服务提供者1SpringCloud微服务案例,API公共模块和部门VO
工具/原料
1

SpringCloud

2

Intellij IDEA

方法/步骤
1

首先新建microcloudservice-provider-dept-8001子模块。项目新建完成之后,在父pom文件中就出现新建的项目模块声明。

2

修改microcloudservice-provider-dept-8001的pom.xml文件,增加相关依赖包的支持。            microservicecloud        com.gwolf.springcloud        1.0-SNAPSHOT        4.0.0    com.gwolf.springcloud    microcloudservice-provider-dept-8001    microcloudservice-provider-dept-8001    http://www.example.com            UTF-8        1.8        1.8                            com.gwolf.springcloud            microcloudservice-api            ${project.version}                                    junit            junit            4.11            test                            mysql            mysql-connector-java                            com.alibaba            druid                            org.mybatis.spring.boot            mybatis-spring-boot-starter                            ch.qos.logback            logback-core                            org.springframework.boot            spring-boot-starter-jetty                            org.springframework.boot            spring-boot-starter-web                            org.springframework.boot            spring-boot-starter-test                            org.springframework            springloaded                            org.springframework.boot            spring-boot-devtools                                                                        maven-clean-plugin                    3.0.0                                                                    maven-resources-plugin                    3.0.2                                                    maven-compiler-plugin                    3.7.0                                                    maven-surefire-plugin                    2.20.1                                                    maven-jar-plugin                    3.0.2                                                    maven-install-plugin                    2.5.2                                                    maven-deploy-plugin                    2.8.2                                       

3

在application.yml文件中配置springboot与springmvc、mybatis的整合配置。server:  port: 8001mybatis:  config-location: classpath:mybatis/mybatis.cfg.xml    # mybatis配置文件所在路径  type-aliases-package: com.gwolf.springcloud.entities       # 定义所有操作类的别名所在包  mapper-locations:                                     # 所有的mapper映射文件  - classpath:mybatis/mapper/**/*.xmlspring:  datasource:    type: com.alibaba.druid.pool.DruidDataSource    # 配置当前要使用的数据源的操作类型    driver-class-name: com.mysql.cj.jdbc.Driver      # 配置MySQL的驱动程序类    url: jdbc:mysql://localhost:3306/cloudDB01?serverTimezone=UTC   # 数据库连接地址    username: root                                  # 数据库用户名    password: root                            # 数据库连接密码    dbcp2:                                          # 进行数据库连接池的配置      min-idle: 5                                   # 数据库连接池的最小维持连接数      initial-size: 5                               # 初始化提供的连接数      max-total: 5                                  # 最大的连接数      max-wait-millis: 200                          # 等待连接获取的最大超时时间  application:    name: microcloudservice-provider-dept

4

在类路径下新建一个mybatis文件夹,用于存放mybatis配置文件和mapper文件。

5

创建部门数据库脚本和数据库的设计。DROP DATABASE IF EXISTS cloudDB01;CREATE DATABASE cloudDB01 CHARACTER SET UTF8;USE cloudDB01;DROP TABLE IF EXISTS dept;CREATE TABLE dept  (  dname varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,  db_source varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,  deptno int(11) NOT NULL AUTO_INCREMENT,  PRIMARY KEY (deptno) USING BTREE);insert into dept(dname,db_source) values('开发部',database());insert into dept(dname,db_source) values('人事部',database());insert into dept(dname,db_source) values('财务部',database());insert into dept(dname,db_source) values('市场部',database());insert into dept(dname,db_source) values('运维部',database());

6

编写部门DeptDAO的接口。package com.gwolf.springcloud.dao;import com.gwolf.springcloud.entities.Dept;import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapperpublic interface DeptDAO {    public  boolean addDept(Dept dept);    public Dept findById(Long id);    public List findAll();}

7

在工程src/main/resoures/mybatis目录下新建mappers文件夹后新建DeptMapper.xml配置文件。                                        INSERT INTO dept(dname,db_source) VALUES (#{dname},database()) ;       

8

实现DeptService部门业务类。package com.gwolf.springcloud.service.impl;import com.gwolf.springcloud.dao.DeptDAO;import com.gwolf.springcloud.entities.Dept;import com.gwolf.springcloud.service.DeptService;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;@Servicepublic class DeptServiceImpl implements DeptService {    @Resource    private DeptDAO deptDAO;        @Override    public Dept get(long id) {        return this.deptDAO.findById(id);    }    @Override    public boolean add(Dept dept) {        return this.deptDAO.addDept(dept);    }    @Override    public List list() {        return this.deptDAO.findAll();    }}

9

实现Rest风格的控制层实现。package com.gwolf.springcloud.controller;import com.gwolf.springcloud.entities.Dept;import com.gwolf.springcloud.service.DeptService;import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import java.util.List;@RestControllerpublic class DeptController {    @Resource    private DeptService deptService;        @RequestMapping(value = '/dept/get/{id}',method = RequestMethod.GET)    public Dept get(@PathVariable('id') long id) {        return this.deptService.get(id);    }    @RequestMapping(value = '/dept/add',method = RequestMethod.POST)    public boolean add(@RequestBody Dept dept) {        return this.deptService.add(dept);    }    @RequestMapping(value = '/dept/list',method = RequestMethod.GET)    public List list() {        return this.deptService.list();    }}

10

实现微服务实现主类:package com.gwolf.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Dept_8001_StartSpringCloudApplication {    public static void  main(String[] args) {        SpringApplication.run(Dept_8001_StartSpringCloudApplication.class,args);    }}

11

启动程序主类,在浏览器中访问查询所有的部门信息。

推荐信息