MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
若是你没用过MongoDB,能够先去看下个人文章:https://www.cnblogs.com/jiekzou/category/851166.html
接上一篇,修改pom.xml,添加mongodb的依赖html
<!--mongodb--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
添加mongodb数据库链接,修改application.ymljava
spring:
profiles:
active: dev
# mongodb
data: mongodb: database: test port: 27017 host: 192.168.1.18
修改原来的Person实体类mysql
public class Person { @Id private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } private String name; private String sex; public Person() { } public Person(Long id,String name, String sex) { this.id=id; this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }
新建包repository,而后在包下建立一个数据操做层接口PersonRepository,继承MongoRepository,代码以下:程序员
public interface PersonRepository extends MongoRepository<Person,Long> { }
建立一个controller类PersonController进行增删改查测试spring
@RestController public class PersonController { @Autowired private PersonRepository userRepository; @GetMapping("save") public String save() { Person userInfo = new Person(System.currentTimeMillis(),"李寻欢","男"); userRepository.save(userInfo); return "success"; } @GetMapping("getUserList") public List<Person> getUserList() { List<Person> userInfoList = userRepository.findAll(); return userInfoList; } @GetMapping("delete") public String delete(Long id) { userRepository.delete(id); return "success"; } @GetMapping("update") public String update(Long id, String username, String password) { Person userInfo = new Person(id, username, password); userRepository.save(userInfo); return "success"; } }
访问http://localhost:8083/boot/save,刷几遍,添加几条数据sql
而后再访问http://localhost:8083/boot/getUserList查看数据mongodb
固然,咱们也可使用可视化的mongodb管理工具去查看,这里我使用的是robo3t数据库
在配置了mysql、mongodb等数据库链接以后咱们发现,基本上咱们都离不开以下几个步骤:json
Thymeleaf就是一个模板引擎和.net的razor同样。Spring boot 推荐用来代替jsp。
Thymeleaf的优势
另外,Thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非Web环境中的应用开发。它是一个开源的Java库,基于Apache License 2.0许可,由Daniel Fernández建立,该做者仍是Java加密库Jasypt的做者。
因为Thymeleaf使用了XML DOM解析器,所以它并不适合于处理大规模的XML文件。也就是说它的性能是有必定问题的,若是文件较大的状况下。
关于Thymeleaf的语法能够参考官网:https://www.thymeleaf.org/documentation.html
修改pom.xml,添加以下依赖
<!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
这里有个坑,默认状况下hymeleaf中全部的标签都必须成对出现,不然IDEA运行时就会报错:" 必须由匹配的结束标记终止..“。
听说spring boot 2.0已结修复了这个标签的问题,可是我这里目前用的版本是低于2.0的,因此须要额外处理。
继续添加依赖
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency>
而后修改application.yml中的配置,
spring:
profiles:
active: dev
thymeleaf: mode: LEGACYHTML5
新建一个控制器类来作测试,AreaPageController,
@Controller public class AreaPageController{ @Autowired private AreaService areaService; @GetMapping("/addArea") public String addArea(Model model) { model.addAttribute("area", new Area()); return "addArea"; } @RequestMapping(value = "/addArea",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public String addArea(@Valid @ModelAttribute Area area, BindingResult bindingResult){ if (bindingResult.hasErrors()) { return "addArea"; }else{ Map<String,Object> modelMap= new HashMap<String,Object>() ; modelMap.put("success",areaService.addArea(area)); return "result"; } } }
修改以前的Area实体类,这东西就跟.net mvc 里面的模型验证同样
package com.yujie.model; import org.hibernate.validator.constraints.NotEmpty; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.util.Date; public class Area { private Integer areaId; @NotEmpty @Size(min=2, max=30) private String areaName; @NotNull @Min(1) @Max(200) private Integer priority; private Date createTime; private Date lastEditTime;
...... }
templates目录是存放html文件的,在templates目录下面新建一个html文件addArea.html,这个就至关于.net mvc中的razor视图。
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"><!--引入thymeleaf--> <head> <meta charset="UTF-8" /> <title>添加区域</title></head> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <body> <h2 style="color:green;text-align: center;">添加区域</h2> <form class="form-horizontal" role="form" action="#" th:action="@{/addArea}" th:object="${area}" method="post"> <div class="form-group"><label for="name" class="col-sm-2 control-label">区域名称:</label> <div class="col-sm-8"><input type="text" th:field="*{areaName}" class="form-control" id="areaName" placeholder="输入区域名称"></div> <label class="col-sm-2" style="color:red" th:if="${#fields.hasErrors('areaName')}" th:errors="*{areaName}">区域名称错误</label></div> <div class="form-group"><label for="priority" class="col-sm-2 control-label">优先级</label> <div class="col-sm-8"><input type="text" th:field="*{priority}" class="form-control" id="priority" placeholder="输入优先级"></div> <label class="col-sm-2" style="color:red" th:if="${#fields.hasErrors('priority')}" th:errors="*{priority}">优先级错误</label></div> <div class="form-group"> <div class="col-sm-12" style="text-align: center"> <button type="submit" class="btn btn-primary" id="btn">Submit</button> <input type="reset" class="btn btn-warning" value="Reset"/></div> </div> </form> </body> </html>
运行结果以下: