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
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一个用于构建Java应用程序的开源框架,MyBatis是一种持久化框架,用于将Java对象映射到关系型数据库中。Thymeleaf是一种模板引擎,用于在Web应用程序中生成动态HTML。 如果你要使用Spring BootMyBatisThymeleaf构建一个Web应用程序,可以按照以下步骤进行: 1. 创建一个Spring Boot项目:可以使用Spring Initializr或者使用IDE(如Eclipse、IntelliJ IDEA创建一个新的Spring Boot项目。 2. 配置MyBatis:在pom.xml文件中添加MyBatisMyBatis-Spring的依赖,并且配置MyBatis的数据源、Mapper扫描等。 3. 配置Thymeleaf:在pom.xml文件中添加Thymeleaf的依赖,并且配置Thymeleaf的模板解析器、视图解析器等。 4. 创建Controller:创建一个或多个控制器类,用于处理HTTP请求并返回响应。在控制器中注入MyBatis的Mapper接口,以便访问数据库。 5. 创建Mapper接口:创建一个或多个Mapper接口,用于定义SQL语句和映射Java对象到数据库表。 6. 创建实体类:创建Java实体类,用于表示数据库中的表。 7. 创建Thymeleaf模板:创建HTML模板,使用Thymeleaf的语法和表达式来动态生成页面。 8. 运行应用程序:运行Spring Boot应用程序,并在浏览器中访问应用程序的URL,以验证应用程序的功能。 以上是最基本的步骤,当然还有很多其他的细节需要注意,比如异常处理、安全认证等。在实际开发中,你可以根据具体需求和业务场景来设计和实现你的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值