參考:https://www.cnblogs.com/tanlei-sxs/p/9855071.html
中途出現問題時參考了太多
1.下載idea社區版
2.在settings -> Plugins 里邊搜Spring Assistant,安裝完后重啟idea
3.創建新項目,選擇Spring Assistant,Spring web starter
4.等下載完畢,運行Applicatoin程序,可以看到一個頁面:
Whitelabel Error Page
5.添加代碼如下:
@RequestMapping("/")
String index() {
return "Hello World";
}
6.運行程序。
7.創建一個package名為controller
8.添加Java類,命名為HelloController,粘帖以下代碼
@RestController
public class HelloController {
@RequestMapping("/hello")
public String say(){
return "Hello World!";
}
}
9.重新運行程序。
10.增刪改查實例,先添加依賴,點一下Import Changes
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
11.創建mysql數據庫springbootdb
12.刪除原先的application.properties,創建yml文件application.yml,粘帖以下代碼
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springbootdb?serverTimezone=UTC
username: root
password: xxx
jpa:
hibernate:
ddl-auto: update
show-sql: true
13.創建一個package名為entity,創建Java類Person,粘帖以下代碼
@Entity
public class Person {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
//必須要有構造函數
public Person() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
14.運行程序,會自動將表建好
15.創建package名為dao,創建java類PersonRepository,粘 帖以下代碼
public interface PersonRepository extends JpaRepository<Person, Integer> {
}
16.創建控制器PersonController,粘帖以下代碼
@RestController
public class PersonController {
@Autowired
private PersonRepository personRepository;
@GetMapping("/person")
private List<Person> personList(){
return personRepository.findAll();
}
/*
新增一個人員*/
@PostMapping(value = "/personadd")
private Person add(@RequestParam("name")String name, @RequestParam("age")Integer age){
Person person =new Person();
person.setName(name);
person.setAge(age);
return personRepository.save(person);
}
/**
* 根據id查找一個人員
*/
@GetMapping(value = "/person/{id}")
public Person personFindOne(@PathVariable("id") Integer id) {
return personRepository.findById(id).get();
}
/**
* 刪除一個員工
*/
@DeleteMapping(value = "/person/{id}")
public void deletep(@PathVariable("id") Integer id){
personRepository.deleteById(id);
}
/**
* 更新一個員工
*/
@PutMapping(value = "/per/{id}")
public Person put(@PathVariable("id") Integer id,
@RequestParam ("name") String name,
@RequestParam ("age") Integer age){
Person person=new Person();
person.setName(name);
person.setAge(age);
return personRepository.save(person);
}
}
17.在數據表中添加兩條數據,訪問根目錄/person,可以看到json數據。
18.訪問/person/1,可以看到指定的json數據。
19.如果要更改端口號和路徑,寫法如下:
注意:server和spring要並列寫,在網上找了好久才找到為什么。另外,關於context_path無效,后來的版本改過了,必須是server——servlet——context_path。
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springbootdb?serverTimezone=UTC
username: root
password: XXX
jpa:
hibernate:
ddl-auto: update
show-sql: true
server:
#端口號
port: 8888
#項目名,如果不設定,默認是 /
servlet:
context-path: /w