server:
port: 8082 #端口號
servlet:
context-path: /girl #項目名
複製代碼
yml配置文件java
server:
port: 8082 #端口號
servlet:
context-path: /girl #項目名
cupSize: B
複製代碼
Controller代碼mysql
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return "cupSize:"+cupSize;
}
}
複製代碼
頁面輸出效果web
yml 配置文件spring
server:
port: 8082 #端口號
servlet:
context-path: /girl #項目名
cupSize: B
age: 18
content : "cupSize: ${cupSize},age: ${age}"
複製代碼
controller調用代碼sql
@RestController
public class HelloController {
@Value("${content}")
private String content;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return content;
}
}
複製代碼
yml文件數據庫
server:
port: 8082 #端口號
servlet:
context-path: /girl #項目名
girl:
cupSize: B
age: 18
複製代碼
引用的bean類app
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
複製代碼
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String cupSize;
private Integer age;
複製代碼
controller 注入調用代碼ide
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return girlProperties.getCupSize();
}
}
複製代碼
spring:
profiles:
active: prod
複製代碼
映射兩個或以上的url到同一個方法,可使用集合。函數
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
public String say(){
return girlProperties.getCupSize();
}
}
複製代碼
請求地址爲:http://localhost:8082/girl/hello/sayspring-boot
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/say",method = RequestMethod.POST)
public String say(){
return girlProperties.getCupSize();
}
}
複製代碼
獲取請求地址上定義的數值
@RequestMapping(value = "/say/{id}",method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id ){
return "id:" +id;
}
複製代碼
請求地址:http://localhost:8082/girl/hello/say/100
輸出效果:
獲取請求地址上?key=value 的參數值
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam("id") Integer myId ){
return "id:" +myId;
}
複製代碼
請求地址:http://localhost:8082/girl/hello/say?id=666
輸出效果:
其他參數
value 爲默認參數
required 表明參數是否必須
defaultValue 參數默認值
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam(value = "id", required = false,defaultValue = "0") Integer myId ){
return "id:" +myId;
}
複製代碼
簡化的一種註解寫法,代替了
@RequestMapping(value = "/say",method = RequestMethod.GET)
@GetMapping("/say")
public String say(@RequestParam("id") Integer myId ){
return "id:" +myId;
}
複製代碼
pom.xml引入JPA和mysql驅動包
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
複製代碼
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: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
#JPA建立表默認引擎爲 MyISAM,不支持事務回滾
# 加上如下設置JPA建立表時引擎爲支持事務的InnoDB,
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
複製代碼
實體類
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;
private Integer height;
public Girl() {}
複製代碼
編寫接口類繼承 JpaRepository接口
public interface GirlRepository extends JpaRepository<Girl,Integer> {
}
複製代碼
controller調用
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository ;
/** * 查詢全部女生列表 * @return */
@GetMapping("/girls")
public List<Girl> girlList(){
return girlRepository.findAll();
}
}
複製代碼
輸出效果:
controller調用
使用post方式添加一條數據,返回的是添加的這個對象
/** * 添加一個女生 * @param cupSize * @param age * @param height * @return */
@PostMapping("/girls")
public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age, @RequestParam("height") Integer height){
Girl girl = new Girl();
girl.setCupSize(cupSize);
girl.setAge(age);
girl.setHeight(height);
return girlRepository.save(girl);
}
複製代碼
輸出效果:
經過id查找單一對象findById(id).orElse(null),沒有找到數據不會報錯,並返回空,
//查詢一個女生
@GetMapping("/girls/{id}")
public Girl girlFindOne(@PathVariable("id") Integer id){
return girlRepository.findById(id).orElse(null);
}
複製代碼
//更新一個女生
@PutMapping("/girls/{id}")
public Girl girlUpdate(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age, @RequestParam("height") Integer height){
Girl girl = new Girl();
girl.setId(id);
girl.setCupSize(cupSize);
girl.setAge(age);
girl.setHeight(height);
return girlRepository.save(girl);
}
複製代碼
postman 使用put方法傳遞參數須要使用 x-www-form-urlencoded
controller 調用
// 刪除一個女孩
@DeleteMapping("/girls/{id}")
public void girlDel(@PathVariable("id") Integer id){
girlRepository.deleteById(id);
}
複製代碼
GirlRepository 擴展查詢接口
public interface GirlRepository extends JpaRepository<Girl,Integer> {
public List<Girl> findByAge(Integer age);
}
複製代碼
controller 調用
//經過年齡去查女生
@GetMapping("/girls/age/{age}")
public List<Girl> girlListByAge(@PathVariable("age") Integer age){
return girlRepository.findByAge(age);
}
複製代碼
在類上面打上@Service 和 @Component 都支持事務回滾
注意事項【不然回滾不了】
@Transactional回滾的方法必須爲public
mysql數據庫引擎必須爲InnoDB,特別檢查對應的表是否InnoDB
Spring boot 2.0 的JPA 自動生成的表默認引擎爲MyISAM,不支持事務回滾,在application.yml配置文件加上database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
#JPA建立表默認引擎爲 MyISAM,不支持事務回滾
# 加上如下設置JPA建立表時引擎爲支持事務的InnoDB,
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
複製代碼
Service 類代碼
@Service
public class GirlService {
@Autowired
private GirlRepository girlRepository;
@Transactional
public void insertTwo(){
Girl girlA = new Girl();
girlA.setCupSize("A");
girlA.setAge(18);
girlA.setHeight(155);
girlRepository.save(girlA);
Girl girlB = new Girl();
girlB.setCupSize("BBBBB");
girlB.setAge(16);
girlB.setHeight(170);
girlRepository.save(girlB);
}
}
複製代碼