Springboot基本使用
8440 ワード
2時間でspringboot==ideaで新しく作成し、最初のspringbootアプリケーション:new project後にspring initializrを選択してプロジェクト名を記入した後、次のステップでいいです.作成後に削除します.mvnとmvnwとmvn.cmd=新規HelloControllerクラス、以下のように
mainメソッドを起動するとspring bootプロファイルの使用元のアプリケーションプロファイル接尾辞をymlに変更し、==server:port:8081 context-path:/girlかつ注入可能なプロファイル:server:port:8081 cupSize:B age:20==と書きます.
このとき、WebページにはB 20が表示されます.
2.属性が多すぎる場合、どのように管理しますか?==server:port:8081 girl:cupSize:B age:20==上記のアプリケーション.yml構成でいいです.
@RestController public class HelloController { @Autowired private Girlproperties girlproperties;
}
Controllerの使用=@Controller httpリクエスト@RestController spring 4に新しく追加された注記を処理します.jsonに戻すには@ResponseBodyと@Controller@RequestMapping構成urlマッピング@PathVariable取得urlのデータ@Requestparam取得要求パラメータの値@GetMapping組合せ注記@RequestMapping(value="/hello",method=RequestMethod.GET)が@GetMapping(value="/hello")==
Springboot簡単な添削はpomで調べます.xmlに追加
プロファイル:=spring:profiles:datasource:driver-class-name:com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/weff username: root password: root jpa: hibernate: ddl-auto: update show-sql: true==
エンティティークラス:
Spring Dataに続くクラスのインタフェースを新規作成
コントロールレイヤで、まず注入します.
Springbootトランザクション@Transactional注記を追加削除方法で追加します.この場合、エラーが発生した場合はすべて実行しなくてもよいです.
@RestController
public class HelloController {
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String sry(){
return "hello! jpq";
}
}
mainメソッドを起動するとspring bootプロファイルの使用元のアプリケーションプロファイル接尾辞をymlに変更し、==server:port:8081 context-path:/girlかつ注入可能なプロファイル:server:port:8081 cupSize:B age:20==と書きます.
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String sry(){
return cupSize+age;
}
}
このとき、WebページにはB 20が表示されます.
2.属性が多すぎる場合、どのように管理しますか?==server:port:8081 girl:cupSize:B age:20==上記のアプリケーション.yml構成でいいです.
@Component
@ConfigurationProperties(prefix = "girl")
public class Girlproperties {
private String cupSize;
private Integer age;
//get set :alt+insert
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@RestController public class HelloController { @Autowired private Girlproperties girlproperties;
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String sry(){
return girlproperties.getCupSize()+girlproperties.getAge();
}
}
Controllerの使用=@Controller httpリクエスト@RestController spring 4に新しく追加された注記を処理します.jsonに戻すには@ResponseBodyと@Controller@RequestMapping構成urlマッピング@PathVariable取得urlのデータ@Requestparam取得要求パラメータの値@GetMapping組合せ注記@RequestMapping(value="/hello",method=RequestMethod.GET)が@GetMapping(value="/hello")==
Springboot簡単な添削はpomで調べます.xmlに追加
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
プロファイル:=spring:profiles:datasource:driver-class-name:com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/weff username: root password: root jpa: hibernate: ddl-auto: update show-sql: true==
エンティティークラス:
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private Integer age;
private String Size;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSize() {
return Size;
}
public void setSize(String size) {
Size = size;
}
}
Spring Dataに続くクラスのインタフェースを新規作成
public interface Girldao extends JpaRepository<Girl,Integer>{
}
コントロールレイヤで、まず注入します.
@Autowired
private Girlproperties girlproperties;
@Autowired
private Girldao girldao;
/**
* ,findAll sql
* @return
*/
/* @GetMapping(value = "/wee")
public List asda(){
return girldao.findAll();
}*/
/**
*
*/
/*@GetMapping(value = "/wet")
public Girl wde(){
Girl ge=new Girl();
ge.setAge(12);
ge.setSize(" ");
return girldao.save(ge);
}*/
/**
*
* @param mid
* @return
*/
@DeleteMapping(value = "/wet/{id}")
public void wde1(@PathVariable("id") Integer mid){
girldao.delete(mid);
}
}
Springbootトランザクション@Transactional注記を追加削除方法で追加します.この場合、エラーが発生した場合はすべて実行しなくてもよいです.