
```xml
4.0.0
com.wang
springtf
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
5.1.46
com.alibaba
druid
1.1.6
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
org.projectlombok
lombok
1.18.8
org.springframework.boot
spring-boot-starter-test
- 创建springboot的启动入口类,`com.wang.Application`
```java
package com.wang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.wang.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/test")
public String test() {
return "hello springboot";
}
}

<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.46version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.6version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.0.1version>
dependency>
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/liuyanban?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
username: root
password:
mybatis:
mapper-locations: classpath:mapper/*.xml
package com.wang.pojo;
import lombok.Data;
import java.util.Date;
@Data
public class Article {
private Long id;
private String name;
private String title;
private String content;
private Date created;
}
package com.wang.mapper;
import com.wang.pojo.Article;
import java.util.List;
public interface ArticelMapper {
List<Article> queryAllArticles();
boolean saveArticle(Article article);
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.mapper.ArticelMapper">
<select id="queryAllArticles" resultType="com.wang.pojo.Article">
select * from article
select>
<insert id="saveArticle" useGeneratedKeys="true" keyProperty="id">
insert into article values (null,#{name},#{title},#{content},#{created})
insert>
mapper>
package com.wang.service;
import com.wang.mapper.ArticelMapper;
import com.wang.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ArticleService {
@Autowired
private ArticelMapper articelMapper;
public List<Article> queryAllArticles() {
return articelMapper.queryAllArticles();
}
public boolean saveArticle(Article article) {
return articelMapper.saveArticle(article);
}
}
package com.wang.controller;
import com.wang.pojo.Article;
import com.wang.service.ArticleService;
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.PostMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@Controller
public class ArticleController {
@Autowired
private ArticleService articleService;
@GetMapping("/")
public String index(Model model) {
List<Article> articles = articleService.queryAllArticles();
model.addAttribute("articles", articles);
return "index";
}
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("article", new Article());
return "add";
}
@PostMapping("/add")
public void addArticel(Article article, HttpServletResponse response) throws IOException {
Date date = new Date();
article.setCreated(date);
boolean b = articleService.saveArticle(article);
System.out.println(b);
response.sendRedirect("/");
}
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Boot 简单留言板title>
<link rel="stylesheet" type="text/css" th:href="@{bootstrap/dist/css/bootstrap.css}">
<script th:src="@{js/jquery-1.9.1.js}">script>
<script th:src="@{bootstrap/dist/js/bootstrap.js}">script>
head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigationspan>
<span class="icon-bar">span>
<span class="icon-bar">span>
<span class="icon-bar">span>
button>
<a class="navbar-brand" href="/">留言板a>
div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">首页a>li>
<li><a href="/add">留言a>li>
ul>
div>
div>
nav>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="panel panel-primary" th:each="article,key:${articles}">
<div class="panel-heading">
<h3 class="panel-title" th:text="${article.title}" style="margin-bottom: 10px">张三h3>
<span th:text="作者:+${article.name}">span>
<span th:text="时间:+${#dates.format(article.created,'yyyy-MM-dd HH:mm:ss')}">span>
div>
<div class="panel-body">
<p th:text="${article.content}">你好p>
div>
div>
div>
<div class="col-md-3">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">最新评论h3>
div>
<div class="panel-body">
Panel content
div>
div>
div>
div>
div>
body>
html>
列表页

新增页
