1、前言
之前玩过使用xml配置文件集成mybatis,这次为了集成thymeleaf模板,选用简单的注解完成数据库的查询。集成工具无非是引入依赖,添加配置完成此相关功能。玩过之后,记录一下学习的过程,以备后续使用。
2、依赖引入
使用springboot开发,建议装上springboot相关的插件,这样能省去很多操作。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 整合模板引擎 thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
使用springboot插件的话,只需要选择关键字“web”、“mysql”、“thmeleaf”即可,依赖会自动引入。
3、yml配置
# server
server:
port: 8080
servlet:
context-path: /book
spring:
# database
datasource:
url: jdbc:mysql://127.0.0.1:3306/test
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
# thymeleaf 模板引擎
thymeleaf:
cache: false
# mybatis
mybatis:
configuration:
map-underscore-to-camel-case: true #开启驼峰匹配
yml配置包括服务配置,数据源配置、模板引擎配置还有mybatis的配置。thymeleaf的配置大多数选择默认配置,自定义配置会覆盖默认配置,为了测试方便,关掉thymeleaf 的缓存机制。
hymeleaf的默认配置详见:
4、程序
@Controller
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/welcome")
@ResponseBody
public String welcome() {
return "welcome";
}
@GetMapping("/getBook/{bookNo}")
@ResponseBody
public Book getBook(@PathVariable("bookNo") String bookNo) {
Book book = bookService.getBookByNo(bookNo);
return book;
}
@GetMapping("/thymeleaf01")
public String thymeleaf01(Model model) {
model.addAttribute("name", "初次使用Thymeleaf");
model.addAttribute("type", "Model");
return "html/index";
}
@GetMapping("/thymeleaf02")
public String thymeleaf02(ModelMap map) {
map.put("name", "初次使用Thymeleaf");
map.put("type", "ModelMap");
return "html/index";
}
@GetMapping("/thymeleaf03")
public String thymeleaf03(HttpServletRequest request) {
request.setAttribute("name", "初次使用Thymeleaf");
request.setAttribute("type", "HttpServletRequest");
return "html/index";
}
}
controller层使用普通的注解,没有使用@RestController。因为需要跳转页面,如果使用@RestController无法跳转页面。对外提供接口话,可以使用@RestController注解。
thymeleaf页面参数的传递可以使用这几种方式:request、ModelMap、Model
thymeleaf跳转自定义页面时,配置prefix无效,只需要在return语句中加上包名即可。
5、页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>springboot-thymeleaf demo</title>
</head>
<body>
<p th:text="'hello, ' + ${name} + '!'" />
<p th:text="'这是使用'+${type}+'返回的数据'" />
</body>
</html>
thymeleaf使用html页面,但是页面可以使用类似jsp页面的语法,用来渲染数据。
6、持久层注解
@Mapper
public interface BookMapper {
@Select("select * from db_book where book_no=#{bookNo}")
Book getBookByNo(@Param("bookNo") String bookNo);
}
mybatis注解需要加上@Mapper,容器会自动扫描(与扫描包的功能类似)。SQL的方式和XML的配置相同。
7、thymeleaf 参考文档
①技术博客:https://blog.csdn.net/zrk1000/article/details/72667478
-- END