SpringBoot
SpringBoot的优点
- 嵌入的Tomcat、Jetty或者Undertow,无须部署 war 文件。
- 约定大于配置,这是SpringBoot的主导思想。
yaml 语法
配置文件:
application.yml
server:port: 8080
处理静态资源
- 在springboot,我们可以使用以下方式处理静态资源
- webjars localhost:8080/webjars/
- public, static, /**, resources localhost:8080/
- 优先级:resources > static(默认使用) > public
- 在 templates 目录下的页面,只能通过 controller 访问(需要模板引擎的支持)
thymeleaf 模板引擎
整合Druid
1、在yaml里面配置参数
spring:datasource:username: rootpassword: Falscheurl: jdbc:mysql://localhost:3306/db_ssm?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&allowPublicKeyRetrieval=truedriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource #自定义数据源#Spring Boot 默认是不注入这些属性值的,需要自己绑定#druid 数据源专有配置initialSize: 5minIdle: 5maxActive: 20maxWait: 6000timeBetweenEvictionRunsMillis: 6000minEvictableIdleTimeMillis: 3000validationQuery: SELECT 1 FROM DUALtestwgukeIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true#配置监控统计拦截的filters,stat:监控统计;log4j:日志记录;wall:防御sql注入filters: stat, wall, log4jmaxPoolPreparedStatementOerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
2、可以创建配置类,通过配置类来绑定yaml文件
因为springboot内置了tomcat,所以没有web.xml,我们可以通过配置类来配置druid的后台监控(即web.xml)以及filter过滤器。
/*** Druid的配置类* 除了配置后台监控(即web.xml)* 还可以配置filter过滤器*/@Configurationpublic class DruidConfig {@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DataSource druidDataSource() {return new DruidDataSource();}/*** 后台监控,相当于以前的web.xml* 因为SpringBoot内置了tomcat服务器,所以没有web.xml,使用ServletRegistrationBean来代替* @return*/@Beanpublic ServletRegistrationBean statViewServlet() {ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");//后台需要有人登录,账号密码配置HashMap<String, String> initParameters = new HashMap<>();initParameters.put("loginUsername", "admin"); //登录账号,key值固定initParameters.put("loginPassword", "123456"); //登录密码,key值固定initParameters.put("allow", ""); //允许谁可以访问bean.setInitParameters(initParameters); //设置初始化参数bean.setLoadOnStartup(1); //设置启动优先级return bean;}}
整合MyBatis
//@Mapper:表示本类是一个MyBatis的Mapper
yaml配置:
#用yml文件来配置mybatis配置文件mybatis:type-aliases-package: com.falsche.sringboot.pojomapper-locations: classpath:mybatis/mapper/*.xml
用yml来配置mybatis配置文件:
#用yml文件来配置mybatis配置文件mybatis:type-aliases-package: com.kuang.pojomapper-locations: classpath:mybatis/mapper/*.xml
整合pagehelper
添加依赖:
<!--pagehelper--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.5</version></dependency>
aplication.yml配置
#pagehelper配置pagehelper:helper-dialect: mysql #配置数据库,不配置也会自动检测reasonable: true #参数合理化开启,默认是falsesupport-methods-arguments: true #通过mapper来传递分页参数,默认是false
- helper-dialect:配置使用哪种数据库语言,不配置也会自动检测
- reasonable:配置分页参数合理化功能,默认是false
- support-methods-arguments:支持通过Mapper接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页
国际化
- 在 resource 目录下创建 i18n 文件
- 创建一个 login.properties 文件
- 再创建一个 login_zh_CN.properties 文件
- 再创建一个 login_en_US.properties 文件
- 对配置文件设置中/英文内容

自定义地区解析器
//自定义地区解析器public class MyLocaleResolver implements LocaleResolver {//解析请求@Overridepublic Locale resolveLocale(HttpServletRequest request) {//获取请求的语言参数String language = request.getParameter("l");Locale locale = Locale.getDefault(); //如果没有就使用默认的语言参数;//如果请求的链接携带了国际化语言if (!StringUtils.isEmpty(language)) {//zh_CNString[] split = language.split("_");//国家,地区locale = new Locale(split[0], split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {}}
杨开振 springboot
