这篇文章主要讲解了“Feign+mybatisplus搭建项目遇到的坑如何解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Feign+mybatisplus搭建项目遇到的坑如何解决”吧!出
这篇文章主要讲解了“Feign+mybatisplus搭建项目遇到的坑如何解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Feign+mybatisplus搭建项目遇到的坑如何解决”吧!
Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource:
@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 { @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>
–注意不要有druid数据源依赖,可能会报sqlSession等异常–
controller
@RestControllerpublic 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@EnableEurekaClientpublic 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#配置eurekaeureka: client: reGISter-with-eureka: true service-url: defaultZone: Http://localhost:7001/eureka/ instance: instance-id: seckill-consumer-8080 prefer-ip-address: truespring: 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 #对当前实例的重试次数
如图:UserServiceImpl 需要实现这个UserService和IUserService
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;@Servicepublic 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.entitymybatis-plus.global.config.refresh-mapper=truemybatis-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>***.yml</include> <include>***.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build></project>
provider工程需要配置druid数据源,不然会报异常,这里不再赘述。
感谢各位的阅读,以上就是“Feign+mybatisplus搭建项目遇到的坑如何解决”的内容了,经过本文的学习后,相信大家对Feign+mybatisplus搭建项目遇到的坑如何解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: Feign+mybatisplus搭建项目遇到的坑如何解决
本文链接: https://lsjlt.com/news/354656.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0