1.所有用到的依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.xellitix.commons</groupId>
<artifactId>jackson-utils</artifactId>
<version>0.2.0</version>
</dependency>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
2.application.properties中的簡(jiǎn)單配置
spring.datasource.url=jdbc:mysql://127.0.0.1/book?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=rootroot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
#spring.thymeleaf.check-template=false
#spring.thymeleaf.check-template-location=false
server.port=8080
controller中的代碼
@Controller
@RequestMapping("article")
public class ArticleController {
@Autowired
private ArticleRepository articleRepository;
/**
* Description: 文章列表
*/
@RequestMapping("")
public ModelAndView articlelist(@RequestParam(value = "start", defaultValue = "0") Integer start,
@RequestParam(value = "limit", defaultValue = "5") Integer limit) {
start = start < 0 ? 0 : start;
Sort sort = Sort.by(Sort.Direction.DESC, "id");
Pageable pageable = PageRequest.of(start, limit, sort);
Page<Article> page = articleRepository.findAll(pageable);
ModelAndView mav = new ModelAndView("article/list");
mav.addObject("page", page);
return mav;
}
/**
* Description: 根據(jù)id獲取文章對(duì)象
*/
@GetMapping("/{id}")
public ModelAndView getArticle(@PathVariable("id") Integer id) throws Exception {
Article articles = articleRepository.findById(id);
ModelAndView mav = new ModelAndView("article/show");
mav.addObject("article", articles);
return mav;
}
/**
* Description: 新增操作視圖
*/
@GetMapping("/add")
public String addArticle() throws Exception{
return "article/add";
}
/**
* Description: 新增保存方法
*/
@PostMapping("")
public String saveArticle(Article model) throws Exception{
articleRepository.save(model);
return "redirect:/article/";
}
/**
* Description: 刪除
*/
@DeleteMapping("/{id}")
public String del(@PathVariable("id") long id) throws Exception{
articleRepository.deleteById(id);
return "redirect:";
}
/**
* Description: 編輯視圖
*/
@GetMapping("/edit/{id}")
public ModelAndView editArticle(@PathVariable("id") long id) throws Exception {
Article model = articleRepository.findById(id);
ModelAndView mav = new ModelAndView("article/edit");
mav.addObject("article", model);
return mav;
}
/**
* Description: 修改方法
*/
@PutMapping("/{id}")
public String editArticleSave(Article model, long id) throws Exception{
model.setId(id);
articleRepository.save(model);
return "redirect:";
}
}
入口類的特殊添加
@EnableJpaAuditing
@SpringBootApplication
Article實(shí)體類自動(dòng)生成了對(duì)Id的set和get方法,沒有@Data的注解,這里需要將對(duì)Id的set和get方法注釋,然后添加@Data注解

2.png
這里的處理方式是為了解決可能出現(xiàn)的以下問題:
ERROR 1409 --- [nio-8080-exec-2]
org.thymeleaf.TemplateEngine:
[THYMELEAF][http-nio-8080-exec-2]
Exception processing template "article/list":
An error happened during template parsing
(template: "class path resource [templates/article/list.html]")
代碼拉取地址:
https://gitee.com/xgkp/SpringBoot8_5_1.git