1. 程式人生 > >springboot+mybatis、JPA+swagger2完美整合

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");
	}

}

目錄結構:

部分程式碼截圖:

效果截圖:

附上原始碼 下載