如何在Spring Boot中使用MyBatis?

た 入场券 2024-03-16 18:44 274阅读 0赞

标题如何在Spring Boot中使用Thymeleaf?

在Spring Boot中使用Thymeleaf需要以下几个步骤:

  1. 添加依赖

pom.xml文件中添加Thymeleaf相关的依赖,如下:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
  1. 配置模板引擎

application.properties文件中配置模板引擎的相关信息,如下:

  1. # 模板引擎类型
  2. spring.thymeleaf.mode=HTML
  3. # 需要扫描的模板文件路径
  4. spring.thymeleaf.prefix=classpath:/templates/
  5. # 模板文件后缀
  6. spring.thymeleaf.suffix=.html
  7. # 是否开启缓存
  8. spring.thymeleaf.cache=false
  1. 创建模板文件

在项目的/src/main/resources/templates目录下创建html类型的模板文件。Thymeleaf支持HTML、XML、JavaScript、Text和.raw类型的文件。

  1. 编写控制器

编写Java控制器,处理请求并将数据传递到模板文件中。示例代码如下:

  1. @Controller
  2. public class UserController {
  3. @GetMapping("/users")
  4. public String users(Model model) {
  5. List<User> users = userService.getAllUsers();
  6. model.addAttribute("users", users);
  7. return "users";
  8. }
  9. }
  1. 在模板文件中使用Thymeleaf

在模板文件中使用Thymeleaf的语法。使用Thymeleaf的语法与普通的HTML非常相似,但是Thymeleaf在模板中支持表达式、迭代器和条件语句等。示例代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>User List</title>
  6. </head>
  7. <body>
  8. <h1>User List</h1>
  9. <table>
  10. <thead>
  11. <tr>
  12. <th>ID</th>
  13. <th>Name</th>
  14. <th>Email</th>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. <!-- 使用 Thymeleaf 迭代器来遍历用户数据并生成 HTML -->
  19. <tr th:each="user : ${users}">
  20. <td th:text="${user.id}" />
  21. <td th:text="${user.name}" />
  22. <td th:text="${user.email}" />
  23. </tr>
  24. </tbody>
  25. </table>
  26. </body>
  27. </html>

至此,就完成了在Spring Boot中使用Thymeleaf的过程。总结一下,需要添加依赖、配置模板引擎、创建模板文件、编写控制器并在模板文件中使用Thymeleaf语法,以上几个步骤是实现Thymeleaf的必要流程。

在Spring Boot中使用MyBatis,需要完成以下几个步骤:

  1. 创建一个Spring Boot项目
  2. 引入MyBatis和MyBatis-Spring-Boot-Starter依赖,可以在pom.xml文件中添加以下代码:

    1. <dependency>
    2. <groupId>org.mybatis.spring.boot</groupId>
    3. <artifactId>mybatis-spring-boot-starter</artifactId>
    4. <version>2.2.0</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.mybatis</groupId>
    8. <artifactId>mybatis</artifactId>
    9. <version>3.5.5</version>
    10. </dependency>
  3. 配置数据源

    在application.properties文件中添加以下内容:

    1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    2. spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
    3. spring.datasource.username=root
    4. spring.datasource.password=root123
  4. 编写MyBatis的Mapper接口和XML文件

    创建一个Mapper接口,定义SQL语句的方法,例如:

    1. public interface UserMapper {
    2. @Select("SELECT * FROM user WHERE id = #{id}")
    3. User getUserById(@Param("id") Integer id);
    4. }

    为Mapper接口编写相应的XML文件,例如:

    1. <mapper namespace="com.example.mapper.UserMapper">
    2. <resultMap id="userMap" type="com.example.domain.User">
    3. <id column="id" property="id"/>
    4. <result column="name" property="name"/>
    5. <result column="age" property="age"/>
    6. </resultMap>
    7. <select id="getUserById" resultMap="userMap">
    8. SELECT * FROM user WHERE id = #{id}
    9. </select>
    10. </mapper>
  5. 配置MyBatis

    在application.properties文件中添加以下内容:

    1. mybatis.mapper-locations=classpath*:mapper/*.xml
    2. mybatis.type-aliases-package=com.example.domain
  6. 注入Mapper并使用

    通过@Autowired注解将Mapper注入到相应的Service或Controller中使用,例如:

    1. @RestController
    2. public class UserController {
    3. @Autowired
    4. private UserMapper userMapper;
    5. @GetMapping("/users/{id}")
    6. public User getUserById(@PathVariable Integer id) {
    7. return userMapper.getUserById(id);
    8. }
    9. }

以上就是在Spring Boot中使用MyBatis的基本流程和步骤。

发表评论

表情:
评论列表 (有 0 条评论,274人围观)

还没有评论,来说两句吧...

相关阅读