您的位置:首页 > 编程语言 > Java开发

springboot+mybatis、JPA+swagger2完美集成

2017-02-17 13:48 405 查看
在网上看很多人在找 springboot 集成mybatis、jpa,springMVC集成swagger 例子什么的,看了不少别人写的,觉得有些配置过于繁琐,我依着我对这些个框架的理解,以最简单精练的方式整理了一个demo,文章最后提供了下载,还希望大家一起交流。

UserMapper 

package com.hl.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import com.hl.model.User;

public interface UserMapper {
//使用注解的方式
@Select("select * from t_user where name like concat('%',#{name},'%')")
public List<User> likeName(String name);

@Select("select * from t_user where id = #{id}")
public User getById(Long id);

//使用xml的方式
public List<User> getUsers();

}


整合swagger2 的配置类:

package com.hl.congfig;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class Swagger {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.hl.controller"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.csdn.net/catoop/article/details/50668896")
.termsOfServiceUrl("http://blog.csdn.net/catoop/article/details/50668896")
.contact("胡磊")
.version("1.0")
.build();
}

}


mybatis 分页的配置类:

package com.hl.congfig;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageHelper;

@Configuration
public class MyBatisConfiguration {

@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}

}


App类:

package com.hl;

import org.apache.log4j.Logger;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@MapperScan("com.hl.mapper")
@ComponentScan
@Configuration
public class App {

private static Logger logger = Logger.getLogger(App.class);

public static void main(String[] args) {
SpringApplication.run(App.class, args);
logger.info("SpringBoot Start Success");
}

}


目录结构:



部分代码截图:







运行App类,访问:http://localhost:8080/swagger-ui.html 即可看到RESTful
API的页面 

效果截图:







附上源码:
下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息