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

发布时间:2023-03-30 15:47:19 作者:iii
来源:亿速云 阅读:106

这篇文章主要讲解了“Feign+mybatisplus搭建项目遇到的坑如何解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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<User>*/ {

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

}

接口工程依赖

<dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-generate</artifactId>
      <version>2.2.0</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-extension</artifactId>
      <version>3.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>

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

consumer工程代码

controller

@RestController
public class UserConsumerController {

    @Autowired
    private IUserService iUserService;

    @GetMapping(value = "/users")
    public  String queryByName(){
        List<User> 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依赖

 <dependencies>
    <dependency>
      <groupId>com.study</groupId>
      <artifactId>seckill-api</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-ribbon</artifactId>
      <version>1.4.6.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
      <version>1.4.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>
  </dependencies>

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<User> {
}
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<UserMapper, User> implements IUserService, UserService {

    @Override
    public List<User> quallAll() {
        QueryWrapper<User> 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依赖

<dependencies>
    <dependency>
      <groupId>com.study</groupId>
      <artifactId>seckill-api</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>


    <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.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.1</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
      <version>3.1.1</version>
    </dependency>
    <!--用来支持AR-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-generate</artifactId>
      <version>2.2.0</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-extension</artifactId>
      <version>3.1.1</version>
    </dependency>
    <!--自动生成模板-->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.29</version>
    </dependency>

    <!--连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.12</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-test</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

  </dependencies>

  <build>
    <!-- 添加资源 -->
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <!-- src/main/resources下的指定资源放行 -->
        <includes>
          <include>**/*.properties</include>
          <include>**/*.yml</include>
          <include>**/**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>
</project>

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

感谢各位的阅读,以上就是“Feign+mybatisplus搭建项目遇到的坑如何解决”的内容了,经过本文的学习后,相信大家对Feign+mybatisplus搭建项目遇到的坑如何解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. SpringCloud如何请求Feign服务调用
  2. SpringCloud Feign的使用代码实例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

feign mybatisplus

上一篇:Python怎么使用Asyncio实现检查网站状态

下一篇:如何使用JavaScript做一款无框架浏览器直接运行的数字棋牌小游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》