67,513
社区成员
发帖
与我相关
我的任务
分享
public class UserAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Item> items=null;
private ItemDAO itemDAO ;
public void setItemDAO(ItemDAO itemDAO){
this.itemDAO=itemDAO;
}
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
items = itemDAO.findAll();
return SUCCESS;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
package com.lee.dao;
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;
import com.lee.model.Item;
/**
* A data access object (DAO) providing persistence and search support for Item
* 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.lee.model.Item
* @author MyEclipse Persistence Tools
*/
public class ItemDAO extends HibernateDaoSupport {
private static final Logger log = LoggerFactory.getLogger(ItemDAO.class);
// property constants
public static final String ITEM_NAME = "itemName";
public static final String SOURCE = "source";
public static final String YEAR = "year";
public static final String FUND = "fund";
public static final String LEADER = "leader";
protected void initDao() {
// do nothing
}
public void save(Item transientInstance) {
log.debug("saving Item instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Item persistentInstance) {
log.debug("deleting Item instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Item findById(java.lang.Integer id) {
log.debug("getting Item instance with id: " + id);
try {
Item instance = (Item) getHibernateTemplate().get(
"com.lee.model.Item", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Item instance) {
log.debug("finding Item 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 Item instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Item 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 findByItemName(Object itemName) {
return findByProperty(ITEM_NAME, itemName);
}
public List findBySource(Object source) {
return findByProperty(SOURCE, source);
}
public List findByYear(Object year) {
return findByProperty(YEAR, year);
}
public List findByFund(Object fund) {
return findByProperty(FUND, fund);
}
public List findByLeader(Object leader) {
return findByProperty(LEADER, leader);
}
public List findAll() {
log.debug("finding all Item instances");
try {
String queryString = "from Item";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Item merge(Item detachedInstance) {
log.debug("merging Item instance");
try {
Item result = (Item) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Item instance) {
log.debug("attaching dirty Item instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Item instance) {
log.debug("attaching clean Item instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static ItemDAO getFromApplicationContext(ApplicationContext ctx) {
return (ItemDAO) ctx.getBean("ItemDAO");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/data"></property>
<property name="username" value="root"></property>
<property name="password" value="madge881227"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/lee/model/Item.hbm.xml</value></list>
</property></bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="ItemDAO" class="com.lee.dao.ItemDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="UserAction" class="com.lee.action.UserAction">
<property name="itemDAO" ref="ItemDAO"></property>
</bean>
</beans>