项目展示:
项目结构:
SQL:
CREATE TABLE `t_article` (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT '文章id',
`title` varchar(200) DEFAULT NULL COMMENT '文章标题',
`content` longtext COMMENT '文章内容',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
CREATE TABLE `t_comment` (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT '评论id',
`content` longtext COMMENT '评论内容',
`author` varchar(200) DEFAULT NULL COMMENT '评论作者',
`a_id` int(20) DEFAULT NULL COMMENT '关联的文章id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>springbootsy</groupId>
<artifactId>sy2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sy2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.datasource.url=jdbc:mysql://localhost:3306/springboottest
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=springbootsy.sy2.domain
articleList.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www. .org/">
<head>
<meta charset="UTF-8">
<title>article列表</title>
</head>
<body>
<form method="get" th:action="@{'/article/findAll'}">
<!--th:action相当于action-->
<input type="submit" value="查询">
</form>
<table cellspacing="1">
<thead>
<tr>
<th>id</th>
<th>标题</th>
<th>内容</th>
<th>操作</th>
</tr>
</thead>
<tbody th:each="article:${articleLists}">
<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>
articleDetail.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>article 详情</title>
</head>
<body>
<!--自行发挥展示article的title、content、commentList等内容-->
<table>
<tr>
<td>title:</td>
<td th:text="${article.title}"></td>
</tr>
<tr>
<td>content:</td>
<td th:text="${article.content}"></td>
</tr>
</table>
<table cellspacing="1">
<thead>
<tr>
<th>id</th>
<th>author</th>
<th>content</th>
</tr>
</thead>
<tbody th:each="comment:${article.commentList}">
<tr>
<td th:text="${comment.id}"></td>
<td th:text="${comment.author}"></td>
<td th:text="${comment.content}"></td>
</tr>
</tbody>
</table>
</body>
</html>
Article.java:
package springbootsy.sy2.domain;
import java.util.List;
public class Article {
private int id;
private String title;
private String content;
private List<Comment> commentList;
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;
}
public List<Comment> getCommentList() {
return commentList;
}
public void setCommentList(List<Comment> commentList) {
this.commentList = commentList;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", commentList=" + commentList +
'}';
}
}
Comment.java:
package springbootsy.sy2.domain;
public class Comment {
private int id;
private String content;
private String author;
private int aId;
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 getaId() {
return aId;
}
public void setaId(int aId) {
this.aId = aId;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", content='" + content + '\'' +
", author='" + author + '\'' +
", aId=" + aId +
'}';
}
}
ArticleMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--<mapper>及<mapper>中内容自行补充-->
<mapper namespace="springbootsy.sy2.mapper.ArticleMapper">
<resultMap id="articleWithComment" type="Article">
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<collection property="commentList" ofType="Comment">
<id property="id" column="cid"/>
<result property="content" column="bcontent"/>
<result property="author" column="author"/>
<result property="aId" column="a_id"/>
</collection>
</resultMap>
<select id="findById" parameterType="Integer" resultMap="articleWithComment">
select a.*,b.id as cid ,b.content as bcontent ,author,a_id
from t_article a left join t_comment b
on a.id=a_id
where a_id=#{id}
</select>
<select id="findAll" parameterType="Integer" resultMap="articleWithComment">
select *
from t_article
</select>
</mapper>
ArticleMapper.java:
package springbootsy.sy2.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import springbootsy.sy2.domain.Article;
import java.util.List;
@Repository
@Mapper
public interface ArticleMapper {
public Article findById(int id);
public List<Article> findAll();
}
MyConfig.java:
package springbootsy.sy2.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registraty){
registraty.addViewController("/article/findAll").setViewName("articleDetail");
}
}
ArticleController.java:
package springbootsy.sy2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springbootsy.sy2.domain.Article;
import springbootsy.sy2.mapper.ArticleMapper;
import java.util.List;
@Controller
@RequestMapping("/article")
public class ArticleController {
@Autowired
private ArticleMapper articleMapper;
@GetMapping("/findAll")
public String findAll(Model model){
List<Article> articleList = articleMapper.findAll();
model.addAttribute("articleLists",articleList);
return "articleList";
}
@GetMapping("/findById")
public String findById(int id,Model model){
Article article = articleMapper.findById(id);
model.addAttribute("article",article);
return "articleDetail";
}
}
Sy2Application.java:
package springbootsy.sy2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Sy2Application {
public static void main(String[] args) {
SpringApplication.run(Sy2Application.class, args);
}
}
访问网址:http://localhost:8080/article/findAll