第一步快速搭建springboot項目:在你建立的工程下創建 Module 選擇Spring initializr創建。
第二步:修改包名、項目名、web項目打成war包、在Type處選擇: Maven Project(項目的構建工具)
第三步:選擇你項目需要的基本依賴
第四步:結束
springboot項目的結構:
注意點:
1、.mvn文件、mvnw、mvnw.cmd可以刪掉
2、程序啟動類必須在所有接口類的上一層,才能被掃描到
配置數據庫連接文件:(兩種文件形式的:properties、yml)默認掃描:application開頭的文件
具體類容:yml文件形式
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx?useSSL=true&verifyServerCertificate=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8
username: xxx
password: xxx
創建實體類:
@Entity --需要導入jpa依賴包
@Table(name="employee") ---指向數據庫的表名
public class Employee {
@Id --表中的主鍵、自增長形式
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer empNo;
private String empName;
private String empSex;
private Integer empAge;
private Double sal;
private Date history;
public Employee() {
}
public Employee(Integer empNo, String empName, String empSex, Integer empAge, Double sal, Date history) {
this.empNo = empNo;
this.empName = empName;
this.empSex = empSex;
this.empAge = empAge;
this.sal = sal;
this.history = history;
}
public Integer getEmpNo() {
return empNo;
}
public void setEmpNo(Integer empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpSex() {
return empSex;
}
public void setEmpSex(String empSex) {
this.empSex = empSex;
}
public Integer getEmpAge() {
return empAge;
}
public void setEmpAge(Integer empAge) {
this.empAge = empAge;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Date getHistory() {
return history;
}
public void setHistory(Date history) {
this.history = history;
}
@Override
public String toString() {
return "Employee{" +
"empNo=" + empNo +
", empName='" + empName + '\'' +
", empSex='" + empSex + '\'' +
", empAge=" + empAge +
", sal=" + sal +
", history=" + history +
'}';
}
}
數據層:直接繼承JpaRepositoryAPI類
public interface EmployeeDaoImpl extends JpaRepository<Employee,Integer> {
}
注意此處省略service層、是為了測試小demo
控制層:
@RestController ---rest分格
@RequestMapping("/jpa") ----請求模塊分層
public class JpaController {
@Autowired ----注入
private EmployeeDaoImpl employeeDaoImpl;
@RequestMapping(value = "/select",method = RequestMethod.GET)
public List<Employee> selectUser(){//查
return employeeDaoImpl.findAll();
}
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String addUser(Employee emp){//增
Employee employee=new Employee();
employee.setEmpName(emp.getEmpName());
employee.setEmpAge(emp.getEmpAge());
employee.setEmpSex(emp.getEmpSex());
employee.setHistory(new Date());
employee.setSal(emp.getSal());
Employee employee1=employeeDaoImpl.save(employee);
return employee1.toString();
}
@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
public void deleteUser(@RequestParam(value = "id")Integer id){//刪
employeeDaoImpl.deleteById(id);
}
@RequestMapping(value="/{id}" ,method=RequestMethod.GET)
public Optional<Employee> getAccountById(@PathVariable("id") int id){
return employeeDaoImpl.findById(id);
}
@RequestMapping(value = "/update",method = RequestMethod.PUT)
public Employee updateUser(Employee emp){//修
return employeeDaoImpl.save(emp);
}
}
postman測試:CRUD
---不足之處,請多指教