首页 > 其他分享 >SpringBoot教学资料5-SpringBoot一对多查询(带简单前端)

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

时间:2023-07-04 09:55:20浏览次数:30  
标签:SpringBoot springframework public content 教学资料 org import 查询 id

项目展示:

 

 项目结构:

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

 


标签:SpringBoot,springframework,public,content,教学资料,org,import,查询,id
From: https://www.cnblogs.com/lysboke/p/17524858.html

相关文章

  • SpringBoot教学资料4-SpringBoot简单增删改查(带前端)
    最终样式:增: 删:  改:  项目结构:     - springboot1.5.9以下兼容jdk1.7- springboot2.x.x版本兼容jdk1.8- springboot3.0及以上版本兼容jdk17- springboot2.1之后的版本已经兼容JDK11 pom.xml:<?xmlversion="1.0"encoding="UTF-8"?><......
  • SpringBoot教学补充资料3-Maven安装
    Maven下载地址:https://maven.apache.org/download.cgi下载后进行解压,记住解压路径。         mvn -v ......
  • SpringBoot教学资料3-SpringBoot启动常见问题
    java.lang.RuntimeException:java.lang.RuntimeException:org.codehaus.plexus.component.repository.excMaven版本过高,与你使用的IDEA版本不兼容。推荐版本:maven3.6(建议)/maven3.9控制台Processterminatedhttps://blog.csdn.net/weixin_44589991/article/details/115013......
  • SpringBoot教学补充资料1-基础SQL语句
    #查询所有内容select*fromemployeeselect(属性1,属性2,...)from表名#通过id查询select*fromemployeewhereuid=1select(属性1,属性2,...)from表名where属性=指定的属性值#增加insertintoemployee(uid,uname,uage,uposition)values(8,'小黑',38,'实习生......
  • SpringBoot教学补充资料2-MyBatis简单查询项目
    SQL:CREATETABLE`tb_user`(`id`int(11)NOTNULLAUTO_INCREMENT,`username`varchar(20)DEFAULTNULL,`password`varchar(20)DEFAULTNULL,`gender`char(1)DEFAULTNULL,`addr`varchar(30)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBA......
  • 数据库之“语句查询字符串长度大于、小于或等于指定值”
    在MySQL中查询字符串长度使用length关键字,具体用法如下:先了解一下,Mysql中length()、char_length()的区别。length():mysql里面的length()函数是一个用来获取字符串长度的内置函数。char_length():在mysql内置函数里面查看字符串长度的还有一个函数是char_length()。这两个函数的区......
  • ES查询
    1.复合查询地址:http://10.0.0.109:9200/nhip-info-20230628/_search/post{"size":20,"query":{"bool":{"must":[{"match_all":{"boost":1.0}},{"......
  • 解决Springboot项目打成jar包后获取resources目录下的文件失败的问题
    前几天在项目读取resources目录下的文件时碰到一个小坑,明明在本地是可以正常运行的,但是一发到测试环境就报错了,说找不到文件,报错信息是:classpathresource[xxxx]cannotberesolvedtoabsolutefilepathbecauseitdoesnotresideinthefilesystem:jar:file:xxxx.jar!/......
  • SpringBoot 服务接口限流
    前言在开发高并发系统时有三把利器用来保护系统:缓存、降级和限流。限流可以认为服务降级的一种,限流通过限制请求的流量以达到保护系统的目的。一般来说,系统的吞吐量是可以计算出一个阈值的,为了保证系统的稳定运行,一旦达到这个阈值,就需要限制流量并采取一些措施以完成限制流量的......
  • SpringBoot项目从0到1配置logback日志打印
    大家好!我是sum墨,一个一线的底层码农,平时喜欢研究和思考一些技术相关的问题并整理成文,限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。以下是正文!一、写文背景我们在写后端项目的时候,日志打印是必需的。支持SpringBoot项目的日志框架一般有log4j、logback,这二者各有优......