Feign+mybatisplus搭建项目遇到的坑及解决
目录
- 出现的错误
- 原因(简洁说明)
- 代码
- consumer工程代码
- prodvider工程
- 总结
出现的错误
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&gandroidt; <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
–注意不要有druid数据源依赖,可能会报sqlSession等异常–
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 开发者_Python#配置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
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 mybati编程s-plus.global.config.refresh-mapper=true mybatis-plus.mapper-locations=classpath:/mapper/**/*.XML
provider依赖
<depewww.devze.comndencies> <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<python/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数据源,不然会报异常,这里不再赘述。
总结
在搭建工程上踩了两天坑。以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
精彩评论