• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Springboot实验二用配置文件的方式整合Mybatis仅供参考

武飞扬头像
白夜的月亮
帮助1

(1)articleList.html 效果如下:
学新通

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>article 列表</title>
</head>
<body>
<form method="get" th:action="@{'/articles/list'}">
<!--th:action 相当于 action-->
<input type="submit" value="查询">
</form>
<table cellspacing="1">
<thead>
<tr>
<th>id</th>
<th>标题</th>
<th>内容</th>
<th>操作</th>
</tr>
</thead>
<!--
Thymeleaf 遍历格式如下:
<tr th:each="user : ${userList}">
<td th:text="${user.name}">xxx</td>
</tr>
-->
<tbody th:each="article:${articleList}">
<tr>
<td th:text="${article.id}"></td>
<td th:text="${article.title}"></td>
<td th:text="${article.content}"></td>
<td>
<a th:href="@{/article/findById(id=${article.id})}">查看详情</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>

th:action属性: 用于设置查询表单的URL地址;
th:each属性: 指定了循环渲染的数据源和迭代变量;
th:text属性: 用于将变量的值渲染到HTML标签内部;
th:href属性: 指定了链接的URL地址,并使用${}语法将变量的值传递给后端接口。

(2**)articleDetail.html** 效果如下:


评论列表:

  • 说:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>文章详情</title>
</head>
<body>
    <h2 th:text="${article.title}"></h2>
    <p th:text="${article.content}"></p>
    <hr>
    <h3>评论列表:</h3>
    <ul th:each="comment:${article.commentList}">
        <li>
            <span th:text="${comment.username}"></span> 说:
            <span th:text="${comment.content}"></span>
        </li>
    </ul>
</body>
</html>

(3)在 application.properties 配置 thymeleaf 页面信息:

spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

(4)pom.xml 中添加数据源依赖 —>druid-spring-boot-starter

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>

(5)创建实体类 ArticleComment,注意:此时实体类所在包为 domain。

public class Article {
    private int id;
    private String title;
    private String content;

    // getter and setter methods
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Article{"  
                "id="   id  
                ", title='"   title   '\''  
                ", content='"   content   '\''  
                '}';
    }
}
public class Comment {
    private int id;
    private String content;
    private String author;
    private int a_id;

    // getter and setter methods
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public int getA_id() {
        return a_id;
    }
    public void setA_id(int a_id) {
        this.a_id = a_id;
    }

    @Override
    public String toString() {
        return "Comment{"  
                "id="   id  
                ", content='"   content   '\''  
                ", author='"   author   '\''  
                ", a_id="   a_id  
                '}';
    }
}

(6)创建 Article 对应的 Mapper 接口 ArticleMapper,注意:此时接口类所在包为 mapper

@Repository
@Mapper
public interface ArticleMapper {
public Article findById(int id);
public List<Article> findAll();
}

(7)在 resources 路径下,创建 mapper 目录,并在其中创建 ArticleMapper.xml文件,实现 ArticleMapper 接口中对应的方法。

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tyut.mapper.ArticleMapper">

    <!-- 根据 id 查询一篇文章 -->
    <select id="findById" resultType="com.tyut.domain.Article" parameterType="int">
        SELECT id, title, content FROM t_article WHERE id = #{id}
    </select>

    <!-- 查询所有文章 -->
    <select id="findAll" resultMap="articleResultMap">
        SELECT id, title, content FROM t_article
    </select>

    <!-- 定义 Article 对象的 ResultMap -->
    <resultMap id="articleResultMap" type="com.tyut.domain.Article">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
    </resultMap>

</mapper>

下面是对该 XML 文件中各个标签及其属性的解释:

  1. <mapper> 标签:定义 Mapper 文件,必须包含命名空间属性,指定该 Mapper 对应的 Java 包路径,如 namespace="com.tyut.mapper.ArticleMapper"

  2. <select> 标签:定义查询语句,可用于查询单个对象或多个对象,该标签必须指定 id 属性,指定该 SQL 语句的唯一标识符。

  • resultType 属性:指定该 SQL 语句的结果类型,如 resultType="com.tyut.domain.Article"

  • parameterType 属性:指定该 SQL 语句的参数类型,如 parameterType="int"

  • SQL 语句:SELECT 语句,用于查询数据表中的数据。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibkcba
系列文章
更多 icon
同类精品
更多 icon
继续加载