一、步骤
a. <!-- 数据源 --> <jee:jndi-lookup id="f3CenterDS" jndi-name="java:/comp/env/jdbc/f3Center" />
b.配置SessionFactory
<bean id="f3CenterSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="f3CenterDS"></property> <property name="annotatedClasses"> <list> <value>cn.f3.db.entity.LogonLog</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
c.用注解解决实体Bean(cn.f3.db.entity.LogonLog)
@javax.persistence.Entity@Entity(dynamicInsert=true) -- 允许默认值 @Table(name = "J_ZX_LogonLog")public class LogonLog implements Serializable { /** * */ private static final long serialVersionUID = 1L; /** * GUID */ private String guid; /** * 登录工号 */ private String id; /** * 登录IP */ private String ip; /** * 登录类型 */ private String type; /** * 登录时间 */ private Date writeTime; @Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "guid") @Column(name = "FGuid", unique = true, nullable = false, length = 50) --自动生成GUID public String getGuid() { return guid; } public void setGuid(String guid) { this.guid = guid; } @Column(name = "FID", nullable = false, length = 10) public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name = "FIP", nullable = false, length = 50) public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } @Column(name = "FType", nullable = false) public String getType() { return type; } public void setType(String type) { this.type = type; } @Temporal(TemporalType.DATE) @Column(name = "FWriteTime", length = 23) public Date getWriteTime() { return writeTime; } public void setWriteTime(Date writeTime) { this.writeTime = writeTime; }}
d、继承HibernateDaoSupport实现保存功能
public class LogonLogHibernateDao extends HibernateDaoSupport implements LogonLogDao { public void addLogonLog(LogonLog logonLog) { getHibernateTemplate().save(logonLog); }}
hibernate_annotations.pdf (447.1 KB)
相关资源:Struts2+Spring2.5+Hibernate3+Freemarker整合