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

(1)articleList.html 效果如下:
Springboot实验二(用配置文件的方式整合Mybatis)仅供参考!_第1张图片

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'}">

<input type="submit" value="查询">
form>
<table cellspacing="1">
<thead>
<tr>
<th>idth>
<th>标题th>
<th>内容th>
<th>操作th>
tr>
thead>

<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 对应的 Java 包路径,如 namespace="com.tyut.mapper.ArticleMapper"