您好,登錄后才能下訂單哦!
這篇文章主要講解了“Feign+mybatisplus搭建項目遇到的坑如何解決”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“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.接口工程中創(chuàng)建無extends IService的接口 A
—— > 2.provider工程中放入代碼生成的service層 接口B
—— > 3.provider工程的實現(xiàn)類需要 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>
–注意不要有druid數(shù)據(jù)源依賴,可能會報sqlSession等異常–
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客戶端負(fù)載均衡策略 #seckill-provider-localhost: # ribbon: # ## NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置規(guī)則 隨機(jī) # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置規(guī)則 輪詢 ## NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置規(guī)則 重試 ## NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置規(guī)則 響應(yīng)時間權(quán)重 ## NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置規(guī)則 最空閑連接策略 # ConnectTimeout: 5000 #請求連接超時時間 # ReadTimeout: 10000 #請求處理的超時時間 # OkToRetryOnAllOperations: true #對所有請求都進(jìn)行重試 # MaxAutoRetriesNextServer: 2 #切換實例的重試次數(shù) # MaxAutoRetries: 2 #對當(dāng)前實例的重試次數(shù)
如圖:UserServiceImpl 需要實現(xiàn)這個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 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數(shù)據(jù)源,不然會報異常,這里不再贅述。
感謝各位的閱讀,以上就是“Feign+mybatisplus搭建項目遇到的坑如何解決”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Feign+mybatisplus搭建項目遇到的坑如何解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。