您的位置:首页 > 编程语言 > Java开发

Struts 2 + Hibernate + Spring 开发环境的搭建与数据访问Dao的生成

2017-09-26 20:15 501 查看
      #SSH环境框架的搭建,网上也有很多教程,但与个人操作的出入很多,这里也做一篇属于个人的教程.教程如图所示:

      1.搭建Spring框架







  2.搭建Hibernate框架













3.搭建Struts 2框架





4.至此,SSH三大开发框架环境顺利搭建



5.但该框架有很多敏捷开发的环境还没有搭建,如反向工程建立数据模型类和数据访问层Dao模型类,可以不用写SQL语句

   # 进入MyEclipse Database Explorer视图窗口

















生成的代码如下:

User.java

package com.sunline.entity;

/**
* User entity. @author MyEclipse Persistence Tools
*/

public class User implements java.io.Serializable {

// Fields

private Integer userId;
private String userName;
private String userPassword;
private String userStatus;

// Constructors

/** default constructor */
public User() {
}

/** full constructor */
public User(String userName, String userPassword, String userStatus) {
this.userName = userName;
this.userPassword = userPassword;
this.userStatus = userStatus;
}

// Property accessors

public Integer getUserId() {
return this.userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPassword() {
return this.userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public String getUserStatus() {
return this.userStatus;
}

public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}

}


User.hbm.xml

<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.sunline.entity.User" table="user" catalog="hengfeng">
<id name="userId" type="java.lang.Integer">
<column name="user_id" />
<generator class="native"></generator>
</id>
<property name="userName" type="java.lang.String">
<column name="user_name" length="40" not-null="true" />
</property>
<property name="userPassword" type="java.lang.String">
<column name="user_password" length="30" not-null="true" />
</property>
<property name="userStatus" type="java.lang.String">
<column name="user_status" length="30" not-null="true" />
</property>
</class>
</hibernate-mapping>


UserDAO.java(自动生成的数据操作类,不用自己写SQL语句啦)

package com.sunline.entity;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
* A data access object (DAO) providing persistence and search support for User
* entities. Transaction control of the save(), update() and delete() operations
* can directly support Spring container-managed transactions or they can be
* augmented to handle user-managed Spring transactions. Each of these methods
* provides additional information for how to configure it for the desired type
* of transaction control.
*
* @see com.sunline.entity.User
* @author MyEclipse Persistence Tools
*/

public class UserDAO extends HibernateDaoSupport {
private static final Logger log = LoggerFactory.getLogger(UserDAO.class);
// property constants
public static final String USER_NAME = "userName";
public static final String USER_PASSWORD = "userPassword";
public static final String USER_STATUS = "userStatus";

protected void initDao() {
// do nothing
}

public void save(User transientInstance) {
log.debug("saving User instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(User persistentInstance) {
log.debug("deleting User instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public User findById(java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getHibernateTemplate().get(
"com.sunline.entity.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public List findByExample(User instance) {
log.debug("finding User instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding User instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from User as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

public List findByUserName(Object userName) {
return findByProperty(USER_NAME, userName);
}

public List findByUserPassword(Object userPassword) {
return findByProperty(USER_PASSWORD, userPassword);
}

public List findByUserStatus(Object userStatus) {
return findByProperty(USER_STATUS, userStatus);
}

public List findAll() {
log.debug("finding all User instances");
try {
String queryString = "from User";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(User instance) {
log.debug("attaching dirty User instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(User instance) {
log.debug("attaching clean User instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static UserDAO getFromApplicationContext(ApplicationContext ctx) {
return (UserDAO) ctx.getBean("UserDAO");
}
}


总结:

 框架就是敏捷开发,迅速上手项目的一种工具,有了这些框架,可以大大缩短开发的周期.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐