项目展示:
项目结构:
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:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.7
springbootsy
sy2
0.0.1-SNAPSHOT
sy2
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
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:
article列表
id
标题
内容
操作
查看详情
articleDetail.html:
article 详情
title:
content:
id
author
content
Article.java:
package springbootsy.sy2.domain;
import java.util.List;
public class Article {
private int id;
private String title;
private String content;
private List 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 getCommentList() {
return commentList;
}
public void setCommentList(List 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:
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 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 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