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

springboot整合mybatis,swagger ui

2017-10-19 22:57 435 查看
项目整体结构



pom.xml文件

4.0.0hongyispringboot-mybatis0.0.1-SNAPSHOTwarUTF-81.71.3.0org.springframework.bootspring-boot-starter-parent1.5.8.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-tomcatorg.mybatis.spring.bootmybatis-spring-boot-starter${mybatis-spring-boot.version}com.github.pagehelperpagehelper5.1.1mysqlmysql-connector-javaio.springfoxspringfox-swagger22.6.1io.springfoxspringfox-swagger-ui2.6.1javax.servletjavax.servlet-apiprovidedjavax.servletjstlorg.apache.tomcat.embedtomcat-embed-jasperorg.slf4jslf4j-log4j12providedorg.springframework.bootspring-boot-maven-pluginsrc/main/webappMETA-INF/resources**/**src/main/resources**/**false


创建application.properties,位置是src/java/resources下

##数据库配置信息
spring.datasource.url=jdbc:mysql://localhost:3306/easyui?userUnicode=true&characterEncoding=UTF8&useSSL=false
spring.datasource.username=root
spring.datasource.password=donghu
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#设置包的别名
mybatis.typeAliasesPackage=com.hy31.pojo
#设置mapper.xml文件的位置
mybatis.mapperLocations=classpath:mapper/*.xml
#设置springmvc的前缀
spring.mvc.view.prefix=/WEB-INF/jsp/
#后缀
spring.mvc.view.suffix=.jsp


mybatis分页插件配置

/**
* 此类属于配置类,代替xml配置
*/
@SpringBootConfiguration
public class MybatisConfig {

/**
* 分页插件配置
* @return
*/
@Bean
public PageInterceptor getPageInterceptor() {
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
//
//
properties.setProperty("offsetAsPageNum", "true");
//		 设置为true时,使用RowBounds分页会进行count查询 会去查询出总数
properties.setProperty("rowBoundsWithCount", "true");
// 分页合理化
properties.setProperty("reasonable", "true");
pageInterceptor.setProperties(properties);
return pageInterceptor;
}

// 配置mybatis的分页插件pageHelper 4.X版本 配置
/*
* @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new
* PageHelper(); Properties properties = new Properties();
* properties.setProperty("offsetAsPageNum","true");
* properties.setProperty("rowBoundsWithCount","true");
* properties.setProperty("reasonable","true");
* properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
* pageHelper.setProperties(properties); return pageHelper; }
*/

}


程序主入口

//MyBatis 扫描mapper接口
@MapperScan("com.hy31.mapper")
@SpringBootApplication
public class SBApplication extends SpringBootServletInitializer  {

/**
* 打成war发布,重写此方法
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SBApplication.class);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(SBApplication.class, args);
}

}

启动:



swagger-ui使用这里暂时不详细写,只截图:





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