新项目 用Spring MVC, 后台 要自动封装成对象的一些注意事项

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id = null; // 表id字段(mysql 自动增长字段)

@Column
private  String  name = null; //班级名称

// @Column
// private String content = null; // 上课时间(课程表)  ? 换成一个实体 CourseCalendar.class

/**
* 我是 伟大 的课程 安排 表
*/
@OneToMany(cascade = CascadeType.PERSIST /** 班级删了? 我擦, 关我飞机事 **/, fetch = FetchType.LAZY /** 懒加载 ?? **/,
mappedBy = "classes")
private List<CourseCalendar> courseCalendars = null; //课程安排

@Column
private Integer status = null; //状态

@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(name = "classes_course",
      joinColumns ={@JoinColumn(name = "classes_id", referencedColumnName = "id")},    
      inverseJoinColumns = {@JoinColumn(name = "course_id", referencedColumnName = "id")    
})
private List<Course> courses = null; //多对多  课程 和 班级 对应关系(这个班级要上的课程列表)

@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(name = "classes_teacher",
      joinColumns ={@JoinColumn(name = "classes_id", referencedColumnName = "id")},    
      inverseJoinColumns = {@JoinColumn(name = "teacher_id", referencedColumnName = "id")    
})
private List<Teacher> teachers = null; //多对多  班级和授课老师  对应关系

/**
* 我是伟大的学生列表
*/
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "classes")
private List<Student> students = null; //这个班的学员

@JsonSerialize(using = WiterJsonString.class)
@Temporal(TemporalType.TIMESTAMP) //时间戳, 精确到秒
private Date popened = null; //计划开班时间

@JsonSerialize(using = WiterJsonString.class)
@Temporal(TemporalType.TIMESTAMP) //时间戳, 精确到秒
private Date pclosed = null; //计划结业时间

@JsonSerialize(using = WiterJsonString.class)
@Temporal(TemporalType.TIMESTAMP) //时间戳, 精确到秒
private Date signend = null; //报名截至日期

@JsonSerialize(using = WiterJsonString.class)
@Temporal(TemporalType.TIMESTAMP) //时间戳, 精确到秒
private Date opened = null; //实际开班日期

@JsonSerialize(using = WiterJsonString.class)
@Temporal(TemporalType.TIMESTAMP) //时间戳, 精确到秒
private Date closed = null; //实际结业时间

@JsonSerialize(using = WiterJsonString.class)
@Temporal(TemporalType.TIMESTAMP) //时间戳, 精确到秒
private Date created = null; //创建时间

@JsonSerialize(using = WiterJsonString.class)
@Temporal(TemporalType.TIMESTAMP) //时间戳, 精确到秒
private Date updated = null; //改记录最后更新时间

时间要加 @Temporal @JsonSerialize(用于告诉jackjson 怎么输出时间)
如果用的jackjson 各种关系 要加好 注解,免得 报错为 无限递归
@JsonIgnore
对于 子实体中引用的父实体加上面的,不然无限递归

WiterJsonString.java  extends JsonSerializer<Date>{
    @Override
public void serialize(Date arg0, JsonGenerator arg1, SerializerProvider arg2)
throws IOException, JsonProcessingException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
arg1.writeString(df.format(arg0));
}
}



action层 要 让spring 自动 封装参数为实体对象:

比如:

@RequestMapping(method = RequestMethod.GET, value = "/listallbywhere")
public @ResponseBody ResultVo listAllByWhere(@ModelAttribute Classes obj){
ResultVo re = new ResultVo();
List<Classes> c = dao.getAll(Classes.class, ModelReflectTools.createNotNull(obj, true));
re.setData(c);
re.setSuccess(c != null);
re.setTotal((re.isSuccess()) ? c.size() : 0);
re.setMsg((re.isSuccess()) ? "操作成功" : "操作失败");
return re;
}
必须加下面:
/**********************                   Spring mvc 参数自动封装 的 一些蛋疼步骤           ******************************/
@InitBinder 
    public void initBinder(WebDataBinder binder) {  
        // 忽略字段绑定异常  
        // binder.setIgnoreInvalidFields(true);  
 
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd mm:dd:ss");  
        binder.registerCustomEditor(Date.class, "popened",  
                new CustomDateEditor(format, true));
        binder.registerCustomEditor(Date.class, "pclosed",  
                new CustomDateEditor(format, true));
        binder.registerCustomEditor(Date.class, "signend",  
                new CustomDateEditor(format, true));
        binder.registerCustomEditor(Date.class, "opened",  
                new CustomDateEditor(format, true));
        binder.registerCustomEditor(Date.class, "closed",  
                new CustomDateEditor(format, true));
        binder.registerCustomEditor(Date.class, "updated",  
                new CustomDateEditor(format, true));
        binder.registerCustomEditor(Date.class, "created",  
                new CustomDateEditor(format, true));
    }
你懂得.不解释.


如果你的dao的某个方法要 操作数据库, 必须加上
@Transactional注解

你可能感兴趣的:(spring,mvc)