SpringCloud搭建及简单的查删(服务提供者,服务调用者)

SpringCloud搭建及简单的查删

欢迎来到半夏威风!

别慌!吃口药!!!

SpringCloud是Spring旗下的项目之一,官网地址:http://projects.spring.io/spring-cloud/

Spring最擅长的就是集成,把世界上最好的框架拿过来,集成到自己的项目中。
SpringCloud也是一样,它将现在非常流行的一些技术整合到一起,实现了诸如:配置管理,服务发现,智能路由,负载均衡,熔断器,控制总线,集群状态等等功能。

这次的案例只是模拟,只提供:服务提供者和服务调用者,没有用服务注册中心,所以不支持远程。

首先我们提供服务的提供者,简单来说我们是提供数据,功能,服务的贡献者。

进行搭建SpringCloud的服务提供者

在这里插入图片描述
2.
在这里插入图片描述
3.
在这里插入图片描述
4.选择我们要用到的依赖!
在这里插入图片描述
5.
在这里插入图片描述
这样以来基本一个项目环境已经搭建好了!

然后就是我们的pom,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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.itcast.demo</groupId>
    <artifactId>user-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>user-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>

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

</project>

user-service
首先进行数据库连接配置
application.yml

server:
  port: 8082
  #jdbc
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/stu?serverTimezone=UTC&characterEncoding=utf-8
    username: root
    password: 123
    hikari:
      maximum-pool-size: 20
      minimum-idle: 10
mybatis:
    type-aliases-package: cn.itcast.userservice.pojo

在这里插入图片描述

User
package cn.itcast.userservice.pojo;


import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA
 * User: 李林威
 * Date: 2019/7/3
 * Time: 14:38
 * To change this template use File / Settings / File Templates.
 * Description:
 */
@Table(name="student")
public class User implements Serializable {
   /* private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)*/
        private Integer id;

        private String name;

        private Integer sex;

        private Integer age;

        private String classes;

        public String getClasses() {
            return classes;
        }

        public void setClasses(String classes) {
            this.classes = classes;
        }

        public Integer getSex() {
            return sex;
        }

        public void setSex(Integer sex) {
            this.sex = sex;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        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 "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                ", classes='" + classes + '\'' +
                '}';
    }
}

UserMapper
package cn.itcast.userservice.mapper;

import cn.itcast.userservice.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;


public interface UserMapper{
    @Select("select * from student where id = #{id}")
     User queryById(int id);

    @Delete("delete from student where id = #{id}")
    int deleteId(int id);
}

UserService
package cn.itcast.userservice.service;


import cn.itcast.userservice.pojo.User;

public interface UserService {
    User queryById(int id);

    int deleteId( int id);
}

UserServiceimpl
package cn.itcast.userservice.service.impl;

import cn.itcast.userservice.mapper.UserMapper;
import cn.itcast.userservice.pojo.User;


import cn.itcast.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceimpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public User queryById(int id) {
        return this.userMapper.queryById(id);
    }

    @Override
    public int deleteId(int id) {
        return this.userMapper.deleteId(id);
    }
}
UserController
package cn.itcast.userservice.controller;

import cn.itcast.userservice.pojo.User;
import cn.itcast.userservice.service.impl.UserServiceimpl;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA
 * User: 李林威
 * Date: 2019/7/11
 * Time: 21:22
 * To change this template use File / Settings / File Templates.
 * Description:
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserServiceimpl userService;

    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") int id) {
        return this.userService.queryById(id);
    }
    @GetMapping(value = "delete/{id}")
    public int delete(@PathVariable("id") int id) {
       return this.userService.deleteId(id);

    }
}

UserServiceApplication
package cn.itcast.userservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("cn.itcast.userservice.mapper")
public class UserServiceApplication {

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

}

注意访问路径,要结合自己的端口和字段!
在这里插入图片描述
这样以来我们就完成了服务的提供者!
临时有事!过段时间更新服务调用者!
欢迎访问项目空间案例成型空间:
在这里插入图片描述
拜拜,别慌吃口药!我还能秀,我还能跑!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值