IDEA2023创建springboot项目+整合mybatis、thymeleaf(JDK8、mysql5.7)

步骤一、new project

如果你的jdk版本没找到,可以替换创建项目的源为

https://start.aliyun.com/

选择Spring Web 

 步骤二、配置Maven

步骤三、运行启动类

启动成功!

输入url访问页面。

步骤四、整合mybatis和thymeleaf

pom文件如下

        <!-- Thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <!-- Druid Spring Boot Starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.6</version>
        </dependency>
        <!-- MySQL 驱动程序 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>

控制器。

@Controller
public class TestController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/test2")
    public String test2Page(Model model) {
        // 获取所有的user数据 自己实现即可
        ArrayList<User> userList = userService.findUserList();
        // 将数据集合设置到userlist属性中
        model.addAttribute("userList",userList);
        // 返回到test2.html 默认返回到是templates目录下的html文件
        return "test2";
    }

}

yml文件,仅供参考。

server:
  port: 8080
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/u139?characterEncoding=UTF-8
    username: root
    password: 123456

html代码,仅供参考。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
</head>
<body>
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Sex</th>
            <th>Age</th>
        </tr>
        </thead>
        <tbody>
        <!-- 使用th:each循环遍历userList -->
        <tr th:each="user : ${userList}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}"></td>
            <td th:text="${user.sex}"></td>
            <td th:text="${user.age}"></td>
        </tr>
        </tbody>
    </table>
</body>
</html>

数据库数据。

执行结果。

附录:可能遇到的错误

这个错误通常是由Spring框架无法找到 com.example.thymeleaf.dao.UserDao 的bean定义引起的。

解决方法:

在启动类上加上扫描。

@SpringBootApplication
@MapperScan("com.example.thymeleaf.dao")
public class ThymeleafApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThymeleafApplication.class, args);
    }

}

相关推荐

  1. idea 使用springboot helper 创建springboot项目

    2024-01-03 17:56:01       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-03 17:56:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-03 17:56:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-03 17:56:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-03 17:56:01       20 阅读

热门阅读

  1. 解决vim退格键无法使用问题

    2024-01-03 17:56:01       40 阅读
  2. uView-UI v2.x常见问题整理

    2024-01-03 17:56:01       37 阅读
  3. 啊哈c语言——逻辑挑战7:奔跑的小人

    2024-01-03 17:56:01       31 阅读
  4. vscode编写python步骤

    2024-01-03 17:56:01       36 阅读
  5. 【linux】echo命令踩坑详解

    2024-01-03 17:56:01       36 阅读
  6. Linux:磁盘分区

    2024-01-03 17:56:01       34 阅读
  7. 【算法题】30. 串联所有单词的子串

    2024-01-03 17:56:01       32 阅读
  8. H12-831_265

    2024-01-03 17:56:01       36 阅读
  9. 塔夫特原则

    2024-01-03 17:56:01       46 阅读
  10. 数字孪生项目中的导航片及寻路实现算法的探索

    2024-01-03 17:56:01       38 阅读