springboot基本使用

2小时学会使用springboot
==在idea中新建,第一个springboot应用:new project后选择spring initializr 填入项目名后下一步即可。建好后删除.mvn和mvnw和mvn.cmd即可==
新建HelloController类,如下

@RestController
public class HelloController {
    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String sry(){
      return  "hello! jpq";
    }
}

启动main方法即可
spring boot配置文件的使用
将原先得application配置文件后缀改成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;
    }
}

这时,网页将出现B20;

2.属性过多时怎么管理?
==server:
port: 8081
girl:
cupSize: B
age: 20==
如上的application.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 spring4新加的注解,原先放回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>{
}

在controller层中,先注入:

 @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注解,这时发生错误的话可以全部不执行

你可能感兴趣的:(java基础知识总结-经典)