贵州城乡住房建设厅网站,棋牌游戏软件开发公司,两学一做考试网站,海报素材目录
1 YAML配置文件
1.1 书写规则
1.2 代码示例
1.3 用yaml进行复杂数据绑定
2 整合日志
2.1 日志配置
3 整合web
3.1 默认配置
3.2 web应用开发方式
3.2.1 全自动
3.2.2 全手动
3.2.3 手自一体(推荐)
4 整合mybatis
4.1 导包
4.2 application.yaml
4.3 dao接…目录
1 YAML配置文件
1.1 书写规则
1.2 代码示例
1.3 用yaml进行复杂数据绑定
2 整合日志
2.1 日志配置
3 整合web
3.1 默认配置
3.2 web应用开发方式
3.2.1 全自动
3.2.2 全手动
3.2.3 手自一体(推荐)
4 整合mybatis
4.1 导包
4.2 application.yaml
4.3 dao接口mapper.xml
4.4 dao接口包扫描 1 YAML配置文件 SpringBoot采用集中化配置管理,即将所有配置都写在application.properties中,但当配置增多后,会出现难阅读,层级结构模糊的问题 于是出现了application.yaml或.yml配置文件,用于替代application.properties配置文件
1.1 书写规则 (1) 区分大小写 (2) K和V之间用 : 空格分隔 (3) 缩进表示层级关系,同一层级有相同的缩进 (4) #表示注释
1.2 代码示例
server:port: 8083 #自定义 端口servlet:context-path: /sb #项目地址spring:web:resources:static-locations: classpath:/a/,classpath:/b/ #废掉4个静态访问路径,自定义a,b文件夹为新的静态访问路径mvc:static-path-pattern: /html/** #访问静态访问路径中的静态文件,需要加html前缀(index.html不用加前缀)servlet:multipart:max-file-size: 20MB #(文件上传)最大文件大小max-request-size: 30MB #请求大小限制profiles:active: dev,testdatasource: #mysql数据源driver-class-name: com.mysql.cj.jdbc.Drivertype: com.zaxxer.hikari.HikariDataSource #SpringBoot(默认)数据源,如果使用其他数据源需(dbcp2等)要额外导包url: jdbc:mysql://localhost:3306/sunnerusername: rootpassword: root--- #用三条短横线可以将下面的配置全部缩进
mybatis:mapper-locations: classpath:com/sunner/dao/*Dao.xml #映射配置文件type-aliases-package: xyz.aboluo.pojo #mybatis(包)起别名
1.3 用yaml进行复杂数据绑定
Component
ConfigurationProperties(prefix person)
public class Person {private String name;private Integer age;private Date birthday;private Boolean manFlag;private Child child; //嵌套对象private ListDog dogs; //嵌套数组private MapString,Cat cats; //嵌套map
}person:name: 王老五age: 35birthday: 1996/05/02 12:05:06 #(默认)只能用这种格式manFlag: true #对于驼峰命名的属性也可以小写用-分隔 例如man-flagchild:name: 王老六age: 8dogs:- name: 旺财age: 1like: 骨头- name: 小黑age: 3like: 鸡肉cats:cat1:name: 喵桑age: 5like: 猫粮cat2: { name: 咪咪,age: 4,like: 鱼罐头 }
2 整合日志 日志门面(接口)采用slf4j,日志实现采用Logback 这些已经被SpringBoot默认整合了,无需我们再做额外的操作 2.1 日志配置 可以在application.yaml中配置(不推荐) 或者使用logback的配置文件logback.xml(推荐),具体可以看这篇文章
3 整合web
3.1 默认配置 (1) 默认静态资源处理:静态资源放在static文件夹(或resources、public文件夹)下可以被直接访问 4个静态资源访问路径:classpath:/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ (2) 支持放在4个静态访问路径中的index.html当做首页展示 (3) 自动注册了Converter(类型转换器),Formatter(格式化)组件,例如在yaml配置文件中可以帮我们将字符串转换为Integer、对象、数组等,也可以将日期字符串(默认只能是yyyy/MM/dd ss:mm:ss:SSS的格式)格式化成Date对象 (4) 支持HttpMessageConverters,用于将返回的对象转为json,即对ResponseBody注解的支持 (5) 自动使用ConfigurableBuildingInitializer,实现数据校验、类型转换、数据绑定功能,即对RequestParameter、RequestBody等注解的支持
3.2 web应用开发方式
3.2.1 全自动 完全按照SpringBoot的默认配置
3.2.2 全手动 在Spring配置类(Configuration或SpringBootConfiguration)上添加EnableWebMvc,禁止所有默认配置
3.2.3 手自一体(推荐) 修改配置可以用yaml配置文件的方式 也可以用Spring配置类的方式: 用Spring配置类实现WebMvcConfigurer,但千万不要标EnableWebMvc,重写需要修改配置的方法
SpringBootConfiguration
EnableConfigurationProperties({Dog.class, Cat.class})
public class SpringConfig implements WebMvcConfigurer {/*** 配置静态资源的方法** param registry*/Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// 自定义配置registry.addResourceHandler(/html/**)//所有以html开头的请求都会去静态访问路径a和b下面匹配.addResourceLocations(classpath:/a/, classpath:/b/)//设置静态资源缓存规则(超过1000秒重新获取静态资源).setCacheControl(CacheControl.maxAge(1000, TimeUnit.SECONDS));}
} 还可以用将WebMvcConfigurer注册进SpringIOC容器的方式
SpringBootConfiguration
EnableConfigurationProperties({Dog.class, Cat.class})
public class SpringConfig {Beanpublic WebMvcConfigurer getWebMvcConfigurer() {return new WebMvcConfigurer() {Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// 自定义配置registry.addResourceHandler(/html/**)//所有以html开头的请求都会去静态访问路径a和b下面匹配.addResourceLocations(classpath:/a/, classpath:/b/)//设置静态资源缓存规则(超过1000秒重新获取静态资源).setCacheControl(CacheControl.maxAge(1000, TimeUnit.SECONDS));}};}
}
4 整合mybatis
4.1 导包
dependencies!--mybatis--dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion3.0.2/version/dependency!--mysql驱动--dependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdversion8.0.33/version/dependency
/dependencies mybatis-spring-boot-starter依赖了spring-boot-starter-jdbc(我们知道jdbc是用来操作数据库的),其中为我们自动配置好的: (1) org.springframework.boot.autoconfigure.jdbc.jdbcTemplateAutoConfiguration是对数据源的自动配置(默认使用HikariDataSource) (2) org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration是用来操作数据库的,但也可以选择整合mybatis (3) org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration用于支持事务(支持Transactional)
4.2 application.yaml 配置mysql数据源 配置mybatis映射配置文件,类(包)别名
4.3 dao接口mapper.xml
4.4 dao接口包扫描 在Spring配置类用MapperScan扫描到dao接口 文章转载自: http://www.morning.zdbfl.cn.gov.cn.zdbfl.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.jksgy.cn.gov.cn.jksgy.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.bchgl.cn.gov.cn.bchgl.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.smry.cn.gov.cn.smry.cn http://www.morning.rjjys.cn.gov.cn.rjjys.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.kpbq.cn.gov.cn.kpbq.cn http://www.morning.ghphp.cn.gov.cn.ghphp.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn http://www.morning.mlhfr.cn.gov.cn.mlhfr.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.rlhh.cn.gov.cn.rlhh.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.hqykb.cn.gov.cn.hqykb.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.zqwp.cn.gov.cn.zqwp.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.rmyt.cn.gov.cn.rmyt.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn