后端
pom.xml
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 模板引擎 Thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- mysql驱动 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring-data-jpa 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- lombok 依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
application.properties
############################
###数据库配置信息
############################
##基础信息
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##连接池中最大的活跃连接数
spring.datasource.tomcat.max-active=20
##连接池中最大、最小的空闲连接数
spring.datasoure.max-idle=8
spring.datasoure.min-idle=8
##初始化连接数
spring.datasoure.initial=10
############################
###Spring Data JPA配置信息
############################
spring.jpa.database=mysql
# 显示SQL语句
spring.jpa.show-sql=true
##指定DDL mode (none, validate, update, create, create-drop)
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.thymeleaf.cache=false
entity
package com.example1.springdatajpa.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* 创建Ssp持久化类
* 1.使用@Entity注解实现实体类的持久化,JPA检测到之后,可以在数据库中生成对应的表
* 2.使用@Id指定主键
* 3.使用 @GeneratedValue指定主键策略,mysql默认自增
* 4.使用@Data简化get/set
*/
@Entity
@Data
public class Ssp {
@Id
@GeneratedValue
private Integer id;
private String avatar;
private String writer;
private String time;
private String title;
private String content;
private String logo;
private Integer number1;
private Integer number2;
}
dao
package com.example1.springdatajpa.dao;
import com.example1.springdatajpa.entity.Ssp;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SspRepository extends JpaRepository<Ssp,Integer> {
}
service及实现
package com.example1.springdatajpa.Services;
import com.example1.springdatajpa.entity.Ssp;
import org.hibernate.validator.constraints.EAN;
import java.util.List;
public interface SspService {
Ssp save(Ssp ssp);
List<Ssp> getAll();
Ssp get(int id);
}
package com.example1.springdatajpa.Services.impl;
import com.example1.springdatajpa.Services.SspService;
import com.example1.springdatajpa.dao.SspRepository;
import com.example1.springdatajpa.entity.Ssp;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class SspServiceImpl implements SspService {
@Resource
private SspRepository sspRepository;
@Override
public Ssp save(Ssp ssp) {
return sspRepository.save(ssp);
}
@Override
public List<Ssp> getAll() {
return sspRepository.findAll();
}
@Override
public Ssp get(int id) {
return sspRepository.findById(id).get();
}
}
controller
package com.example1.springdatajpa.Controller;
import com.example1.springdatajpa.Services.SspService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
@Controller
@RequestMapping(value = "/ssp")
public class SspController {
private static final String SSP_DETAIL_PATH_NAME = "sspDeatil";
private static final String SSP_LIST_PATH_NAME = "sspList";
@Resource
private SspService sspService;
@GetMapping("/all")
public String getSspList(ModelMap map) {
map.addAttribute("sspList",sspService.getAll());
return SSP_LIST_PATH_NAME;
}
/**
* 获取 SSP
* 处理 "/ssp/{id}" 的 GET 请求
*/
@GetMapping(value = "/{id}")
public String getSsp(@PathVariable Integer id, ModelMap map) {
map.addAttribute("sspDeatil",sspService.get(id));
return SSP_DETAIL_PATH_NAME;
}
}
前端
sspList.html页面写法
<html xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>少数派热门文章</title>
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
<style>
.title{
font-size: 20px;
/*padding-left: 5px;*/
color: white;;
}
.center{
font-size:30px;
color:white;
}
body{
background-color: whitesmoke;
}
.avatar{
width: 50px;
height:50px;
margin-left: 20px;
margin-top: 5px;
}
.avatar1{
width: 50px;
height:50px;
/*margin-top: 5px;*/
float: left;
}
.bar{
background-color: #292525;
height:60px;
line-height:60px;
width:100%;
/*position:absolute;*/
/*z-index:5;*/
top:0;
text-align:center;
}
.btn{
margin-top: 12px;
width:80px;
border-radius: 15px;
color: white;
background-color:transparent;
border: 1px solid white;
}
.btn:hover {
background-color:white; /* Green */
color: black;
}
.font{
margin-left: 20px;
}
.li{
width:130px;
text-align: center;
color: #292525;
font-size: 16px;
}
.down{
margin-top: 20px;
margin-left:20px;
}
.avatar{
width:45px;
height:45px;
float: left;
}
.font1{
/*margin-left: 20px;*/
}
.font2{
/*margin-left: 20px;*/
color: lightgray;
/*padding-top: 10px;*/
}
.font-total{
margin-left: 70px;
}
.total{
margin-left: 20px;
margin-top: 20px;
}
.font-big{
margin-top: 10px;
font-size: 25px;
font-weight: 700;
}
</style>
</head>
<body>
<nav class="navbar navbar-default bar">
<div class="container">
<div class="row">
<div class="col-md-2">
<img th:src="@{img/biao.jpg}" class="img-circle avatar">
<span class="title">少数派</span>
</div>
<div class="col-md-7">
<span class="center">#热门文章</span>
</div>
<div class="col-md-3">
<img th:src="@{img/write.png}">
<h style="color: #292525;">s</h>
<img th:src="@{img/search.png}" >
<img th:src="@{img/32.jpg}" class="img-circle avatar">
</div>
</div>
</div>
</nav>
<div class="container">
<div class="font">
<ul class="nav nav-tabs">
<li role="presentation" class="li"><a href="#"style="color: #292525;font-size: larger">正版软件</a></li>
<li role="presentation" class="li"><a href="#"style="color: #292525;font-size: larger">付费栏目</a></li>
<li role="presentation" class="li"><a href="#"style="color: #292525;font-size: larger">Matrix</a></li>
<li role="presentation" class="li" ><a href="#"style="color: #292525;font-size: larger">专题广场</a></li>
<li role="presentation" class="li"><a href="#"style="color: #292525;font-size: larger">热门文章</a></li>
<li role="presentation" class="li"><a href="#"style="color: #292525;font-size: larger">应用推荐</a></li>
<li role="presentation" class="li"><a href="#"style="color: #292525;font-size: larger">生活方式</a></li>
<li role="presentation" class="li"><a href="#"style="color: #292525;font-size: larger">新玩意</a></li>
</ul>
</div>
</div>
<div class="container">
<div class="down">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div th:each="ssp:${sspList}" >
<div class="thumbnail" style="height: 300px; ">
<div class="total">
<img th:src="@{${ssp.avatar}}" class="img-circle avatar1">
<div class="font-total">
<p class="font1" th:text="${ssp.writer}"></p>
<p class="font2" th:text="${ssp.time}"></p>
</div>
<div class="row">
<div class="col-md-8">
<p class="font-big" th:text="${ssp.title}"></p>
<th scope="row" th:text="${ssp.id}"></th>
<td><a th:href="@{/ssp/{sspId}(sspId=${ssp.id})}" th:text="${ssp.title}"></a></td>
<p th:text="${ssp.content}"></p>
</div>
<div class="col-md-4">
<img th:src="@{${ssp.logo}}" style="width: 210px;height: 160px;">
</div>
<div style="margin-left: 590px">
<img th:src="@{img/love.png}">
<span class="font2" th:text="${ssp.number1}"></span>
<img th:src="@{img/number.png}">
<span class="font2" th:text="${ssp.number2}"></span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
</div>
</div>
</body>
</html>
bookDetail.html页面
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
<link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
<meta charset="UTF-8"/>
<title>文章详情</title>
<style>
.logo{
width: 600px;
height: 260px;
margin-left: 300px;
}
.title{
font-size: 36px;
text-align: center;
width: 736px;
margin-left: 200px;
}
.author-line{
display: flex;margin-top: 30px;
}
.writer{
margin-top: 30px;
font-size: 24px;
margin-left: 10px;
}
.content{
font-size: 26px;
margin-top: 30px;
}
</style>
</head>
<body>
<div class="container">
<h3>Spring Data JPA练习</h3>
<img th:src="@{${sspDeatil.logo}}" class="logo">
<p th:text="${sspDeatil.title}" class="title"></p>
<span th:text="${sspDeatil.time}"class="time"></span>
<div class="author-line">
<img th:src="@{${sspDeatil.avatar}}" class="avatar">
<p th:text="${sspDeatil.writer}" class="writer"></p>
</div>
<span th:text="${sspDeatil.content}" class="content"></span>
<!--<div class="col-md-2">-->
<!--</div>-->
<!--<div class="col-md-8">-->
<!--<img th:src="@{${sspDeatil.logo}}" class="logo">-->
<!--<div class="col-md-2">-->
<!--<p th:text="${sspDeatil.title}"></p>-->
<!--<span th:text="${sspDeatil.time}"class="time"></span>-->
<!--</div>-->
<!--<div class="col-md-10">-->
<!---->
<!--</div>-->
<!--<div class="col-md-4">-->
<!--<img th:src="@{${sspDeatil.avatar}}" class="avatar">-->
<!--<p> th:text="${sspDeatil.writer}" class="writer"></p>-->
<!--<div class="col-md-8"></div>-->
<!--<span th:text="${sspDeatil.content}" class="content"></span>-->
<!--<div class="col-md-2"></div>-->
<!--</div>-->
<!--</div>-->
</div>
</body>
</html>
效果图
13520722F13613F4825CBB6E8EB6C704.png
C8D26B6301F5F2E338C5262063AD2A4D.png