zoukankan      html  css  js  c++  java
  • Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介

    初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好。所以很久以来我的Spring学习都是出于停滞状态的。

    不过这种状态在我接触了Spring Boot之后,就发生了改变。Spring官方可能也觉得庞杂的配置是一件很不优雅的事情,虽然加入了包扫描,但是也没有触及灵魂。

    Spring还有一个问题就是依赖的冲突问题,这个对我这种半道出家的程序员来说更是痛苦至极。

    还好,所有的痛苦都被Spring Boot终结了。

    Spring Boot有三个特点:

    • 自动配置
    • 起步依赖
    • Actuator对运行状态的监控

    每一个特点都那么的美好。

    其实开发人员的本职工作是什么?是完成业务代码的编写,实现业务功能,因此如果消耗大量时间在Spring本身的配置和依赖冲突解决上,那么等于是浪费了大量的时间。

    Spring Boot的出现,可以说是对生产力的一次解放。

    闲话少叙,看一个需求。

    我现在想要写一个工具,连接数据库,实现增删改查功能,恐怕很多程序员会回忆起最初学习Java的时候,那堪称ugly的JDBC模板代码了吧。我本身是一个DBA,因为公司安排,写过很多JDBC代码,深刻的感觉到这种代码实在浪费时间,后来接触了JPA框架,感觉非常好。下面就用Spring Boot来实现一个数据库的增删改查功能。

    2. Spring Boot实战JPA

    数据库我会使用H2,这种嵌入式的数据库最适合在家学习的时候使用,很小,支持最基本的数据库操作,只要不涉及到太深刻的内容,感觉和MySQL差不多。至于如何在本机上启动一个H2 Server,就不在这里描述了。

    首先呢,打开IDEA,遵循下面的顺序:
    new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。

    组件选择

    上图是我选择的需要的组件。

    既然是JPA,那么我们首先要定义一个Entity,我的表叫做Demo,那么Entity也就是叫做Demo:

    package com.example.springwithjdbc.entity;
    
    
    import lombok.Data;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity
    @Data
    public class Demo {
        @Id
        @GeneratedValue
        private int id;
        
        private String uname;
    }
    

    注解解释:

    1. @Entity:表示这个类是个Entity;
    2. @Data:这是lombok提供的功能,这个注解加上之后,就不需要写getter和setter了,也不用写toString方法,都会自动生成;
    3. @Id:表示这个是主键;
    4. @GeneratedValue:表示采用自增

    接下来,需要经典的DAO层出现了:

    package com.example.springwithjdbc.dao;
    
    import com.example.springwithjdbc.entity.Demo;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface DemoDao extends JpaRepository<Demo, Integer> { }
    

    DAO层只是定义了一个interface,没有进行任何实现,其实也不需要进行任何具体的实现,注意继承的JpaRepository,它帮我们做了很多需要我们原先手动编码的工作。

    接下来就是经典的Service层了,Service即业务层:

    package com.example.springwithjdbc.service;
    
    import com.example.springwithjdbc.entity.Demo;
    
    import java.util.List;
    
    public interface IDemoService {
        Demo add(Demo demo);
    
        Demo findById(int id);
    
        List<Demo> findAll();
    }
    

    以上代码是service的接口,接下来编写具体的实现:

    package com.example.springwithjdbc.service;
    
    import com.example.springwithjdbc.dao.DemoDao;
    import com.example.springwithjdbc.entity.Demo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class DemoService implements IDemoService {
        @Autowired
        private DemoDao demoDao;
    
        @Override
        public Demo add(Demo demo) {
            return demoDao.save(demo);
        }
    
        @Override
        public Demo findById(int id) {
            return demoDao.findById(id).get();
        }
    
        @Override
        public List<Demo> findAll() {
            return demoDao.findAll();
        }
    }
    

    注解解释:

    1. @Service:表示这是一个Service;

    2. @Autowired:将DemoDao注入。

    接下来,我们需要一个Controller来实现REST接口:

    package com.example.springwithjdbc.controller;
    
    import com.example.springwithjdbc.entity.Demo;
    import com.example.springwithjdbc.service.IDemoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/")
    public class DemoRestController {
        @Autowired
        IDemoService demoService;
    
        @PostMapping("/save")
        public String save(@RequestParam(name = "name")String name) {
            Demo demo = new Demo();
            demo.setUname(name);
            return demoService.add(demo).toString();
        }
    
        @GetMapping("/find")
        public String findById(@RequestParam(name = "id")int id) {
            return demoService.findById(id).toString();
        }
    
        @GetMapping("/list")
        public String findAll() {
            List<Demo> demoList = demoService.findAll();
            return demoList.toString();
        }
    }
    

    最后,配置application.yml,配置数据源:

    spring:
      datasource:
        url: jdbc:h2:tcp://localhost/~/code/h2/bin/demo
        username: admin
        password: admin
      jpa:
        show-sql: true
    

    启动这个工程即可,接下来就可以用浏览器或者postman进行测试了,这是我用postman进行的测试,首先发送一个POST请求,写入一条数据:

    写入一条数据

    下面我们查询这条数据:

    查询单条记录

    多写几条数据以后,调用list接口:

    批量查询接口

    到这里,我们已经成功的编写了一段基于Spring Boot的JPA代码,实现了简单的新增和查询功能。比我之前写的JDBC代码不知道简单到哪里去了,而且也更加的优雅了,再也没有那么多复杂的让人难以看懂的xml配置了。

    Spring甚至贴心到做了一个网站专门生成工程骨架。

    3. 小结

    我最近在学习微服务,要学习微服务绕不开的就是Spring Cloud,而Spring Cloud离不开Spring Boot。因此首先了解Spring Boot是很有必要的。

    这是我的笔记整理出来的第一篇,希望能够帮助到其他和我一样在学习微服务,学习Spring Cloud,学习Spring Boot的人。

  • 相关阅读:
    [Python] Python2 、Python3 urllib 模块对应关系
    [Python] Mac pip安装的模块包路径以及常规python路径
    git 使用详解
    版本控制工具简介
    python基础练习题(二)
    python简介,安装
    python基础练习题(一)
    python练习题之面向对象(三)
    python之input函数,if,else条件语句使用的练习题(一)
    C++ 静态变量
  • 原文地址:https://www.cnblogs.com/wingsless/p/10699443.html
Copyright © 2011-2022 走看看