Rails 写给 Rails 开发者的 Spring boot 教程

rocLv · March 25, 2019 · Last by ken replied at September 21, 2020 · 3808 hits

最近想研究一下 Spring Cloud 方面的内容,为公司后期发展做一些技术调研,就上网找了一下是否有与 Rails 相关 Spring boot 之类的文章。文章没有找到,不过找到了一个 PPT,理论上应该有个视频,不过视频貌似已经过期了。

我根据这个 PPT 整理了一下,也算借花谢佛了。

RVM 的 Java 版

对于 Ruby 开发者来说,可能第一步做的就是下载 Ruby 版本控制工具,如 RVM,或 RBENV。由于 Java 总是向前兼容的,所以就省去了版本控制 的工具,这里我们直接下载安装 JDK 8,也是当前最流行的 Java 版本。

$   java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

顺便吐槽一下,这个 java 命令一点也不*nix...

RubyMine/Vim

Spring Tool Suite 或者 IntelliJ IDEA

Rake 的 Java 版

之前比较流行的是 maven,目前比较流行的应该是 gradle 吧,目测 Android Studio 用的就是 gradle。

当然,maven 和 gradle 做的比 Rake 要多一些,我感觉类似于 Rake + Bundle。

gem install rails

与 rails 命令行类似的工具是 springboot

$ brew tap pivotal/tap
$ brew install springboot

安装完以后你会发现,如果执行$ which spring,指向的是 rvm 目录下的 spring。这是因为 rails 会安装 spring 命令,这个 spring 会用来加速开发环境的启动。

那么我们只需要做以下两步,就可以避免他们的冲突:

  1. 把 springboot 的路径添加至 PATH $ echo "export PATH=/usr/local/Cellar/springboot/2.1.3.RELEASE/bin:$PATH" >> ~/.zshrc.local $ source ~/.zshrc.local
  2. 重命名 spring 命令为 springboot $ cd /usr/local/Cellar/springboot/2.1.3.RELEASE/bin $ mv spring springboot $ which springboot

貌似这个命令有点长,你可以使用 alias 缩短一下命令:

$ echo "alias sb='springboot'" >> ~/.aliases
$ source ~/.aliases

rails new blog

$ springboot init blog -d=web,data-jpa,h2,freemarker -g com.bcenv.demo -a blog
total 56
-rwxr-xr-x  1 wangqsh  staff   8.9K  3 25 11:10 mvnw
drwxr-xr-x  4 wangqsh  staff   128B  3 25 11:10 src
-rw-r--r--  1 wangqsh  staff   432B  3 25 11:10 HELP.md
-rw-r--r--  1 wangqsh  staff   5.7K  3 25 11:10 mvnw.cmd
-rw-r--r--  1 wangqsh  staff   1.6K  3 25 11:10 pom.xml

进入我们的项目下面:

$ cd blog
  • blog 是项目名称,也是项目工作的目录
  • -ddependency的缩写,这里指依赖 web 包,ORM 使用data-jpa, 数据库用h2( 内存数据库), freemarker 是模板引擎,类似于 erb.
  • -g 是组织前缀,一般的顺序正好是域名颠倒过来,比如com.ruby-china.demo
  • -a 项目 id, 一般可以用项目名称
  • Spring Boot CLI默认会使用 Maven, 如果想用 gradle 可以添加参数--build gradle
  • 使用springboot help init 可以了解跟多的命令

rails server

$ mvn spring-boot:run

访问 http://localhost:8080/

rails g scaffold Post title content:string

这个并没有,而且还是故意没有...

routes.rb

这个也并没有...

Spring Boot 中定义路由的方法:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PagesController {
  @RequestMapping("/")
  public String home() {
     return "home";
  }
}
  • 注解 @Controller会告诉 Spring 为这个类查找RequestMapppings的注解

  • @RequestMapping 会用一个路径,HTTP 动词,header, 参数等来和一个路径关联

path_for

并没有

resources :photos

@RequestMapping("/photos")
public class PhotosController {
    @RequestMapping(value = "/", method = RequestType.GET)
    public String listPhotos() { ... }

    @RequestMapping(value = "/new", method = RequestType.GET)
    public String newPhoto() { ... }

    @RequestMapping(value = "/", method = RequestType.POST)
    public String createPhoto() { ... }

    @RequestMapping(value = "/{photoId}", method = RequestType.GET)
    public String showPhoto(@PathVariable("photoId") Integer id, Model model) { ... }

    @RequestMapping(value = "/{photoId}/edit", method = RequestType.GET)
    public String editPhoto(@PathVariable("photoId") Integer id, Model model) { ... }

    @RequestMapping(value = "/{photoId}", method = RequestType.PUT)
    public String updatePhoto() { ... }

    @RequestMapping(value = "/", method = RequestType.DELETE)
    public String deletePhoto() { ... }
}

rails g model Post title content:text

  • 使用[Spring Data JPA](http://projects.spring.io/spring-data-jpa/)
  • 使用@Entity 注解来声明 Model
  • Spring 中 Model 指的是你传递给 view 的对象
@Entity(name = "categories")
public class Category {
  @ID
  @GeneratedValue
  private Long id;
  private String name;
  private Long groupId;

  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinColumn(name = "categoryId")
  @Where(clause = "enabled = 1")
  @OrderBy("name")
  private Set<Project> projects;
}

db/schema.rb

Spring Boot 可以自动创建 schema, 它会自动导入 schema.sql 和 data.sql. 也支持针对不同数据库的 schema, 命名规则是 schema-platform.sql, data-platform.sql, 例如 schema-postgresql.sql, data-postgresql.sql

路径是src/main/resources/schema.sql

rails test

$ mvn test    #单元测试(使用[Maven Surefire Plugin](https://maven.apache.org/surefire/maven-surefire-plugin/)
$ mvn verify #单元测试和集成测试[Maven Failsafe Plugin](https://maven.apache.org/surefire/maven-failsafe-plugin/)

Rest API

  • Spring Data Rest

其他的一些,感觉和 Rails 其实相差比较多了。我觉得倒不如直接去学习 Spring Boot 了。

之前我们接 Java 团队进来是都实现成 API,NGINX 上做规则整合。 或者 RPC,感觉代码上还是保持各自语言的优势

这个东西,没有什么可比性。

java 面向就业岗位和简历编程。

Reply to edwardzhou

太片面了

Reply to rocLv

是有些片面,但很赤裸裸的直接。

你看看身边的 java 程序员,是不是至少八九成都只是个 spring 框架的使用者?有多少会写单元测试,功能测试,关注架构?

别说 TDD (Test Driven Development),连 DDT (Development Driven Test) 都做不到。

java 开发人员离开了 Spring, 是不是大部分都得直接跪在那里。

Reply to edwardzhou

没怎么了解过 Java 那边的社区不好评论,虽有点这方面的感觉,但真有如此夸张?

7 Floor has deleted
8 Floor has deleted

其他语言也一样。
只跟人有关。

可以,大家有空可以玩玩Spring

https://github.com/spring-guides/gs-spring-boot/issues/17#issuecomment-308879160

对于 springboot cli 的 spring 和 rails 里的 spring 冲突问题,这里有更加优雅的处理方式。

You need to Sign in before reply, if you don't have an account, please Sign up first.