Rest学习环境搭建:服务提供者

Rest学习环境搭建:服务提供者_第1张图片

 Rest学习环境搭建:服务提供者_第2张图片

Rest学习环境搭建:服务提供者_第3张图片 

创建一个普通的Maven项目

Rest学习环境搭建:服务提供者_第4张图片 

Rest学习环境搭建:服务提供者_第5张图片 

 pom.xml父工程



    4.0.0

    com.kuang
    springcloud
    1.0-SNAPSHOT

    
        UTF-8
        8
        8
        4.12
        1.16.10
        1.2.17
    

    
    pom

    
            
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    Greenwich.SR1
                    pom
                    import
                
                
                
                    org.springframework.boot
                    spring-boot-dependencies
                    2.1.4.RELEASE
                    pom
                    import
                
            
                
                    mysql
                    mysql-connector-java
                    5.1.47
                
            
                
                    com.alibaba
                    druid
                    1.1.10
                
            
                
                    org.mybatis.spring.boot
                    mybatis-spring-boot-starter
                    1.3.2
                

                
                    ch.qos.logback
                    logback-core
                    1.2.3
                
                
                
                    junit
                    junit
                    ${junit.version}
                
                
                
                    org.projectlombok
                    lombok
                    ${lombok.version}
                
                
                
                    log4j
                    log4j
                    ${log4j.version}
                


            

    


创建数据库

Rest学习环境搭建:服务提供者_第6张图片

 

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());

select * from dept

 springcloud-api pom



    
        springcloud
        com.kuang
        1.0-SNAPSHOT
    
    4.0.0

    springcloud-api

    
        8
        8
    


    
        
            org.projectlombok
            lombok
        
    

实体类

package com.kuang.springcloud.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
@NoArgsConstructor
@Accessors(chain = true)//链式写法
@Data
//实现序列化  实体类  ORM映射  mysql--dept
public class Dept implements Serializable {

    private Long deptno;//主键
    private String dname;
    //这个数据库存在哪个数据库字段。~微服务,一个服务对应一个数据库,同一个信息可能存在不同的数据库
    private String db_source;

    public Dept(String dname) {
        this.dname = dname;
    }

    /**
     * 链式写法:
     *Dept dept = new Dept();
     * dept.setDeptNo(11).setDbane('sss').setDb_source("001");
     */



}

springcloud-provider-dept-8001 第二个子模块

POM



    
        springcloud
        com.kuang
        1.0-SNAPSHOT
    
    4.0.0

    springcloud-provider-dept-8001

    
        8
        8
    

    

        
            com.kuang
            springcloud-api
            1.0-SNAPSHOT
        


        
            junit
            junit
        


        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
        
        
        
            ch.qos.logback
            logback-core
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-test
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-jetty
        
        
        
            org.springframework.boot
            spring-boot-devtools
        


    

YML

server:
  port: 8001
#mybatis配置
mybatis:
  type-aliases-package: com.kuang.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
#  configuration: 这个配置驼峰命名转化为键值映射
#    map-underscore-to-camel-case: true

#spring配置
spring:
  application:
    name: springcloud-provider-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #数据源
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 2001

mapper

package com.kuang.springcloud.dao;

import com.kuang.springcloud.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface DeptMapper {

    int addDept(Dept dept);

    Dept queryDeptById(Long id);

    List queryAll();


}

mapper.xml






   inner join dept(dname,db_source)
    values (#{dname},DATABASE())

    




mybatis-config.xml




        
            
            
        


Service

package com.kuang.springcloud.service;

import com.kuang.springcloud.pojo.Dept;

import java.util.List;

public interface DeptService {

    int addDept(Dept dept);

    Dept queryDeptById(Long id);

    List queryAll();

}

ServiceImpl

package com.kuang.springcloud.service.impl;

import com.kuang.springcloud.dao.DeptMapper;
import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
  private DeptMapper deptMapper;

    @Override
    public int addDept(Dept dept) {
        return deptMapper.addDept(dept);
    }

    @Override
    public Dept queryDeptById(Long id) {
        return deptMapper.queryDeptById(id);
    }

    @Override
    public List queryAll() {
        return deptMapper.queryAll();
    }
}

Controller

package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供Restful服务
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/dept/add")
    public String addDept(Dept dept){
        int i = deptService.addDept(dept);
        if (i>0){
            return "添加成功";
        }
        return "添加失败";
    }

    @GetMapping("/dept/get/{id}")
    public Dept getDept(@PathVariable("id")Long id){
        Dept dept = deptService.queryDeptById(id);
        return dept;
    }
    @GetMapping("/dept/list")
    public List getAll(){
        return deptService.queryAll();
    }


}

主启动类

package com.kuang.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

 

你可能感兴趣的:(SpringCloud,学习)