首页 > 编程语言 > 详细

SpringBoot入门学习笔记

时间:2017-08-24 12:12:33      阅读:331      评论:0      收藏:0      [点我收藏+]
SpringBoot是SpringMVC的升级版,SpringBoot的特点:

技术分享

application.properties文件配置

server.port = 8080端口配置
server.context-path = /girl URL前缀

application.yml文件配置:
server:
   port: 8081  
   context-path: /girl

建议使用yml文件来配置

属性配置:

@Value:单属性配置

@Value("${cupSize}")
private String cupSize;//通过一个注解,把配置文件里的属性注入进来

配置文件里是这样的:
server:
   port: 8080
girl:
    cupSize: B
     age: 18
类里面是这样的:
@Component
@ConfigurationProperties(prefix = "girl")//可以把配置文件里相应前缀下的属性全部注入进来
public class GirlProperties{

      private String cupSize;
      private Integer age;

      getset方法略....
}

实体类初始化属性注入时,需要添加两个注解

@Component//把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>
Controller层的注解使用:
技术分享

匹配多个路径:
@RequestMapping(value={“hello”,“hi”} ,method=RequestMethod.GET)

RestController = Controller + ResponseBody

@PathVariable("id")获取路url中的数据
@RequestMapping(value="hello/{id}" ,method=RequestMethod.GET)
使用方法:public String say(@PathVariable("id")Integer id){}

@RequestParam("id")获取请求的参数值
@RequestMapping(value="hello" ,method=RequestMethod.GET)
使用方法:public String say(@RequestParam("id")Integer id){}
  value:请求的属性
  required:是否必须如传参
  
 defaultValue:默认值
@RequestMapping(value="hello" ,method=RequestMethod.GET)
使用方法:public String say(@RequestParam(value="id",required=false,defaultValue="0")Integer id){}

@GetMapping 组合注解

使用方法:
@GetMapping(value="/say")意思就是get方式的请求映射

还有@PostMapping(value="/say")



数据库操作:

实体类设计:(注意要生成一个无参构造器,听说不这样的话就会报错)

技术分享

数据库配置:
ddl-auto: create 每次都会创建表 如果表存在就会先drop再create
ddl-auto: update 表不存在就create 表存在就update
ddl-auto: validate 验证表结构
ddl-auto: create-drop 程序结束后会把表drop掉

技术分享

查询所有和新增:
 1  private GirlRepository girlRepository;
 2 
 3 //查询所有女生
 4 @GetMapping(value="/girls")
 5 public List<Girl> girlList(){
 6 
 7    return girlRepository.findAll();
 8 }
 9 
10 //添加一个女生
11 @PostMapping("/girls")
12 public girl girlAdd(@RequestParam("cupSize")String cupSize,
13                                 @RequestParam("age") Integer age){
14               Girl girl=new Girl();
15               girl.setCupSize(cupSize);
16               girl.setAge(age);
17           return girlRepository.save(girl);
18       }
19  
20 //查询一个女生
21 @GetMapping("/girls/{id}")
22 public girl girlFindOne(@PathVariable("id")Integer id) {
23     return girlRepository.findOne(id);
24 }
25 
26 //更新
27 //添加一个女生
28 @PutMapping("/girls/{id}")
29 public girl girlAdd (@PathVariable("id") Integer id,
30                                 @RequestParam("cupSize")String cupSize,
31                                 @RequestParam("age") Integer age){
32                Girl girl=new Girl();
33                girl.setId(id);
34                girl.setCupSize(cupSize);
35                girl.setAge(age);
36               return girlRepository.save(girl);
37       }  
38 
39 //删除
40 @DeleteMapping(value="/girls/{id}")
41 public void girlDelete(@PathVariable("id") Integer id){
42         girlRespository.delete(id);
43 }                            
数据库操作Jpa实现增删改查,接口方法名有讲究

技术分享

 

实务操作 只需要加上注解@Transactiona


SpringBoot入门学习笔记

原文:http://www.cnblogs.com/jiliunyongjin/p/7422270.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!