springboot+mybatis+resteasy+swagger整合

本文详细介绍了如何一步步整合Springboot、Mybatis、Resteasy和Swagger,从单独的Springboot应用到逐步添加Mybatis、Swagger,再到Resteasy的整合,最后解决跨域问题,实现通过Swagger对API的测试。文中提供了每个阶段的配置文件、代码示例以及遇到的问题和解决方案,适合学习者参考。
摘要由CSDN通过智能技术生成

http://blog.csdn.net/dfsdffe/article/details/52101691

前言

由于在公司实习,自学了Springboot,mybatis,resteasy,swagger等内容,并做了相关整合,网上相关内容较少,故在此记录并分享一下。由于本人学习程度有限,如有错误,请多包涵并指正,谢谢。

逐步整合

1.springboot

Spring Boot提供了一个强大的一键式Spring的集成开发环境,能够单独进行一个Spring应用的开发。
springboot将tomcat等内容集成,故只需建立一个java项目,导入springboot依赖,就可以发布使用了。

项目结构如图:

这里写图片描述

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>TestSpring</groupId>
    <artifactId>TestSpring</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
        <relativePath />
    </parent>
    <dependencies>

        <!-- springboot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

启动类application:

@SpringBootApplication

@ComponentScan("controller")
public class Application {
   
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

其中@ComponentScan标识Controller所在的文件夹。

Controller:

@RestController
@RequestMapping("echo")
public class TestController {
   

    @RequestMapping("/{text}")
    public String echo(@PathVariable("text") String text){
        return "hello world," + text;
    }
}

运行application启动类,打开浏览器输入:http://localhost:8080/echo/test

显示: hello world,test 结果即为成功

2.springboot+ mybatis

MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJO映射成数据库中的记录。

项目结构如图:

这里写图片描述

在pom.xml中加入依赖:

<!-- mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.1</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

分别是mybatis整合springboot的依赖,mybatis相关,以及mysql依赖(使用其他数据库的自行更换)

applicationContext-mybatis.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值