栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud搭建springboot三层架构,连接数据库

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud搭建springboot三层架构,连接数据库

3spring_cloud_store-server(一个商品模块)实际需求可以根据项目来改

主要功能集成数据库,SpringCloud,等操作


3.1、pom:


    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    


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



    mysql
    mysql-connector-java
    5.1.26



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.1.1
   
    
        com.jd
        spring_cloud_base
        1.0-SNAPSHOT
    


3.2启动类

@SpringBootApplication
//客户端的Eureka
@EnableEurekaClient
//扫描包路径
@MapperScan("com.jd.mapper")
public class StoreStarter {
    public static void main(String[] args) {
        SpringApplication.run(StoreStarter.class);
    }
}

3.3三层架构

最好不要抽domain,关联会出问题。

mapper层:加通用注解

//注解,它的作用就是实现bean的注入

@Component,注意是接口

server层是接口,IXXXX是接口,不加注解

XXXimpl是

@Service
public class StoreServiceImpl implements IStoreServer(实现类)

conreoller层:加注解

@RestController 添加用的是:@PostMapping("/getinsert"),添加单个是
@GetMapping("/getfindOne/{id}")

application.yml文件配置:
eureka:
  client:
    serviceUrl: #注册地址,要和注册中心服务端配置一样
      defaultZone: http://localhost:2500/eureka/
  instance:
    prefer-ip-address: true #使用ip注册到注册中心
    instance-id: 商品服务:${server.port} #显示在注册中心的实例的id
server:
  port: 10011
spring:
  application:
    name: store-server   #服务名
  datasource:
    username: root
    password: java
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///itsourcesstore?useUnicode=true&characterEncoding=UTF-8
//配置别名
mybatis:
  type-aliases-package: com.jd.domain

xxxmapper文件:





    
    
        insert into t_store (product_id,number)
        values (#{product_id},#{number});
    

    
    
        select * from t_store where id = #{id}
    


feign配置文件:

xxxfeign文件
@FeignClient(value ="store-server",fallbackFactory = FallbackStore.class)

注意:请求名要一致调用子服务,请求名

public interface  StoreFeign {
    @GetMapping("/getfindOne/{id}")
      Store findOne(@PathVariable Long id);


//http://localhost:10011/getinsert
    @PostMapping("/getinsert")
     void insert(@RequestBody Long id);

熔断写法:Fallbackxxx

  implements FallbackFactory
 new StoreFeign() 
return Store.builder().id(-1l).number(100L).product_id(18).build();
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1002759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号