SpringBoot教学资料5-SpringBoot一对多查询(带简单前端)

news/2024/12/23 4:25:59/

项目展示:

 项目结构:

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=utf8CREATE 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=.htmlspring.datasource.url=jdbc:mysql://localhost:3306/springboottest
spring.datasource.username=root
spring.datasource.password=123456mybatis.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;}@Overridepublic 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;}@Overridepublic String toString() {return "Comment{" +"id=" + id +", content='" + content + '\'' +", author='" + author + '\'' +", aId=" + aId +'}';}
}

ArticleMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//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_idfrom t_article a left join t_comment bon a.id=a_idwhere 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 {@Overridepublic 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 {@Autowiredprivate 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



http://www.ppmy.cn/news/754833.html

相关文章

【Linux修炼】开发工具使用

&#x1f307;个人主页&#xff1a;平凡的小苏 &#x1f4da;学习格言&#xff1a;命运给你一个低的起点&#xff0c;是想看你精彩的翻盘&#xff0c;而不是让你自甘堕落&#xff0c;脚下的路虽然难走&#xff0c;但我还能走&#xff0c;比起向阳而生&#xff0c;我更想尝试逆风…

简要介绍 | 元学习:学会学习的新途径

注1&#xff1a;本文系“简要介绍”系列之一&#xff0c;仅从概念上对元学习&#xff08;Meta-Learning&#xff09;进行非常简要的介绍&#xff0c;不适合用于深入和详细的了解。 元学习&#xff1a;学会学习的新途径 BLOG | Samsung Research 1 背景介绍 元学习&#xff08;…

【Django-报错处理】form.is_valid()方法报错:KeyError: ‘###‘

最初&#xff0c;报错的form表单验证部分如下&#xff1a; class ChangePwdForm(forms.Form):password1 forms.CharField(requiredTrue, min_length6)password2 forms.CharField(requiredTrue, min_length6)def clean(self):pwd1 self.cleaned_data[password1]pwd2 self.c…

世界时区对照表

时区代表城市标准时间夏令时时区ID(UTC04:30)喀布尔阿富汗标准时间阿富汗夏令时Afghanistan Standard Time(UTC-09:00)阿拉斯加阿拉斯加标准时间阿拉斯加夏令时Alaskan Standard Time(UTC-10:00)阿留申群岛阿留申群岛标准时间阿留申群岛夏令时Aleutian Standard Time(UTC07:00)…

2023第七届河南省高等学校信息安全对抗大赛-御网杯-ISCC2023线下赛-misc(详解-思路-脚本)

芜湖~ 是真累呀 原本一天的时间 硬打了一天半 还是那句话 不评价 各位道友心中自有公论 我misc手又发现一个小小的非预期 哎 没想到线下也有这种情况 欧克 以下是我自己的一些思路和解析 有什么问题或者建议随时都可以联系我 2023第七届河南省高等学校信息安全对抗大赛-御…

Jetson AGX Orin 平台12路4K相机CPHY驱动调试问题记录

1.前言 在Orin上启动CPHY相机模块时遇到了一些问题。 将4台CPHY摄像机连接到Orin 每个相机输出3VC图像 camera 1 vc0---4096x3072@30fps --- /dev/video0 vc1---4096x3072@30fps --- /dev/video1 vc2---4096x3072@30fps --- /dev/video2camera 2 vc0---4096x3072@30fps --…

鼠标上下滑轮时,来回乱跑

问题是&#xff1a; 鼠标滚轮滚动一次&#xff0c;向上或是向下。页面上下抖动后停在原位&#xff0c;或不规律的停留在某个位置&#xff0c;不受控制的感觉。 鼠标滑轮上下来回乱跳 ............... 以Win7为例&#xff1a; 控制面板-硬件和声音-鼠标-滑轮 ...... 垂直滚动 ..…