初次接触Spring的时候,我感受这是一个很难接触的框架,由于其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很很差。因此好久以来个人Spring学习都是出于停滞状态的。java
不过这种状态在我接触了Spring Boot以后,就发生了改变。Spring官方可能也以为庞杂的配置是一件很不优雅的事情,虽然加入了包扫描,可是也没有触及灵魂。程序员
Spring还有一个问题就是依赖的冲突问题,这个对我这种半道出家的程序员来讲更是痛苦至极。web
还好,全部的痛苦都被Spring Boot终结了。spring
Spring Boot有三个特色:sql
每个特色都那么的美好。数据库
其实开发人员的本职工做是什么?是完成业务代码的编写,实现业务功能,所以若是消耗大量时间在Spring自己的配置和依赖冲突解决上,那么等因而浪费了大量的时间。浏览器
Spring Boot的出现,能够说是对生产力的一次解放。app
闲话少叙,看一个需求。框架
我如今想要写一个工具,链接数据库,实现增删改查功能,恐怕不少程序员会回忆起最初学习Java的时候,那堪称ugly的JDBC模板代码了吧。我自己是一个DBA,由于公司安排,写过不少JDBC代码,深入的感受到这种代码实在浪费时间,后来接触了JPA框架,感受很是好。下面就用Spring Boot来实现一个数据库的增删改查功能。tcp
数据库我会使用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; }
注解解释:
接下来,须要经典的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(); } }
注解解释:
@Service:表示这是一个Service;
@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甚至贴心到作了一个网站专门生成工程骨架。
我最近在学习微服务,要学习微服务绕不开的就是Spring Cloud,而Spring Cloud离不开Spring Boot。所以首先了解Spring Boot是颇有必要的。
这是个人笔记整理出来的第一篇,但愿可以帮助到其余和我同样在学习微服务,学习Spring Cloud,学习Spring Boot的人。