1.DAO接口 UserDAO
package com.yanek.demo.ssh.dao;
import java.util.List;
import com.yanek.demo.ssh.vo.User;
public interface UserDAO {
public void save(User user);
public List<User> findAll();
}
2.DAO接口实现 UserDAOImpl
package com.yanek.demo.ssh.dao;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.yanek.demo.ssh.vo.User;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
public void save(User user) {
this.getHibernateTemplate().save(user);
}
public List<User> findAll()
{
try {
String queryString = "from User";
Query queryObject = getHibernateTemplate().getSessionFactory()
.openSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
throw re;
}
}
}
3. vo: User
package com.yanek.demo.ssh.vo;
public class User implements java.io.Serializable {
private int id;
private String username;
private String password;
private String email;
public User() {
}
public User(int id, String username, String password, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
4. 配置文件: User.hbm.xm
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yanek.demo.ssh.vo.User" table="t_user" schema="dbo"
catalog="ssh" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="50" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="50" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="50" />
</property>
</class>
</hibernate-mapping>