Rails 框架首次提出是在 2004 年 7 月,它的研发者是 26 岁的丹麦人 David Heinemeier Hansson。不同于已有复杂的 Web 开发框架,Rails 是一个更符合实际需要而且更高效的 Web 开发框架。Rails 结合了 PHP 体系的优点(快速开发)和 Java 体系的优点(程序规整),因此,Rails 在其提出后不长的时间里就受到了业内广泛的关注。
SpringBoot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot 致力于在蓬勃发展的快速应用开发领域。
Rails: Rake SpringBoot:
$ brew install maven
$ brew install gradle
Rails: gem install rails SpringBoot: SpringBoot 客户端使用 SpringBoot CLI
$ brew tap pivotal/tap
$ brew install springboot
Rails: rails new blog SpringBoot:
$ spring init blog -d=web,data-jpa,h2,freemarker
$ cd foo
$ mvn dependency:resolve
参数说明:
blog
项目名称-d
选择数据库--build gradle
spring help init
web
使用 Spring MVC 框架和内置的 Tomcath2
内存数据库 (相当于 Rails 的 SQLite 数据库)data-jpa
等同于 Rails 的 ActiveRecord,自动映射 model 和数据库freemarker
模版引擎spring init --list
查看更多参数Rails: rails s
SpringBoot: 启动服务器,使用spring-boot:run
,
这个命令又插件 Spring Maven/Gradle Plugin 插件提供
$ mvn spring-boot:run
打开浏览器查看http://localhost:8080/
rails generate scaffold Post title content:string SpringBoot 没有代码生成功能,也不需要配置 xml
Rails 在 routes.rb 中统一管理路由 SpringBoot
@Controller
@RequestMapping
.import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PagesController {
@RequestMapping("/")
public String home() {
return "home";
}
}
@Controller
注解告诉 Spring 检查这个类中的RequestMappings
@RequestMapping
声明一个 URL 的路由,请求方法,请求头,参数等信息。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
SpringBoot
@Entity
注解这个类为实体类@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;
}
Rails: Model.find, Modle.create, Model.update, Model.delete SpringBoot:
public interface UserDao extends CrudRepository<User, Long> {
public User findByEmail(String email);
}
Rails: Model.where().order().limit() SpringBoot:
public interface CategoriesRepository
extends CrudRepository<Category, Long> {
List<Category> findAllByOrderByNameAsc();
}
Rails: has_many :comments SpringBoot: SpringBoot 需要生成对应的 setter 和 getter 方法
@OneToMany(targetEntity=App.class,
mappedBy="user",
fetch=FetchType.EAGER,
cascade=CascadeType.ALL)
private Set<App> apps;
public Set<App> getApps() {
return apps;
}
public void setApps(Set<App> apps) {
this.apps= apps;
}
Rails: rails s -e (development, test & production) SpringBoot:
application-development.propreties
application-test.propreties
application-production.propreties
mvn spring-boot:run -D
spring.profiles.active=development