本文主要是介绍SpringBoot窥探2--整合第三方,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一:Spring Boot 使用Swagger2构建RESTful API
二:Springboot整合Mybatis
三:Spring Boot 整合 Dubbo
四:Spring Boot 整合 Elasticsearch
一:Spring Boot 使用Swagger2构建RESTful API
1.版本说明
springboot版本: <version>2.2.2.RELEASE</version>
swagger2版本: <version>2.9.2</version>
说明:在springboot整合swaggger2时,注意springboot版本和swagger2版本关系,否则启动会失败,报如下类似错误:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-26 11:35:44.287 ERROR 11728 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : ***************************
APPLICATION FAILED TO START
***************************Description:Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 15 were found:- modelBuilderPluginRegistry: defined in null- modelPropertyBuilderPluginRegistry: defined in null- typeNameProviderPluginRegistry: defined in null- documentationPluginRegistry: defined in null
2.注解说明
参考: swagger2 注解整体说明1
参考:swagger2 注解说明2
二:Springboot整合Mybatis
0.注解说明
@Mapper 标志接口为 MyBatis Mapper 接口
@Select 是 Select 操作语句
@Results 标志结果集,以及与库表字段的映射关系
@MapperScan ("指定mapper接口包路径") mapper接口扫描,配置到springboot启动类上
1.添加依赖
<!--springboot 整合mybaits-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.application.properties添加mybaits配置
## Mybatis 配置
mybatis.typeAliasesPackage=com.gm.springmvcdemo.domain
mybatis.mapperLocations=classpath:mapper/*.xml
其他相关配置详解:
mybatis.config = mybatis 配置文件名称
mybatis.mapperLocations = mapper xml 文件地址
mybatis.typeAliasesPackage = 实体类包路径
mybatis.typeHandlersPackage = type handlers 处理器包路径
mybatis.check-config-location = 检查 mybatis 配置是否存在,一般命名为 mybatis-config.xml
mybatis.executorType = 执行模式。默认是 SIMPLE
3.启动类添加注解@MapperScan
@SpringBootApplication //启动类
@MapperScan("com.gm.springmvcdemo.dao") // mapper 接口类扫描包配置
public class SpringmvcdemoApplication{public static void main(String[] args) {SpringApplication.run(SpringmvcdemoApplication.class, args);}}
4.Controller层
@RestController
@RequestMapping("/user")
public class UserController {Logger logger = LoggerFactory.getLogger(UserController.class);@Autowiredpublic UserService userService;@RequestMapping(value ="/query", method = {RequestMethod.POST})public List<User> queryUsers(@RequestParam String name){logger.info("查询用户:"+name);return userService.getUserByName(name);}}
5. Servcie 业务以及业务实现
public interface UserService {/*** @Author gaoming * @Description 根据用户名查询用户* @Date * @Param * @return java.util.List<com.gm.springmvcdemo.domain.User>**/List<User> getUserByName(String name);
}@Service
public class UserServiceImpl implements UserService{@AutowiredUserMapper userMapper;@Overridepublic List<User> getUserByName(String name) {return userMapper.getUserByName(name);}
}
.6. Dao层 mapper接口
@Mapper
public interface UserMapper {//查用户名List<User> getUserByName(String name);
}
7.工程结构
工程代码:https://github.com/gaoming-tai/springboot-learning-example/tree/master/springboot
参考:Springboot 整合 Mybatis 的完整 Web 案例
三:Spring Boot 整合 Dubbo
TODO: 待更新
四:Spring Boot 整合 Elasticsearch
TODO: 待更新
这篇关于SpringBoot窥探2--整合第三方的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!