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;
}
}
創建DAO訪問對象
1. 在src/com.jandar.service.dao新建IDAO.java接口,所有的DAO都繼承該接口
package com.jandar.services.dao;
public interface IDAO {
}
2. 在src/com.jandar.service.dao下新建IUserDAO.java接口 public interface IUserDAO extends DAO {
List getUsers();
User getUser(Integer userid);
void saveUser(User user);
void removeUser(Integer id);
}
該接口提供了訪問對象的方法,
3. 在src/com.jandar.service.dao.hibernate下新建UserDAOHiberante.javaimport 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);
}
}
}
在這個類中實現了IUserDAO接口的方法,並且繼承了HibernateDAOSupport類。這個類的作用是通過hibernate訪問、操作對象,進而實現對數據庫的操作。