验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

Feign+mybatisplus搭建项目遇到的坑如何解决

阅读:524 来源:乙速云 作者:代码code

Feign+mybatisplus搭建项目遇到的坑如何解决

出现的错误

1.Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource:

2.@org.springframework.beans.factory.annotation.Autowired(required=true)等

原因(简洁说明)

使用代码生成器的service层需要继承extends IService,这是一个坑,它不可以在接口工程中使用,实体类可以

如果想要使用mp的功能,就需要:

—— > 1.接口工程中创建无extends IService的接口 A

—— > 2.provider工程中放入代码生成的service层 接口B

—— > 3.provider工程的实现类需要 implements A,B

代码

接口工程

import com.study.seckill.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;

@Service
@FeignClient(value = "seckill-provider-localhost")
public interface IUserService /*extends IService*/ {

    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public List quallAll();

}

接口工程依赖


    
      org.springframework.cloud
      spring-cloud-starter-openfeign
    

    
      com.baomidou
      mybatis-plus-generate
      2.2.0
    
    
      com.baomidou
      mybatis-plus-extension
      3.1.1
    
    
      org.projectlombok
      lombok
      true
    
  

–注意不要有druid数据源依赖,可能会报sqlSession等异常–

consumer工程代码

controller

@RestController
public class UserConsumerController {

    @Autowired
    private IUserService iUserService;

    @GetMapping(value = "/users")
    public  String queryByName(){
        List users = iUserService.quallAll();
        return users.toString();
    }
    
}

启动类

@EnableFeignClients(basePackages = {"com.study.seckill"})
@SpringBootApplication
@EnableEurekaClient
public class Seckill_Consumer_8080 {
    public static void main(String[] args) {
        SpringApplication.run(Seckill_Consumer_8080.class, args);
    }
}

consumer依赖

 
    
      com.study
      seckill-api
      0.0.1-SNAPSHOT
    

    
      org.springframework.boot
      spring-boot-starter-web
    
    
    
      org.springframework.cloud
      spring-cloud-starter-ribbon
      1.4.6.RELEASE
    

    
      org.springframework.cloud
      spring-cloud-starter-eureka
      1.4.6.RELEASE
    
    
      org.springframework.cloud
      spring-cloud-starter-openfeign
    

    
      org.springframework.cloud
      spring-cloud-starter-hystrix
    
    
      org.springframework.cloud
      spring-cloud-starter-hystrix-dashboard
    
  

consumer的application.yml

server:
  port: 8080
#配置eureka
eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: seckill-consumer-8080
    prefer-ip-address: true
spring:
  application:
    name: seckill-consumer-8080

#开启降级
feign:
#  hystrix:
#    enabled: true

#feign客户端负载均衡策略
#seckill-provider-localhost:
#  ribbon:
#
##   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置规则 随机
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询
##   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置规则 重试
##   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重
##   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略
#    ConnectTimeout: 5000 #请求连接超时时间
#    ReadTimeout: 10000 #请求处理的超时时间
#    OkToRetryOnAllOperations: true #对所有请求都进行重试
#    MaxAutoRetriesNextServer: 2 #切换实例的重试次数
#    MaxAutoRetries: 2 #对当前实例的重试次数

prodvider工程

如图:UserServiceImpl 需要实现这个UserService和IUserService

Feign+mybatisplus搭建项目遇到的坑如何解决

import com.baomidou.mybatisplus.extension.service.IService;
import com.study.seckill.entity.User;
public interface UserService  extends IService {
}
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.seckill.entity.User;
import com.study.seckill.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.seckill.service.IUserService;
import com.study.seckill.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl implements IUserService, UserService {

    @Override
    public List quallAll() {
        QueryWrapper userQueryWrapper = new QueryWrapper<>();
        return baseMapper.selectList(null);
    }
}

provider的application.properties

mybatis-plus.type-aliases-package=com.study.seckill.entity
mybatis-plus.global.config.refresh-mapper=true
mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml

provider依赖


    
      com.study
      seckill-api
      0.0.1-SNAPSHOT
    


    
      org.springframework.boot
      spring-boot-starter-jdbc
    

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

    
      org.springframework.cloud
      spring-cloud-starter-eureka
    
    
    org.springframework.boot
    spring-boot-starter-actuator
    
    
      mysql
      mysql-connector-java
    

    
      com.baomidou
      mybatis-plus-boot-starter
      3.1.1
    
    
      com.baomidou
      mybatis-plus
      3.1.1
    
    
    
      com.baomidou
      mybatis-plus-generate
      2.2.0
    
    
      com.baomidou
      mybatis-plus-extension
      3.1.1
    
    
    
      org.freemarker
      freemarker
      2.3.29
    

    
    
      com.alibaba
      druid
      1.1.12
    
    
      org.projectlombok
      lombok
      true
    
    
      junit
      junit
    
    
      log4j
      log4j
    
    
      ch.qos.logback
      logback-core
    

    
      org.springframework.boot
      spring-boot-test
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
      
        
          org.junit.vintage
          junit-vintage-engine
        
      
    

  

  
    
    
      
        src/main/resources
        
        
          **/*.properties
          **/*.yml
          **/**/*.xml
        
        false
      
    
  

provider工程需要配置druid数据源,不然会报异常,这里不再赘述。

分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>