SpringBoot基础

创建springBoot项目

通过IDEA的Spring Initializr创建springBoot项目

一、三种启动方式

1.DemoApplication.java启动类 -> 打开 -> 右击 -> run DemoApplication

2.项目根目录 -> mvn spring-boot:run

3.mvn install -> java -jar demo_0.0.1_snapshot.jar

二、springBoot配置文件

application.properties 或者 application.yml  (注意yml键值间要空格)

application.properties

--|server.port=8081

--|server.context-path=/demo

application.yml

--|server

----|port: 8081

----|context-path: /demo

三、项目属性配置 @Value  @Component  @ConfigurationProperties

3.1yml配置文件-值读取

application.yml

cupSize: B

age: 18

content: "cupSize: ${cupSize}, age: ${age}"      #获取yml属性值

3.2通过类获取yml配置文件属性值

@RestController

public class Demo{

    @value("${cupSize}")

     private String cupSize;      //获取yml属性值

    

     @value("${age}")

     private Integer age;

     @value("${content}")

     private String content;

   @RequestMapping(value="/say" method = RequestMethod.GET)

    public void say(){

      return content;

    }

}

3.3配置文件属性值对象化

application.yml

girl: 

 cupSize: B

 age: 18

/**

*需要注入  @Component

**/

@Component

@ConfigurationProperties("prefix="girl")

public class GirlProperties{

private String cupSize;

private Integer age;

//相应get和set省略

}

四、controller的使用

@Controller  处理http请求。 配合模板使用,使用模板带来性能吮耗,所以不推荐使用。

@RestController   Spring4之后新加注解,原来返回json需要@ResponseBody配合@Controller

@RequestMapping  配置url映射

4.1@RestController 和 @RequestMapping配置两个url映射

@RestController

public class HelloController{

@RequestMapping(value={"hello","hi"} method=RequestMethod.GET)

public void say(){

return  girlProperties.getCupSize();

}

}

4.2@RequestMapping给整个类配置url映射

@RestController

@RequestMapping("/hello")

public class HelloController{

@RequestMapping(value="/say" method=RequestMethod.GET)

public String say(){

return  girlProperties.getCupSize();

}

}

访问: localhost:8080/hello/say

4.3获取请求参数

@PathVariable     获取url中数据

@RequestParam  获取请求参数值

@GetMapping     组合注解

4.3.1@PathVariable实例

@RestController

@RequestMapping("/hello")

public class HelloController{

@RequestMapping(value="/say/{id}" method=RequestMethod.GET)

public String say(@PathVariable("id") Integer id){

return  "id"+id;

}

}

4.3.2@RequestParam实例

@RestController

@RequestMapping("/hello")

public class HelloController{

@RequestMapping(value="/say" method=RequestMethod.GET)

public String say(@RequestParam(value="id",defaultValue="0") Integer myId){

return  "id"+myId;

}

}

 访问:localhost:8080/hello/say?id=100

4.4@GetMaping和@PostMaping简化注解写法

@RestController

@RequestMapping("/hello")

public class HelloController{

@GetMapping("/say")    //等同于:  @RequestMapping(value="/say" method=RequestMethod.GET)

public String say(@RequestParam(value="id",defaultValue="0") Integer myId){

return  "id"+myId;

}

}

五、数据库操作

5.1Spring-Data-Jpa : springBoot对hibernate整合

5.2数据库配置和JPA配置:application.yml

spring: 

   datasource: 

      driver-class-name: com.mysql.jdbc.Driver

      url: jdbc:mysql://127.0.0.1:3306/dbgirl

      username: root

      password: 123456

jpa:

      hebernate:

        ddl-auto: create

        show-sql: true

5.3数据库操作

接口:

public interface GirlRepository extends JpaRepository<Girl,Integer>{

    //扩展接口-以年龄查询

    public List<Girl> findByAge(Integer age);

}

类:

@RestController

public class GirlController{

    @Autowired

    private GirlRepository girlRepository ;

    

      /**

     *查询表全部记录

     */

    @GetMapping(value="/girls")

     public List<Girl> girlList(){

          return girlRepository.findAll(); 

      }

     /**

     *添加一条记录

     */

     @PostMapping(value="/girls")

     public List<Girl> girlAdd(@RequestParam("cupSize")String cupSize,@RequestParam("age")String 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.findOne(id);

     }

     //更新

    @PutMapping(value="girls/{id}")

    public Girl girlFindOne(@PathVariable("id") Integer id,

      @RequestParam("cupSize") Integer cupSize){

       Girl girl = new Girl();

       girl.setId(id);

       girl.setCupSize(cupSize);

      return girlRepository.save(girl);

     }

       

    //删除

    @DeleteMapping(value="girls/{id}")

    public Girl girlDelete(@PathVariable("id") Integer id){

      return girlRepository.delete(id);

     }

}

六、事物管理

事物管理@Transactional

Service类:

@Service

pubic class GirlService{

    @Autowired

   private GirlRepository girlRepository;

    @Transactional

    public void  insertTwo(){

        Girl girlA = new Girl();

        girlA.setCupSize("A");

        girlA.setAge(18);

       girlRepository.save(girlA);

        Girl girlB = new Girl();

        girlB.setCupSize("B");

        girlB.setAge(19);

        girlRepository.save(girlB);

    }

}

Controller类:

@RestController

public class GirlController{

       @PostMapping

        public void girlTwo(){

         girlService.insertTwo();

         }

}

原文地址:https://www.cnblogs.com/chenweichu/p/6863912.html