SpringBoot配置文件
properties配置文件
- 语法:key-value结构 key=value
- 数据类型:默认是String
- 字符数据类型:properties默认的加载编码的格式为ISO-8859-1
- 缺点:所有的key都必须手动编辑
yml配置文件
- 语法:key-value结构 写法上key:value
- 加载编码的格式为UTF-8
spring:
thymeleaf:
prefix: templates/
suffix: .html
为属性赋值
@RestController
//通过Spring容器加载配置文件
@PropertySource(value = "classpath:/properties/redis.properties",encoding = "utf-8")
public class RedisController {
@Value("${redis.host}")
private String Host;
@Value("${redis.port}")
private String Port;
@RequestMapping("getMesg")
public String getMesg(){
return Host+":"+Port;
}
}
环境切换
spring:
profiles:
active: pro
#环境分割线
---
spring:
profiles: pro
server:
port: 8090
---
spring:
profiles: dev
server:
port: 8080
热部署配置
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
配置IDEA:Ctrl+alt+shift+/ 进入Registry页面
SpringMVC处理流程
四大组件
- 前端控制器(DispatcherServlet)
- 映射适配器(HandlerMapping)
- 控制器适配器(HandlerAdaptor)
- 视图解析器(ViewReslover)
@RequestController
@Controller+@ResponseBody
将返回值转换为JSON串使用,程序将不会执行视图解析器
@Controller
返回类型为String或modleAndView
SpringBoot整合Mybatis
链式加载
pojo类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)//开启链式加载 重构set方法可以连续
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
private String sex;
/*链式加载源码
public User setName(String name){ this.name = name; return this; } */}
User user = new User();
user.setAge(10).setName("dsd").setSex("男");
配置文件
server:
port: 8090
servlet:
context-path: /
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: 123456
mybatis:
#别名包定义 mapper的resultType中只需要写类名
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
MybatisPlus
添加依赖
<!--spring整合mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
配置文件
server:
port: 8090
servlet:
context-path: /
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: 123456
mybatis-plus:
#别名包定义 mapper的resultType中只需要写类名
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
#SQL日志打印
logging:
level:
com.jt.mapper: debug
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。