一、用maven构建
1.在Project Object Model(pom)中添加parent
org.springframework.boot spring-boot-starter-parent2.6.7
添加spring-boot-start-web和thymeleaf依赖
org.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-starter-web
2.创建启动类Application
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
3.创建controller, service, dao 等层,配置文件application.properties
application.properties:
#thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false spring.thymeleaf.content-type=text/html spring.thymeleaf.enabled=true spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.mode=HTML5 pageCache.enbale=true #log logging.level.com.imooc.miaosha=DEBUG logging.level.org.mybatis=DEBUG logging.level.com.ibatis=DEBUG logging.level.com.alibaba.druid=DEBUG
编写sampleController
@RequestMapping({"/db/get", "/demo"})
@Controller
public class SampleController {
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model){
model.addAttribute("name", "fanziqi");
return "hello~";
}
4.mybatis
依赖
org.mybatis.spring.boot mybatis-spring-boot-starter2.2.2
配置文件添加
# mybatis
mybatis.type-aliases-package=com.imooc.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/imooc/miaosha/dao
@ResponseBody
@RequestMapping({"/db/get", "/demo"})
@Controller
public class SampleController {
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model){
model.addAttribute("name", "fanziqi");
return "hello~";
}
}
6.service
public User getById(int id) {
return UserDao.getById(id);
}
7.dao
public class UserDao {
public static User getById(int id) {
return UserDao.getById(id);
}
}
8.运行结果



