springboot介绍
springboot简化传统ssm框架中繁杂的配置文件,取而代之的是实体bean以及注解,他还需要一个配置文件application.properties或者.yml
springboot中的注解
1.启动类
//核心注解,通过启动main方法可以直接启动项目,用内置的tomcat
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true)
//扫包
@MapperScan("net.wanho.mapper")
public class SsmDemoApplication {
@Bean
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
public static void main(String[] args) {
SpringApplication.run(SsmDemoApplication.class, args);
}
}
2.配置文件转化成bean
@Configuration
@Bean
@EnableWebMvc
@EnableWebMvc是使用Java 注解快捷配置Spring Webmvc的一个注解。在使用该注解后配置一个继承于WebMvcConfigurerAdapter的配置类即可配置好Spring Webmvc。
通过查看@EnableWebMvc的源码,可以发现该注解就是为了引入一个DelegatingWebMvcConfiguration Java 配置类。并翻看DelegatingWebMvcConfiguration的源码会发现该类似继承于WebMvcConfigurationSupport的类。
其实不使用@EnableWebMvc注解也是可以实现配置Webmvc,只需要将配置类继承于WebMvcConfigurationSupport类即可
package net.wanho.config;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
//只要在类上出现@Configuration就说明他是配置
@Configuration
@EnableWebMvc
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public FilterRegistrationBean siteMeshFilter(){
FilterRegistrationBean filter = new FilterRegistrationBean();
Sitemesh3Filter siteMeshFilter = new Sitemesh3Filter();
filter.setFilter(siteMeshFilter);
return filter;
}
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
application.yml
//端口号
server :
port : 8088
spring :
//数据库连接池信息
datasource:
driver-class-name : com.mysql.jdbc.Driver
url : jdbc:mysql://localhost:3306/test?characterEncoding=utf8
username : root
password : root
// redis缓存数据库的ip+端口
redis:
database: 0
host: 172.16.219.166
port: 6379
password:
// redis连接池
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 0
//分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
#mybatis:
#config-location : classpath:net/wanho/mapper/*.xml
//配置mapper的扫描路径
mybatis:
mapper-locations: classpath:net/wanho/mapper/*.xml