`
gordon20082008
  • 浏览: 25471 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

捣鼓ssh上手的例子---struts2+spring+hibernate3(二)

    博客分类:
  • SSH
阅读更多
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


	<bean id="test" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="111111"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="test" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>entity/User.hbm.xml</value>
				<value>entity/Accounts.hbm.xml</value></list>
		</property></bean>
	<bean id="personManage" class="dao.PersonManage" abstract="false"
            lazy-init="default" autowire="default" dependency-check="default">
         <property name="sessionFactory" >
         <ref local="sessionFactory"/>
   </property>
</bean>

<!--   配置LoginAction.java类文件 -->

<bean id="LoginAction" class="action.LoginAction" >

       <property name="personManage">
           <ref bean="personManage" />
       </property> 

</bean>
	<bean id="AccountsDAO" class="dao.AccountsDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean></beans>



<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.objectFactory" value="spring" />

<include file="struts-default.xml"></include>
<package name="default" extends="struts-default">

   <action name="loginAction" class="LoginAction" scope="request">
    <result name="fail">/login.jsp </result>
    <result name="success">/success.jsp</result>
    <result name="input">/login.jsp</result>
    <result name="userexist">/login.jsp?info=exist</result>
   </action>
</package>

</struts>


Accounts.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="entity.Accounts" table="accounts" catalog="test">
        <id name="id" type="java.lang.String">
            <column name="id" length="50" />
            <generator class="uuid"/>
        </id>
        <property name="userid" type="java.lang.String">
            <column name="userid" length="50" not-null="true" unique="true" />
        </property>
        <property name="account" type="java.lang.Float">
            <column name="account" precision="10" />
        </property>
    </class>
</hibernate-mapping>


AccountsDAO.java

package dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import entity.Accounts;

/**
 * Data access object (DAO) for domain model class Accounts.
 * 
 * @see entity.Accounts
 * @author MyEclipse Persistence Tools
 */

public class AccountsDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(AccountsDAO.class);

	// property constants
	public static final String USERID = "userid";

	public static final String ACCOUNT = "account";

	protected void initDao() {
		// do nothing
	}

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

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

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

	public List findByExample(Accounts instance) {
		log.debug("finding Accounts 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 Accounts instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Accounts 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 findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}

	public List findByAccount(Object account) {
		return findByProperty(ACCOUNT, account);
	}

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

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

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

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

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



主要包见附件图,有些不需要,比如hibernate-annotations.jar
  • 大小: 36.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics