说明
本文章只是非常简单的入门,springboot+mongodb+thymeleaf
仅供参考,如有错误,还请大佬指正
=====================================
一、创建maven项目
二、导入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
三、编写properties文件
spring.data.mongodb.uri=mongodb://127.0.0.1:27017
spring.data.mongodb.database=coffee
保证mongodb处于开启状态
四、编写实体类
package com.pojo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Setter
@Getter
@Document("product")
public class Products {
// @Id的作用的指定id,避免和下面的那个重复
@Id
private Object _id;
@Field
private double id;
private String name;
private String describe;
private String introduce;
private String descriptor;
private String[] picture;
private String type;
private double deleted;
public Products(Object _id, double id, String name, String describe, String introduce, String descriptor, String[] picture, String type, double deleted) {
this._id = _id;
this.id = id;
this.name = name;
this.describe = describe;
this.introduce = introduce;
this.descriptor = descriptor;
this.picture = picture;
this.type = type;
this.deleted = deleted;
}
public Products() {
}
}
注意: 实体类需要和对应的集合名称对应,如果不对应,需要加上@Document("集合名称")
来指明。
@Id
的作用是:因为自定义了id属性,所以加上该注解来标明“_id”才是主键。
@Field
的作用是:如果字段的名字是“user_name”,但是实体类中写的是“userName”,加上该注解,在编译时,自动将“userName”编译成“user_name”。
五、编写持久层(service)
定义接口
package com.dao;
import com.pojo.Products;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductDao{
public List<Products> findAll();
public void insert(Products product);
public void delete(String id);
public void update(Products product);
}
@Repository用在持久层的接口上,这个注解是将接口的一个实现类交给spring管理。
六、实现接口`
package com.service;
import com.dao.ProductDao;
import com.pojo.Products;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductService implements ProductDao {
@Autowired
MongoTemplate mongoTemplate;
@Override
public List<Products> findAll() {
List<Products> list = mongoTemplate.findAll(Products.class);
for (Products pro : list) {
System.out.println("========>>>" + pro);
}
System.out.println("执行了service ==> findAll方法");
return list;
}
@Override
public void insert(Products product) {
mongoTemplate.insert(product);
}
@Override
public void delete(String id) {
mongoTemplate.remove(id);
}
@Override
public void update(Products product) {
mongoTemplate.save(product);
}
}
@Service
的作用是:如果一个类带了@Service注解,将自动注册到Spring容器。
@Autowired
的作用是:对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
七、编写控制层
package com.controller;
import com.pojo.Products;
import com.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/productPreview")
public String toProductPreview(Model model){
List<Products> list=productService.findAll();
model.addAttribute("list",list);
System.out.println("总长度:"+list.size());
for(Products pro:list){
System.out.println("========>>>"+pro);
}
return "productPreview";
}
}
@Controller
的作用是:用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping 注解。
@RequestMapping
的作用是:用来处理请求地址映射的注解。简单点就是,通过访问该注解括弧内的地址可以找到对应的controller对象(路标)。
八、编写html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<td>id</td>
<td>名字</td>
<td>短评</td>
<td>简介</td>
<td>价格</td>
<td>假删除</td>
<td>类型</td>
</tr>
<tr th:each="pro : ${list}">
<td th:text="${pro.id}">id</td>
</tr>
</table>
</body>
</html>
xmlns:th="http://www.w3.org/1999/xhtml"
:xmlns其实是XML NameSpace的缩写。它指示了这里使用的xml标签来自哪里的定义。
th:each="pro : ${list}
:thymeleaf的语法,用来循环后端拿到的列表/数组。
th:text="${pro.id}"
:thymeleaf语法,用来取在list列表拿到的对象的id属性。
注意: 该页面的名字和controller层的return值对应。
该页面的名字为:productPreview
controller层:
@RequestMapping("/productPreview")
public String toProductPreview(Model model){
List<Products> list=productService.findAll();
model.addAttribute("list",list);
System.out.println("总长度:"+list.size());
for(Products pro:list){
System.out.println("========>>>"+pro);
}
return "productPreview";
}
九、截图
mongodb
至此,你已经可以编写一个及其简单的springboot+mongodb+thymeleaf项目。本文章只提供思路,供springboot集成mongodb没有头绪的小伙伴参考;更加精美的页面需要自己去探索,毕竟我也是百度了一天的bug才做出了这么简单的东西。自己努力的成果最香。
最后,如果文中有错误或者不足的地方,恳请大佬留言指正。