使用Spring Roo ,感受ROR式的开发
Roo是一种 Spring 开发的辅助工具,使用命令行操作来生成自动化项目,操作非常类似于rails
我这里使用spring tool suit来开发一个demo项目
首先新建个spring template project,然后选择template类型为Roo Wep App (based on Roo 1.0.0.RC1)
这样,便会通过roo的命令自动生成一个标准的maven web项目,里面含有最基本的Spring 配置
建好项目后,我们可以在project上运行roo shell
通过hint命令,我们可以很快的了解各种操作提示
首先,设置数据库
install jpa -provider HIBERNATE -database MYSQL
new persistent class jpa -name ~.domain.Employee -testAutomatically
add field string -fieldName name -notNull -sizeMax 200add field date jpa -fieldName birth -type java.util.Date......
@Entity@RooEntity@RooJavaBean@RooToStringpublic class Employee { @NotNull @Size(max = 200) private String name; @Temporal(TemporalType.TIMESTAMP) private Date birth;}
// Decompiled by DJ v3.5.5.77 Copyright 2003 Atanas Neshkov Date: 2009-8-8 21:37:43// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!// Decompiler options: packimports(3) // Source File Name: Employee.javapackage com.javaeye.domain;import java.util.Date;import java.util.List;import javax.persistence.EntityManager;import org.aspectj.runtime.reflect.Factory;import org.springframework.beans.factory.annotation.Configurable;import org.springframework.beans.factory.aspectj.*;// Referenced classes of package com.javaeye.domain:// Employee_Roo_ToString, Employee_Roo_Entity, Employee_Roo_JavaBeanpublic class Employee implements ConfigurableObject{ public Employee() { org.aspectj.lang.JoinPoint joinpoint1 = Factory.makeJP(ajc$tjp_1, this, this); org.aspectj.lang.JoinPoint joinpoint = Factory.makeJP(ajc$tjp_0, this, this); if(this != null && getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) && AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable))) AnnotationBeanConfigurerAspect.aspectOf().ajc$before$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$1$e854fa65(this); if(this != null && getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) && (this == null || !getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) || !AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable))) && AbstractDependencyInjectionAspect.ajc$if_0(joinpoint)) AnnotationBeanConfigurerAspect.aspectOf().ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(this); Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$entityManager(this); Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$id(this); Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$version(this); if(!AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable)) && AbstractDependencyInjectionAspect.ajc$if_0(joinpoint1)) AnnotationBeanConfigurerAspect.aspectOf().ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(this); } public static String ajc$privFieldGet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$name(Employee employee) { return employee.name; } public static void ajc$privFieldSet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$name(Employee employee, String s) { employee.name = s; } public static Date ajc$privFieldGet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$birth(Employee employee) { return employee.birth; } public static void ajc$privFieldSet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$birth(Employee employee, Date date) { employee.birth = date; } public static long countEmployees() { return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$countEmployees(); } public static EntityManager entityManager() { return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$entityManager(); } public static List findAllEmployees() { return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findAllEmployees(); } public static Employee findEmployee(Long long1) { return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findEmployee(long1); } public static List findEmployeeEntries(int i, int j) { return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findEmployeeEntries(i, j); } public void flush() {Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$flush(this); } public Date getBirth() { return Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$getBirth(this); } public Long getId() { return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$getId(this); } public String getName() { return Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$getName(this); } public Integer getVersion() { return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$getVersion(this); } public void merge() { Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$merge(this); } public void persist() { Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$persist(this); } public void remove() { Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$remove(this); } public void setBirth(Date date) { Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$setBirth(this, date); } public void setId(Long long1) { Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$setId(this, long1); } public void setName(String s) { Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$setName(this, s); } public void setVersion(Integer integer) { Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$setVersion(this, integer); } public String toString() { return Employee_Roo_ToString.ajc$interMethod$com_javaeye_domain_Employee_Roo_ToString$com_javaeye_domain_Employee$toString(this); } private String name; private Date birth; public transient EntityManager ajc$interField$com_javaeye_domain$entityManager; public Long ajc$interField$com_javaeye_domain_Employee_Roo_Entity$id; public Integer ajc$interField$com_javaeye_domain_Employee_Roo_Entity$version; private static final org.aspectj.lang.JoinPoint.StaticPart ajc$tjp_0; /* synthetic field */ private static final org.aspectj.lang.JoinPoint.StaticPart ajc$tjp_1; /* synthetic field */ static { Factory factory = new Factory("Employee.java", Class.forName("com.javaeye.domain.Employee")); ajc$tjp_0 = factory.makeSJP("initialization", factory.makeConstructorSig("1", "org.springframework.beans.factory.aspectj.ConfigurableObject", "", "", ""), 17); ajc$tjp_1 = factory.makeSJP("initialization", factory.makeConstructorSig("1", "com.javaeye.domain.Employee", "", "", ""), 17); }}
new controller automatic -formBackingObject ~.domain.Employee -name ~.web.CommentController
@RooWebScaffold(automaticallyMaintainView = true, formBackingObject = Employee.class)@RequestMapping("/employee/**")@Controllerpublic class EmployeeController {}
package com.javaeye.web;privileged aspect EmployeeController_Roo_Controller { @org.springframework.web.bind.annotation.RequestMapping(value = "/employee", method = org.springframework.web.bind.annotation.RequestMethod.POST) public java.lang.String EmployeeController.create(@org.springframework.web.bind.annotation.ModelAttribute("employee") com.javaeye.domain.Employee employee, org.springframework.validation.BindingResult result) { if (employee == null) throw new IllegalArgumentException("A employee is required"); for(javax.validation.ConstraintViolation<com.javaeye.domain.Employee> constraint : javax.validation.Validation.buildDefaultValidatorFactory().getValidator().validate(employee)) { result.rejectValue(constraint.getPropertyPath(), null, constraint.getMessage()); } if (result.hasErrors()) { return "employee/create"; } employee.persist(); return "redirect:/employee/" + employee.getId(); } @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/form", method = org.springframework.web.bind.annotation.RequestMethod.GET) public java.lang.String EmployeeController.createForm(org.springframework.ui.ModelMap modelMap) { modelMap.addAttribute("employee", new com.javaeye.domain.Employee()); return "employee/create"; } @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}", method = org.springframework.web.bind.annotation.RequestMethod.GET) public java.lang.String EmployeeController.show(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id, org.springframework.ui.ModelMap modelMap) { if (id == null) throw new IllegalArgumentException("An Identifier is required"); modelMap.addAttribute("employee", com.javaeye.domain.Employee.findEmployee(id)); return "employee/show"; } @org.springframework.web.bind.annotation.RequestMapping(value = "/employee", method = org.springframework.web.bind.annotation.RequestMethod.GET) public java.lang.String EmployeeController.list(org.springframework.ui.ModelMap modelMap) { modelMap.addAttribute("employees", com.javaeye.domain.Employee.findAllEmployees()); return "employee/list"; } @org.springframework.web.bind.annotation.RequestMapping(method = org.springframework.web.bind.annotation.RequestMethod.PUT) public java.lang.String EmployeeController.update(@org.springframework.web.bind.annotation.ModelAttribute("employee") com.javaeye.domain.Employee employee, org.springframework.validation.BindingResult result) { if (employee == null) throw new IllegalArgumentException("A employee is required"); for(javax.validation.ConstraintViolation<com.javaeye.domain.Employee> constraint : javax.validation.Validation.buildDefaultValidatorFactory().getValidator().validate(employee)) { result.rejectValue(constraint.getPropertyPath(), null, constraint.getMessage()); } if (result.hasErrors()) { return "employee/update"; } employee.merge(); return "redirect:/employee/" + employee.getId(); } @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}/form", method = org.springframework.web.bind.annotation.RequestMethod.GET) public java.lang.String EmployeeController.updateForm(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id, org.springframework.ui.ModelMap modelMap) { if (id == null) throw new IllegalArgumentException("An Identifier is required"); modelMap.addAttribute("employee", com.javaeye.domain.Employee.findEmployee(id)); return "employee/update"; } @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}", method = org.springframework.web.bind.annotation.RequestMethod.DELETE) public java.lang.String EmployeeController.delete(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id) { if (id == null) throw new IllegalArgumentException("An Identifier is required"); com.javaeye.domain.Employee.findEmployee(id).remove(); return "redirect:/employee"; } @org.springframework.web.bind.annotation.InitBinder public void EmployeeController.initBinder(org.springframework.web.bind.WebDataBinder binder) { binder.registerCustomEditor(java.util.Date.class, new org.springframework.beans.propertyeditors.CustomDateEditor(new java.text.SimpleDateFormat("yyyy-M-d"), false)); } }