package com.foraise.dao.impl;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 com.foraise.entity.Users;/**
 * A data access object (DAO) providing persistence and search support for Users
 * 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.foraise.entity.Users
 * @author MyEclipse Persistence Tools
 */public class UsersDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UsersDAO.class);
// property constants
public static final String UNAME = "uname";
public static final String UPWD = "upwd"; protected void initDao() {
// do nothing
} public void save(Users transientInstance) {
log.debug("saving Users instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} public void delete(Users persistentInstance) {
log.debug("deleting Users instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} public Users findById(java.lang.Integer id) {
log.debug("getting Users instance with id: " + id);
try {
Users instance = (Users) getHibernateTemplate().get(
"com.foraise.entity.Users", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} public List findByExample(Users instance) {
log.debug("finding Users 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 Users instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Users 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 findByUname(Object uname) {
return findByProperty(UNAME, uname);
} public List findByUpwd(Object upwd) {
return findByProperty(UPWD, upwd);
} public List findAll() {
log.debug("finding all Users instances");
try {
String queryString = "from Users";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} public Users merge(Users detachedInstance) {
log.debug("merging Users instance");
try {
Users result = (Users) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Users instance) {
log.debug("attaching dirty Users instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public void attachClean(Users instance) {
log.debug("attaching clean Users instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static UsersDAO getFromApplicationContext(ApplicationContext ctx) {
return (UsersDAO) ctx.getBean("UsersDAO");
}
}

解决方案 »

  1.   

    貌似好难理解啊
    public void save(Users transientInstance) {
            log.debug("saving Users instance");
            try {
                getHibernateTemplate().save(transientInstance);
                log.debug("save successful");
            } catch (RuntimeException re) {
                log.error("save failed", re);
                throw re;
            }
        }
    这完全看不到SQL的身影,是怎么保存这个实体的啊?
      

  2.   


    这个用的是Hibernate来保存的。Hibernate操作的就是对象!!!
      

  3.   

    我现在是只要调底层的dao,进去就报错。能帮忙调下吗,我从昨天调到今天都没搞定.....
      

  4.   

    自己顶一下!
    action部分:public String login() throws Exception{
    System.out.print(users.getUname());
    System.out.print(users.getUpwd());
    try {
    Users loginUser=loginservice.login(users.getUname(),users.getUpwd());
    if(loginUser!=null){
    ActionContext context=ActionContext.getContext();
    context.getSession().put("LOGINUSER", loginUser);
    return SUCCESS;
    }
    } catch (Exception e) {
    log.error("登录查询失败", e);
    return ERROR;
    }

    return INPUT;
    }service层:public Users login(String uname,String upass){
    List list=usersDAO.findByUname(uname);

    if(list==null ||list.size()==0){
    return null;
    }
    Users users=(Users)usersDAO.findByUname(uname).get(0);

    if(users==null){
    return null;
    }

    if(uname.equals(users.getUname())&&upass.equals(users.getUpwd())){
    return users;
    }else{
    return null;
    }
    }进了这个方法就直接到action了public List findByUname(Object uname) {
    return findByProperty(UNAME, uname);
    }
      

  5.   


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

  6.   

    可以使用的,但是,注意事务的开启,我之前没有用Spring的时候 就是用的Myeclipse逆向生成的DAO
      

  7.   

    来这里回吧:
    http://topic.csdn.net/u/20120720/14/1e38a9ee-5c07-411d-91d3-0b0a6550b383.html?seed=500912197&r=79182477#r_79182477
      

  8.   

    粘下spring的配置文件信息,应该是配置的问题。
      

  9.   


    <?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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/redwooddb">
            </property>
            <property name="username" value="root"></property>
            <property name="password" value="123654"></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/foraise/entity/Furnituretype.hbm.xml</value>
                    <value>com/foraise/entity/Redwoodparameter.hbm.xml</value>
                    <value>com/foraise/entity/Images.hbm.xml</value>
                    <value>com/foraise/entity/News.hbm.xml</value>
                    <value>com/foraise/entity/Users.hbm.xml</value></list>
            </property></bean>
        <bean id="FurnituretypeDao"
            class="com.foraise.dao.impl.FurnituretypeDao">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>
        <bean id="RedwoodparameterDao"
            class="com.foraise.dao.impl.RedwoodparameterDao">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>
        <bean id="ImagesDao" class="com.foraise.dao.impl.ImagesDao">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>
        <bean id="NewsDao" class="com.foraise.dao.impl.NewsDao">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>
        <bean id="UsersDao" class="com.foraise.dao.impl.UsersDao">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>
        <bean id="LoginService" class="com.foraise.service.impl.LoginService">
            <property name="usersDao" ref="UsersDao"/>
        </bean>
        <bean id="LoginAction" class="com.foraise.action.LoginAction">
            <property name="loginservice" ref="LoginService"/>
        </bean>
        
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>       
        </bean>
        
        <!--配置事务的传播特性-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
          <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
          </tx:attributes>
        </tx:advice>
        
        <!-- 哪些类的哪些方法参与事务 -->
        <aop:config>
            <aop:pointcut id="allServiceMethod" expression="execution(* com.foraise.service.impl.*.*(..))"/>
            <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice"/>
        </aop:config>
        
        </beans>
      

  10.   

    <bean id="UsersDao" class="com.foraise.dao.impl.UsersDao">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>id="UsersDao"   改成小写  id="usersDao" 再试试。