日常springMVC项目中使用hibernateBean还是很便捷,不推荐查询,但保存单个对象还是很方便的; so,项目内集成hibernates还是可推荐的; 但也别太重, 比如我们集成.xml映射方式,而是简单的注解,然后通过this.getHibernateTemplate().getSessionFactory().getCurrentSession() , session工厂来更新or保存对象
好了,不多少举个栗子;
import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import javax.persistence.Transient;import org.springframework.format.annotation.DateTimeFormat;@Entity@Table(name="demo_table")public class DemoTable implements java.io.Serializable{ @Transient private static final long serialVersionUID = -2593493866782904614L; private long id; private String name; private int age; @DateTimeFormat(pattern="yyyy-MM-dd") private Date createDate; private String email; private String phone; private String url; private String xb; public DemoTable(){ } @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false) public long getId() { return id; } public void setId(long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="age") public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Column(name="create_date") public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } // 忽略某个字段 @Transient public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name="phone") public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Column(name="url") public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } @Column(name="xb") public String getXb() { return xb; } public void setXb(String xb) { this.xb = xb; } }
其中:
@Table(name="demo_table") //映射类名和表名对应
@Column(name="id") //映射属性名和字段名
@Column(length=50) //限制字符串长度 (这里没有写,如果严格限制可以写上)
@Temporal(TemporalType.DATE) //限制时间格式:只记录日期不记录时间。默认是时间日期都记录
//隐藏属性,不会被映射到表中 ,忽略单个属性
@Enumerated(EnumType.STRING) //把枚举里的sring存储进去
@Enumerated(EnumType.ORDINAL) //把存在的顺序编号int存储进去
@GeneratedValue // 自增主键标识,可以定义该标识符的生成策略 (我们默认就好)
好了,今天就介绍到这里了,要想深入的话,可以@题主,或者回复本贴;