当前页面: 开发资料首页 → Spring 专题 → hibernate+spring
摘要: hibernate+spring 本文是开发基于spring的web应用的入门文章,前端采用Struts MVC框架,中间层采用spring,后台采用Hibernate。 本文包含以下内容: ·配置H...
create table app_user(id number not null primary,firstname vchar(32),lastname vchar(32)); |
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping package="com.jandar.model"> <class name="User" table="APP_USER"> <id column="ID" name="id" type="integer" > <generator class="assigned" /> </id> <property column="LASTNAME" length="10" name="lastname" not-null="false" type="string" /> <property column="FIRSTNAME" length="10" name="firstname" not-null="true" type="string" /> </class> </hibernate-mapping> |
import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; public class BaseObject implements Serializable { public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } public boolean equals(Object o) { return EqualsBuilder.reflectionEquals(this, o); } public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } } public class User extends BaseObject { private Long id; private String firstName; private String lastName; /** * @return Returns the id. */ public Long getId() { return id; } /** * @param id The id to set. */ public void setId(Long id) { this.id = id; } /** * @return Returns the firstName. */ public String getFirstName() { return firstName; } /** * @param firstName The firstName to set. */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * @return Returns the lastName. */ public String getLastName() { return lastName; } /** * @param lastName The lastName to set. */ public void setLastName(String lastName) { this.lastName = lastName; } } |
package com.jandar.services.dao; public interface IDAO { } |
public interface IUserDAO extends DAO { List getUsers(); User getUser(Integer userid); void saveUser(User user); void removeUser(Integer id); } |
import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.orm.hibernate.support.HibernateDaoSupport; import com.jandar.model.User; import com.jandar.service.dao.IUserDAO; public class UserDaoHibernate extends HibernateDaoSupport implements IUserDAO { private Log log=LogFactory.getLog(UserDaoHibernate.class); /* (非 Javadoc) * @see com.jandar.dao.IUserDAO#getUsers() */ public List getUsers() { return getHibernateTemplate().find("from User"); } /* (非 Javadoc) * @see com.jandar.dao.IUserDAO#getUser(java.lang.Long) */ public User getUser(Integer id) { // TODO 自动生成方法存根 return (User) getHibernateTemplate().get(User.class,id); } /* (非 Javadoc) * @see com.jandar.dao.IUserDAO#saveUser(com.jandar.model.User) */ public void saveUser(User user) { log.debug("xxxxxxx"); System.out.println("yyyy"); getHibernateTemplate().saveOrUpdate(user); if(log.isDebugEnabled()) { log.debug("userId set to "+user.getId()); } } /* (非 Javadoc) * @see com.jandar.dao.IUserDAO#removeUser(java.lang.Long) */ public void removeUser(Integer id) { Object user=getHibernateTemplate().load(User.class,id); getHibernateTemplate().delete(user); if(log.isDebugEnabled()){ log.debug("del user "+id); } } } |