人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的Struts+Spring+Hibernate快速入门懂设计的网友们快点来了解吧!
【 tulaoshi.com - 编程语言 】
本文是开发基于spring的web应用的入门文章,前端采用Struts MVC框架,中间层采用spring,后台采用Hibernate。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访问、操作对象,进而实现对数据库的操作。
来源:http://www.tulaoshi.com/n/20160219/1599448.html
看过《Struts+Spring+Hibernate快速入门》的人还看了以下文章 更多>>