初始化好工程以後java
新建一個HelloController.javamysql
package com.example.girl1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/girl",method = RequestMethod.GET)
public String say(){
return "1111111";
}
}
複製代碼
在瀏覽器中輸入localhost:8080/girl 內容就是1111111web
resource 目錄下 將application.xxx修改爲application.ymlspring
server:
port: 8080
servlet:
context-path: /first
複製代碼
以後訪問就得 :http://localhost:8080/first/girlsql
server:
port: 8080
cupSize: A
複製代碼
HelloController.java中shell
@RestController
public class HelloController {
// 經過註解把配置文件中的cupSize注入到變量中
@Value("${cupSize}")
private String cupSize;
@RequestMapping(value = "/girl",method = RequestMethod.GET)
public String say(){
return cupSize;
}
}
複製代碼
打開瀏覽器就能看到cupSize了數據庫
package com.example.girl1;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
// 獲取前綴是girl的配置
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String cupSize;
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
}
複製代碼
配置文件json
server:
port: 8080
girl:
cupSize: A
age: 18
複製代碼
HelloControllerapi
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/girl",method = RequestMethod.GET)
public Integer say(){
return girlProperties.getAge();
}
}
複製代碼
這樣就能作到用前綴來區分,本身打開瀏覽器看看就明白了瀏覽器
// application.yml
spring:
profiles:
active: dev
複製代碼
server:
port: 8080
girl:
cupSize: B
age: 18
複製代碼
server:
port: 8081
girl:
cupSize: A
age: 20
複製代碼
java -jar jar包名 --spring.profiles.active=prod
複製代碼
// 若是想訪問兩個
@RequestMapping(value = {"/girl","/hello"},method = RequestMethod.GET)
複製代碼
// 能夠給類加,訪問就得 :localhost:8080/hello/girl了
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = {"/girl"},method = RequestMethod.GET)
public Integer say(){
return girlProperties.getAge();
}
}
// RequestMethod方式有不少
能夠使用postman來測試api
複製代碼
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = {"/girl/{id}"},method = RequestMethod.GET)
public Integer say(@PathVariable("id") Integer myId){
return myId;
}
}
// localhost:8080/girl/100
複製代碼
//localhost:8080/girl?id=100
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = {"/girl"},method = RequestMethod.GET)
public Integer say(@RequestParam("id") Integer myId){
return myId;
}
}
// 另外一種寫法
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = {"/girl"},method = RequestMethod.GET)
// 默認值和是否必須傳入
public Integer say(@RequestParam(value = "id",required = false,defaultValue = "10") Integer myId){
return myId;
}
}
// RequestMapping 能夠寫成GetMapping
複製代碼
在pom.xml導入包
// jpa和mysql的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
複製代碼
結束後導入一下包
// 運行前先新建一個dbgirl的數據庫,utf8mb4
// application.yml
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update // create會把以前的表刪掉在新建一個
show-sql: true
複製代碼
package com.example.girl1;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
// 這個註解表示這個類就是對應數據庫中的表
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
public Girl() {
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
}
// 以後表中的那些字段就是girl類中對應過去的
複製代碼
get /girls 獲取女生列表
post /girls 建立一個女生
get /girls/id 經過id查詢一個女生
put /girls/id 經過id更新一個女生
delete /girls/id 經過id刪除一個女生
複製代碼
新建一個GirlRepository.java的接口
package com.example.girl1;
import org.springframework.data.jpa.repository.JpaRepository;
// Girl類名,Integer是id的類型
public interface GirlRepository extends JpaRepository<Girl,Integer> {
}
複製代碼
新建GirlController.java
package com.example.girl1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository;
/* * 查詢女生列表 * */
@GetMapping(value = "/girls")
public List<Girl> girlList(){
return girlRepository.findAll();
}
}
// 使用postman測試
複製代碼
/* * 新增一個女生 * */
@PostMapping(value = "/girls")
public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){
Girl girl = new Girl();
girl.setCupSize(cupSize);
girl.setAge(age);
return girlRepository.save(girl);
}
複製代碼
// 查詢一個女生
@GetMapping(value = "/girls/{id}")
public Girl girlFindOne(@PathVariable("id") Integer id){
return girlRepository.findById(id).orElse(null);
}
複製代碼
// 更新
@PutMapping(value = "/girls/{id}")
public Girl girlUpdate(@PathVariable("id") Integer id,@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){
Girl girl = new Girl();
girl.setId(id);
girl.setCupSize(cupSize);
girl.setAge(age);
return girlRepository.save(girl);
}
複製代碼
// 刪除
@DeleteMapping(value = "/girls/{id}")
public void girlDelete(@PathVariable("id") Integer id){
girlRepository.deleteById(id);
}
複製代碼
// 經過年齡查詢女生列表
@GetMapping(value = "/girls/age/{age}")
public List<Girl> girlListByAge(@PathVariable("age") Integer age){
return girlRepository.findByAge(age);
}
複製代碼
GirlRepository.java中
package com.first.first;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface GirlRepository extends JpaRepository<Girl,Integer> {
// 經過年齡查詢
public List<Girl> findByAge(Integer age);
}
複製代碼
需求是若是我有兩條數據,一條插入失敗的話另外一條就不能插入,這時候須要加一個事務
GirlService.java
package com.example.girl1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class GirlService {
@Autowired
private GirlRepository girlRepository;
@Transactional
public void insertTwo(){
Girl girlA= new Girl();
girlA.setAge(10);
girlA.setCupSize("B");
girlRepository.save(girlA);
Girl girlB= new Girl();
girlA.setAge(11);
girlA.setCupSize("F");
girlRepository.save(girlB);
}
}
// 在Girlcontroller裏新增
@Autowired
private GirlService girlService;
@PostMapping(value = "/girls/two")
public void girlTwo(){
girlService.insertTwo();
}
複製代碼