[TOC]
项目介绍、开发环境、环境搭建
课程技术点
第一章 项目框架搭建
SpringBoot 环境搭建
集成 Thymeleaf,Result 结果封装
Result 结果封装前后对比
public Result<String> hello() {
return Result.success("hello,imooc");
// return new Result(0, "success", "hello,imooc");
// 每次都要 new 对象,浪费
// 看起来也干净利落
}
集成 thymeleaf
return hello,SpringBoot 会自动寻找视图解析器,比如我配置的 Thymeleaf,然后根据配置文件
#在构建 URL 时添加的前缀
spring.thymeleaf.prefix=classpath:/templates/
#构建 URL 时添加的后缀
spring.thymeleaf.suffix=.html
组装成转发路径 hello.html
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model) {
model.addAttribute("name", "Xiehang");
return "hello";
}
thymeleaf 配置文件
#不缓存页面
spring.thymeleaf.cache=false
#内容为 text/html
spring.thymeleaf.content-type=text/html
#启用 MVC thymeleaf 视图解析
spring.thymeleaf.enabled=true
#模板编码
spring.thymeleaf.encoding=UTF-8
#要应用于模板的模板模式
spring.thymeleaf.mode=HTML5
#在构建 URL 时添加的前缀
spring.thymeleaf.prefix=classpath:/templates/
#构建 URL 时添加的后缀
spring.thymeleaf.suffix=.html
集成 Mybatis + Druid
集成 Mybatis
Mybatis 配置
# mybatis
mybatis.type-aliases-package=com.xiehang.miaosha.domain
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
mybatis.mapperLocations = classpath:com/xiehang/miaosha/dao/*.xml
这里不采用 xml 的方式配置 SQL 语句,而是在 dao 接口上写注解 @select、@update...
@Mapper
public interface UserDao {
@Select("select * from user where id = #{id}")
public User getById(@Param("id") int id);
}