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);
    }

}

  • 10
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值