微服務已成爲如今比較熱門的話題,而Pivotal團隊提供該框架是爲了節省時間並實現簡單開發,因它默認了整合了不少的框架(固然,各位能夠自行去摸索或者查看官方文檔:spring.io/projects/sp… 而Mybatis做爲數據層採用阿里提供的Druid數據源,分頁採用PageHelper,lomback做爲日誌輸出,Swagger做爲生成Restful的API接口文檔(缺點:有侵入代碼,不是很友好),我的感受仍是蠻好用的。java
logback、mybatis、pagehelper配置mysql
logging:
level:
com.test: ERROR
config: classpath:logback.xml
path: /項目名
spring:
application:
name: 項目名
boot:
admin:
url: http://localhost:3306
datasource:
name: test
url: jdbc:mysql://數據庫地址:3306/項目名?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=round&allowMultiQueries=true
username: root
password: 數據庫密碼
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat,wall
#最大鏈接池數量
maxActive: 20
#初始鏈接數
initialSize: 5
#獲取連接超時時間爲1分鐘,單位爲毫秒。
maxWait: 60000
#配置5,當線程池數量不足,自動補充。
minIdle: 5
#1.Destroy線程會檢測鏈接的間隔時間
#2.testWhileIdle的判斷依據
timeBetweenEvictionRunsMillis: 60000
#一個連接生存的時間(默認的值:25200000,換算後的結果公式是:300000/1000/60 = 5分鐘)
minEvictableIdleTimeMillis: 300000
#檢測數據庫連接是否有效,必須配置
validationQuery: select 'x'
#此項配置爲true便可,不影響性能,而且保證安全性。意義爲:申請鏈接的時候檢測,
#若是空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測鏈接是否有效
testWhileIdle: true
#獲取連接的時候,不校驗是否可用,開啓會有損性能
testOnBorrow: false
#歸還連接到鏈接池的時候校驗連接是否可用
testOnReturn: false
poolPreparedStatements: true
#要啓用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改成true。
#在Druid中,不會存在Oracle下PSCache佔用內存過多的問題,能夠把這個數值配置大一些,好比說100
maxOpenPreparedStatements: 20
#連接回收的時候控制檯打印信息,測試環境能夠加上true,線上環境false。會影響性能。
log-abandoned: true
maxPoolPreparedStatementPerConnectionSize: 20
http:
encoding:
charset: utf-8
enabled: true
force: true
server:
port: 8090
#錯誤日誌輸出
debug: true
複製代碼
一、logback配置web
resource文件夾下新建logback.xml文件,而後具體配置以下:spring
<configuration scan="true" scanPeriod="60 seconds">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springboot.sample" level="TRACE" />
<!-- 測試環境+開發環境. 多個使用逗號隔開. -->
<springProfile name="test,dev">
<!--<logger name="org.springframework.web" level="INFO"/>-->
<logger name="org.springboot.sample" level="DEBUGGER" />
<logger name="全包名,一直到公共包名中止,如項目基於com.test下面進行功能或者模塊分包裹,那麼這裏就到com.test" level="DEBUGGER" />
</springProfile>
<!-- 生產環境. -->
<springProfile name="prod">
<logger name="org.springboot.sample" level="ERROR" />
<logger name="全包名,一直到公共包名中止,如項目基於com.test下面進行功能或者模塊分包裹,那麼這裏就到com.test" level="ERROR" />
</springProfile>
<!--<jmxConfigurator/>-->
</configuration>
複製代碼
二、mybatis、pagehelper配置sql
com.test下新建conf文件,在文件中新建MybatisConfiguration類,具體配置以下:數據庫
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@Configuration
@MapperScan(basePackages = {"com.test.*.mapper"})
@EnableTransactionManagement(proxyTargetClass = true)
public class MybatisConfiguration {
@Autowired
WallFilter wallFilter;
@Bean
@ConfigurationProperties("spring.datasource")
@Primary
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
// filter
List<Filter> filters = new ArrayList<>();
filters.add(wallFilter);
datasource.setProxyFilters(filters);
return datasource;
}
/**
* 修改阿里sql執行過濾器的初始配置
* @param wallConfig
* @return
*/
@Bean(name = "wallFilter")
@DependsOn("wallConfig")
public WallFilter wallFilter(WallConfig wallConfig){
WallFilter wallFilter = new WallFilter();
wallFilter.setConfig(wallConfig);
return wallFilter;
}
@Bean(name = "wallConfig")
public WallConfig wallConfig(){
WallConfig wallConfig = new WallConfig();
//容許一次執行多條語句
wallConfig.setMultiStatementAllow(true);
//容許一次執行多條語句
wallConfig.setNoneBaseStatementAllow(true);
return wallConfig;
}
@Bean
@Primary
public DataSourceTransactionManager dataSourceTransactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
@Bean
@Primary
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
PagingPlugin pageHelper = new PagingPlugin();
Properties properties = new Properties();
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "count=countSql");
pageHelper.setProperties(properties);
Interceptor[] plugins = new Interceptor[]{pageHelper};
sessionFactory.setPlugins(plugins);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/**/*.xml"));
return sessionFactory.getObject();
}
}
複製代碼
Sagger配置apache
在Application同級中新建Swagger2類,具體以下:json
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//爲當前包路徑
.apis(RequestHandlerSelectors.basePackage("com.test"))
.paths(PathSelectors.any())
.build();
}
/**
* 構建 api文檔的詳細信息函數,注意這裏的註解引用的是哪一個
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//頁面標題
.title("接口文檔")
.description("RestFul 接口文檔")
//服務條款網址
// .termsOfServiceUrl("")
.contact(new Contact("zy", "", ""))
//版本號
.version("1.0")
//描述
.build();
}
}
複製代碼
springboot集成fastjsonapi
控制層返回json處理,首先在項目Application類中繼承WebMvcConfigurerAdapter,而後重寫configureMessageConverters方法安全
這樣就將springboot和fastJson集成了,能夠在控制層經過註解(方法中添加@ResponseBody或類中將@Controller更改成@RestController)返回對象,讓springboot自動處理爲json。springboot打包
在Application同級中新建TestApp,具體以下:
import org.springframework.boot.builder.SpringApplicationBuilder;
public class TestApp extends org.springframework.boot.web.support.SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(FunctionAppuserApplication.class);
}
}
複製代碼
而後就可使用maven的package進行打包;好了,根據上述能夠進行springboot的項目的暢遊!謝謝你們!