springboot+mybatis、JPA+swagger2完美集成

在网上看很多人在找 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的页面 

效果截图:

 

 

 

 

 

 

 

附上源码 下载

github地址:https://github.com/ChaselRain/mybatis

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值