美文网首页
《二》、springcloud微服务案例—— 部门微服务提供者P

《二》、springcloud微服务案例—— 部门微服务提供者P

作者: 神奇作手 | 来源:发表于2019-07-18 14:42 被阅读0次

    一、构建 microservicecloud-provider-dept-8001 部门微服务提供者 Module

    (1)、新建microservicecloud-provider-dept-8001模块
    (2)、pom.xml文件

        
    <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>
        <parent>
            <groupId>cn.smilexl.springcloud</groupId>
            <artifactId>microservicecloud</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>microservicecloud-provider-dept-8001</artifactId>
    
        <dependencies>
            <!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
            <dependency>
                <groupId>cn.smilexl.springcloud</groupId>
                <artifactId>microservicecloud-api</artifactId>
                <version>${project.version}</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
            </dependency>
            <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>
            </dependency>
        </dependencies>
    </project>
    
    

    (3)、application.yml文件

    server:
      port: 8001
      
    mybatis:
    #  config-location: classpath:mybatis/mybatis.cfg.xml       # mybatis配置文件所在路径
      type-aliases-package: cn.smilexl.springcloud.entities     # 所有Entity别名类所在包
      mapper-locations: classpath:mapper/**/*.xml               # mapper映射文件
        
    spring:
       application:
        name: microservicecloud-dept                           # 微服务名
       datasource:
        type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
        driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
        url: jdbc:mysql://47.99.218.123:3306/clouddb01          # 数据库名称
        username: root
        password: 123456
        dbcp2:
          min-idle: 5                                           # 数据库连接池的最小维持连接数
          initial-size: 5                                       # 初始化连接数
          max-total: 5                                          # 最大连接数
          max-wait-millis: 200                                  # 等待连接获取的最大超时时间
          
    

    (4)、Mysql创建部门数据库脚本

    DROP DATABASE IF EXISTS cloudDB01;
    CREATE DATABASE cloudDB01 CHARACTER SET UTF8;
    USE cloudDB01;
    CREATE TABLE `dept` (
      `deptno` bigint(20) NOT NULL AUTO_INCREMENT,
      `dname` varchar(60) DEFAULT NULL,
      `db_source` varchar(60) DEFAULT NULL,
      PRIMARY KEY (`deptno`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
    
    INSERT INTO `dept` VALUES ('1', '开发部', 'clouddb01');
    INSERT INTO `dept` VALUES ('2', '人事部', 'clouddb01');
    INSERT INTO `dept` VALUES ('3', '财务部', 'clouddb01');
    INSERT INTO `dept` VALUES ('4', '市场部', 'clouddb01');
    INSERT INTO `dept` VALUES ('5', '运维部', 'clouddb01');
    INSERT INTO `dept` VALUES ('6', '资讯部', 'clouddb01');
    
    

    (5)、DeptDao.java 部门接口

    package cn.smilexl.springcloud.dao;
    
    import java.util.List;
    import org.apache.ibatis.annotations.Mapper;
    import cn.smilexl.springcloud.entities.Dept;
    
    @Mapper
    public interface DeptDao {  
            //添加
            public boolean addDept(Dept dept);
            //根据id查询
            public Dept findById(Long id);
            //查询全部
            public List<Dept> findAll();
    
    }
    

    (6)、工程src/main/resources/mybatis目录下新建mapper文件夹后在建DeptMapper.xml文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     
    <mapper namespace="cn.smilexl.springcloud.dao.DeptDao">
     
      <select id="findById" resultType="Dept" parameterType="Long">
        select deptno,dname,db_source from dept where deptno=#{deptno}; 
      </select>
      <select id="findAll" resultType="Dept">
        select deptno,dname,db_source from dept; 
      </select>
      <insert id="addDept" parameterType="Dept">
        INSERT INTO dept(dname,db_source) VALUES(#{dname},DATABASE());
      </insert>
      
    </mapper>
    
    

    (7)、DeptService.java 部门服务接口

    package cn.smilexl.springcloud.service;
    import java.util.List;
    import cn.smilexl.springcloud.entities.Dept;
    public interface DeptService {  
        //添加
        public boolean addDept(Dept dept);
        //根据id查询
        public Dept findById(Long id);  
        //查询全部
        public List<Dept> findAll();
    }
    
    

    (8)、DeptServiceImpl.java 部门服务接实现类

    package cn.smilexl.springcloud.service.impl;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import cn.smilexl.springcloud.dao.DeptDao;
    import cn.smilexl.springcloud.entities.Dept;
    import cn.smilexl.springcloud.service.DeptService;
    
    @Service
    public class DeptServiceImpl implements DeptService{
        
        @Autowired
        private DeptDao deptDao;
    
        @Override
        public boolean addDept(Dept dept) {
            
            return deptDao.addDept(dept);
        }
    
        @Override
        public Dept findById(Long id) {
            
            return deptDao.findById(id);
        }
    
        @Override
        public List<Dept> findAll() {
            
            return deptDao.findAll();
        }
    
    }
    
    

    (9)、DeptServiceImpl.java 部门微服务提供者REST

    package cn.smilexl.springcloud.controller;
    
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    import cn.smilexl.springcloud.entities.Dept;
    import cn.smilexl.springcloud.service.DeptService;
    
    @RestController
    public class DeptController {
        
        @Autowired
        private DeptService deptService;
        
        // 添加
        @PostMapping("/dept/add")
        public Boolean addDept(Dept dept) {
            
            return deptService.addDept(dept);
        }
    
        // 根据id查询
        @GetMapping("/dept/get/{id}")
        public Dept findById(@PathVariable("id") Long id) {
            return deptService.findById(id);
        }
    
        // 查询全部
        @GetMapping("/dept/list")
        public List<Dept> findAll() {
            return deptService.findAll();
        }
    }
    
    

    (10)、DeptProvider8001_App.java 主启动类

    package cn.smilexl.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DeptProvider8001_App {
    
        public static void main(String[] args) {
            
            SpringApplication.run(DeptProvider8001_App.class, args);
            
        }
    }
    
    

    (11)、启动测试*
    (12)、最终工程展现

    相关文章

      网友评论

          本文标题:《二》、springcloud微服务案例—— 部门微服务提供者P

          本文链接:https://www.haomeiwen.com/subject/qsdllctx.html