目录
1、创建父工程
二、SpringCloud Rest环境搭建:服务提供者
三、SpringCloud Rest环境搭建:服务消费者
1.创建maven工程。
2.导入依赖
4.0.0
org.example
springcloud
1.0-SNAPSHOT
springcloud-api
springcloud-provider-user-8001
springcloud-consumer-dept-80
pom
UTF-8
1.8
1.8
4.12
1.2.17
1.16.18
org.springframework.cloud
spring-cloud-dependencies
Greenwich.SR5
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.12
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
ch.qos.logback
logback-core
1.2.3
junit
junit
${junit.version}
log4j
log4j
${log4j.version}
org.projectlombok
lombok
${lombok.version}
3.编写pojo实体类
1.导入依赖
springcloud
org.example
1.0-SNAPSHOT
4.0.0
springcloud-provider-user-8001
org.example
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
2、写配置文件application.yaml
server:
port: 8001
mybatis:
type-aliases-package: com.kun.springcloud.pojo
config-location: classpath:mybatis/mybatis.config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
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/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: root
3.编写mybatis.config配置文件
4.编写数据库操作层dao
@Mapper
@Repository
public interface DeptDao {
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
List queryUser();
}
5.编写mybatis——mapper——DeptMapper.xml
6.编写service层接口
public interface DeptService {
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
List queryUser();
}
7.编写接口实现类
package com.kun.springcloud.service;
import com.kun.springcloud.dao.DeptDao;
import com.kun.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptDao deptDao;
@Override
public User queryUserById(int id) {
return deptDao.queryUserById(id);
}
@Override
public int addUser(User user) {
return 0;
}
@Override
public int updateUser(User user) {
return 0;
}
@Override
public int deleteUser(int id) {
return 0;
}
@Override
public List queryUser() {
return null;
}
}
8.编写控制层DeptController
package com.kun.springcloud.controller;
import com.kun.springcloud.pojo.User;
import com.kun.springcloud.service.DeptService;
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.RestController;
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/dept/get/{id}")
public User query(@PathVariable("id") int id){
return deptService.queryUserById(id);
}
}
9.主启动类
package com.kun.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
1.导入依赖
springcloud
org.example
1.0-SNAPSHOT
4.0.0
springcloud-consumer-dept-80
org.example
springcloud-api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
2.编写配置类
server:
port: 80
3.编写控制类
package com.kun.springcloud.controller;
import com.kun.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class DeptConsumerController {
// 消费者不应该有service层
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://localhost:8001";
@RequestMapping("/user/dept/get/{id}")
public User get(@PathVariable("id") int id){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,User.class);
}
}
4.编写config层
package com.kun.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
5.编写主启动类
package com.kun.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
}