Boot初体验-就那么回事儿

Spring-Boot初体验


基础配置

  1. 环境配置

    1. java环境>>>1.8
    2. maven环境>>>3.5
    3. IDE编辑器>>>Idea2015
  2. 创建boot项目方式

    1. idea创建 >>> 选择springinitiallizr 创建
    2. spring官网创建.>>> start.spring.io
  3. 启动方式

    1. main方法启动
    2. 密令行启动
      1. 进入项目的文件夹
      2. 执行: mvn spring-boot:run
    3. 找到相应jar文件 执行java -jar 相应的文件名启动.....(我觉得不好用...)
  4. 相关配置在resource文件夹下的application.properties文件

    1. 服务相关信息,如端口,路径前缀的设置:

      server.port=8081 //修改默认端口号
      server.context-path=/prepath //访问路径前缀

    2. 服务相关信息推荐使用yml文件格式 ,Idea 也对相应的文件做了支持,推荐使用yml格式

      yml格式如下: ( yml有自己的语法,:冒号后跟一个空格 )

      yuml文件 >>>>>>>>>>>>>>>>>>>
      server:
        port: 8082
        context-path: /pre
      
    3. 在配置文件中配置属性并调用

      1. 在配置中声明属性

      2. 在@RestController标注的类中调用配置信息

        1. 如果是简单的数据

          yuml文件 >>>>>>>>>>>>>>>>>>>
          cupSize: B
          
          @Value("${cupSize}")
          private String size;
          
        2. Javabean数据类型

          yuml文件 >>>>>>>>>>>>>>>>>>>
          boy:
            name: "张三"
            age: 18
          
          @Autowired
          private Boy boy ;
          @RequestMapping(value = "/hello",method = RequestMethod.GET)
          public String ss() {
              System.out.println(boy);
              return "Hello , boot !";
          }
          
          @Repository  // 声明bean
          @ConfigurationProperties(prefix = "boy")
          public class Boy {
              private String name;
              private Integer age;
              ....封装略
              }
          
          
      3. 处理多环境配置

        创建多套配置环境后如何修改环境?

        application.yml

        application-dev.yml

        application-product.yml

        在application.yml文件中修改
        spring:
          profiles:
            active: "product"          //选择的环境   双引号加 - 后边的字符串内容
        
    4. Controller 使用

      1. RestController //spring 4 后出来的一个组合注解 ,原来返回json需要@Controller+@RequestBody

      2. Controller

      3. RequestMapping

        GetMapping (value= " / path " ) //组合注解

               @RequestMapping(value = "/hello1/{id}",method = RequestMethod.GET)
            @GetMapping(value = "/hello1/{id}")
            public String sss(@PathVariable("id") Integer id) {
                return  "id : "+id ;
            }          
            @PathVariable("id")  类似于    @RequestParam("id")
        

    代码实现

  5. springboot操作数据库

    1. SpringBoot-Data-Jpa=====>>>>MySql

      关于SpringBoot-Data-Jpa>>> 定义一些列对象持久化标准, 目前实现这一规范的产品有Hibernate,toplink等等

      1. 相应的配置

        1. application.yml文件>>>>>>
          spring:
            profiles:
              active: "product"
            datasource:
                driver-class-name: com.mysql.jdbc.Driver
                url: jdbc:mysql:///test
                username: root
                password: mysql
            jpa:
              hibernate:
                ddl-auto: update
              show-sql: true
          
          
        2. 实体类:

          
          @Entity
          public class Student {
              @Id
              @GeneratedValue
              private Integer id;
          
              private String name;
          
              private String pwd;
          
              private Integer age;
                  ...... 
          }
          
        3. dao层

          import java.util.List;
          @Repository
          public interface StudentDao extends JpaRepository<Student , Integer > {
              //扩展接口   根据年龄来操作数据库
              public List<Student> getStuListByAge(Integer age);
          }
          
          
        4. Service层

          @Service
          public class StudentService {
              @Autowired
              private StudentDao studentDao ;
              @Transactional  //声明事务
              public void  saveTwo () {
                  Student student = new Student("刘邦","1234");
                  studentDao.save(student);
                  Student student1 = new Student("项羽","1234");
                  studentDao.save(student1);
                  throw new RuntimeException("newError");
              }
          }
          
        5. controller层

          事务处理测试>>>>>>>>>>>>>>>>>..
          @RestController
          @RequestMapping("/stu")
          public class StudentController {
          
            @Autowired
            private StudentService studentService;
            @PostMapping("/sav")
            public void  saveTwoTest () {
                studentService.saveTwo();
            }

        }

        ```

        增删改查初学>>>>>>>>>>>

        ```
        @Autowired
        private Boy boy ;
        @Autowired
        private StudentDao studentDao;
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public String ss() {
            return "index";
        }
        //@RequestMapping(value = "/hello1/{id}",method = RequestMethod.GET)
        @GetMapping(value = "/hello1/{id}")
        public String sss(@PathVariable("id") Integer id) {
            return  "id : "+id ;
        }
        //查询
        @GetMapping(value = "/gets")
        public List<Student> getlist(){
            return studentDao.findAll();
        }
        //增加
        @GetMapping(value = "/add")
        public Student getlist(String name, String pwd){
            return studentDao.save(new Student(name,pwd));
        }
        //根据id查询
        @GetMapping(value = "/get/{id}")
        public Student getStuById (@PathVariable("id") Integer id ) {
           return studentDao.findOne(id) ;
        }
            //根据id更新
        @PutMapping(value = "/up/{id}")
        public Student upStuById (@PathVariable("id") Integer id ) {
            Student student = new Student();
            student.setId(id);
            student.setName("ceshi");
            student.setPwd("123");
           return studentDao.save(student);
        }
        //根据id删除
        @DeleteMapping(value = "/del/{id}")
        public void delStuById ( @PathVariable ("id") Integer id ) {
            studentDao.delete(id);
        }
        //根据age查询
        @GetMapping(value = "/age/{age}")
        public List<Student> getStuByAge (@PathVariable("age") Integer age ) {
            return studentDao.getStuListByAge(age);
        }


        ```

     6. web层:     此处未做jsp页面......

          使用postman测试.........

        注意:   PutMapping  在postman中应在body中选择 x-www-form-urlencodde 方式

        ​
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,992评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,972评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,778评论 18 399
  • 米水的比例一般是1:1, 如果是天然气炉具,一开始就开最小火,见蒸汽后开始计时,十分钟后关火,再焖制几分钟即可。 ...
    鹤舞霏扬阅读 620评论 0 0
  • 大姐说故乡已经飘起了雪 我的窗外是寒冷的雨夜 那么轻柔,那么小的片,那么灵盈的雪 只有一小层那么薄薄地 明天就在人...
    贾士隐刘玉美阅读 245评论 2 2