1、之前使用 SSM(Spring+SpringMVC+MyBatis
)+Maven 寫后端的接口,創建了不少 Maven 工程。一開始還覺得 SSM+Maven 是十分簡便的,但是后來審美疲勞,漸漸感覺這種固定化地創建 New Maven Project 是一件體力操作。
2、比如那些 web.xml
(全局配置)、dispatch-Servlet.xml
(SpringMVC配置)、applicationContext.xml
(Spring配置)、mybatis-config.xml
(MyBatis配置)等大同小異,一開始還會老老實實從0開始寫,后來煩了直接復制粘貼之前的代碼。
3、SpringBoot
就解決這個硬傷,很快建立工程,使用 SpringData
封裝數據庫訪問層基本 CURD 接口(SSM還得使用逆向工程),一個簡單的 application.yml
配置就搞定前面的所有配置,一手輕巧的注解取代了之前大量的代碼。
這次根據 SpringBoot
官方API文檔等,測試一下基於 SpringBoot
的 CURD,同時啟用 Restful 風格
,編寫起代碼來,十分具有美感
簡單的 CRUD,后台的基本
1、代碼結構
總體來說,還是采用了標准的編程模式,建立 entity、dao、service、controller
包進行分類包裝,部分接口采用 interface+implements
。命名方面也有注意。
代碼應該是很規范的。
2、Student.java
實體
package com.cun.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.validator.constraints.NotEmpty; @Entity // 實體 @Table(name = "t_student") // 數據庫表名 public class Student { @Id // 主鍵 @GeneratedValue // 自增 private Integer id; @NotEmpty(message = "學生姓名不能為空") // 表單驗證 @Column(length = 20) // 字段長度 private String t_name; @Column(length = 20) // 字段長度 private String major; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getT_name() { return t_name; } public void setT_name(String t_name) { this.t_name = t_name; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
3、StudentDao.java
dao 接口
實現類都不用寫了,SpringData-JPA 自動幫你實現,里邊雖然空空,但是單表基本CRUD接口都寫好了
package com.cun.dao; import org.springframework.data.jpa.repository.JpaRepository; import com.cun.entity.Student; /** * 簡單的dao層只需要繼承JpaRepository接口,即可, * 兩個參數,分別表示 —— 實體類型、主鍵類型 * 復雜sql語句再自己增加接口 * @author linhongcun * */ public interface StudentDao extends JpaRepository<Student, Integer>{ }
4、StudentService.java
事務處接口
package com.cun.service; import java.util.List; import com.cun.entity.Student; public interface StudentService { public void addStudent(Student student); public void deleteStudent(Integer id); public void updateStudent(Student student); public Student findStudent(Integer id); public List<Student> findAllStudent(); }
5、StudentServiceImpl.java
事務處接口實現類
package com.cun.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.cun.dao.StudentDao; import com.cun.entity.Student; import com.cun.service.StudentService; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentDao studentDao; @Override public void addStudent(Student student) { // TODO Auto-generated method stub studentDao.save(student); } @Override public void deleteStudent(Integer id) { // TODO Auto-generated method stub studentDao.delete(id); } @Override public void updateStudent(Student student) { // TODO Auto-generated method stub studentDao.save(student); } @Override public Student findStudent(Integer id) { // TODO Auto-generated method stub return studentDao.findOne(id); } @Override public List<Student> findAllStudent() { // TODO Auto-generated method stub return studentDao.findAll(); } }
6、StudentController.java
控制層
package com.cun.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.cun.entity.Student; import com.cun.service.StudentService; //@RestController 代替 @Controller,省略以后的 @ResponseBody @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentservice; /** * 顯示所有 * url:"http://localhost/student/findall" * * @return */ @RequestMapping(value = "/findall") public List<Student> findAllStudent() { return studentservice.findAllStudent(); } /** * 查找 restful 風格 * url:"http://localhost/student/findone/1" * * @param id * @return */ // == @RequestMapping(value = "/findone/{id}", method = RequestMethod.GET) @GetMapping("/findone/{id}") public Student findStudentRestful(@PathVariable("id") Integer id) { return studentservice.findStudent(id); } /** * 刪除 restful 風格 * url:"http://localhost/student/deleteone/4" * 注意無法通過瀏覽器的鏈接來模擬檢驗,可以通過 jquery的 $.ajax方法,並type="delete" * * @param id */ // == @RequestMapping(value = "/deleteone/{id}", method = RequestMethod.DELETE) @DeleteMapping("/deleteone/{id}") public void deleteStudentRestful(@PathVariable("id") Integer id) { studentservice.deleteStudent(id); } /** * 增加 restful 風格 * url:"http://localhost/student/addone" * 通過<form>表單模擬驗證 * * @param student */ // == @RequestMapping(value="/addone",method=RequestMethod.POST) @PostMapping("/addone") public void addStudentRestful(Student student) { studentservice.addStudent(student); } /** * 修改 restful 風格 * url:"http://localhost/student/updateone" * 驗證:可以通過 jquery的 $.ajax方法,並type="put",同時注意data形式——A=a&B=b&C=c * * @param student */ // == @RequestMapping(value="/addone",method=RequestMethod.PUT) @PutMapping("/updateone") public void updateStudentRestful(Student student) { studentservice.updateStudent(student); } }
7、index.html
接口測試,這里寫得有點簡陋,不過還是可以正確測試接口寫得是否有問題
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/jquery-1.7.2.js"></script> <script type="text/javascript"> /** * 實際應用時,里邊的參數應根據實際而改變,而不是寫死的, * 這里僅僅①簡單地模擬前后端,②簡單測試接口 * */ function del() { $.ajax({ type : "delete", url : "http://localhost/student/deleteone/4", async : true }); } function upd() { $.ajax({ type : "put", data:"id=6&t_name=cun&major=計科", url : "http://localhost/student/updateone", async : true }); } </script> </head> <body> <!-- 刪除 --> <button id="btn" onclick="del()">delete request</button> <!-- 更新 --> <button id="btn2" onclick="upd()">update request</button> <!-- 增加 --> <form action="http://localhost/student/addone" method="post"> major<input type="text" name="major" /> t_name<input type="text" name="t_name" /> <input type="submit" value="submit" /> </form> <!-- 查詢 --> <a href="http://localhost/student/findone/6">select request</a> </body> </html>
8、application.yml
配置,算是很簡單了,下面幾個配置是最常用的
server: port: 80 #為了以后訪問項目不用寫端口號 context-path: / #為了以后訪問項目不用寫項目名 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/testspring username: root password: 123 jpa: hibernate: ddl-auto: update #數據庫同步代碼 show-sql: true #dao操作時,顯示sql語句
9、pom.xml
在 eclipse 創建 SpringBoot 工程時加入
以后再加入 mysql、jpa、web 就好了,並且可以通過 Alt+/
=> Edit Starter 加入