java - SSH整合時 getHibernateTemplate()為空
滿天的星座
滿天的星座 2017-06-12 09:26:34
0
1
805
@Repository
public class AuthorAdminDaoImpl implements IAuthorAdminDao {
    @Resource
    private BaseDAO basedao;
public boolean loginAuthorAdmin(AuthorAdmin aa) {
        try {
            String queryString = " from AuthorAdmin  a where a.authorName= ? and a.authorPwd= ? ";
            basedao=new BaseDAO();
            if (basedao.getTemplate() == null) {
                System.out.println("没有获得HibernateTemplate?");
            }
            List<AuthorAdmin> find = (List<AuthorAdmin>) basedao.getTemplate().find(
                    queryString, aa.getAuthorUsername(), aa.getAuthorPwd());
            if (find != null && find.size() > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }


}
    }
public class BaseDAO extends HibernateDaoSupport{

    public HibernateTemplate getTemplate(){
        return getHibernateTemplate();
    }

}

HibernateTemplate為空不知道為什麼?用的是Spring注入

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


<!-- 激活组件扫描功能,在包gk.aop及其子包下面自动扫描通过注解配置的组件 -->
     <context:component-scan base-package="com.lcy" />
    <!-- 激活自动代理功能,false或者省略基于接口的代理,为true基于类的的代理 -->
    <aop:aspectj-autoproxy proxy-target-class="false"/> 

    <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/ssh"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></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>
                <prop key="hibernate.format_sql">
                    true
                </prop>
                <prop key="hibernate.show_sql">
                    true
                </prop>
                
            </props>
        </property>

        <property name="mappingResources">
            <list>
                <value>com/lcy/po/DocumentInfo.hbm.xml</value>
                <value>com/lcy/po/ColumnEditorAdmin.hbm.xml</value>
                <value>com/lcy/po/ExpertAdmin.hbm.xml</value>
                <value>com/lcy/po/AttachInfo.hbm.xml</value>
                <value>com/lcy/po/DocumentStateInfo.hbm.xml</value>
                <value>com/lcy/po/ColumnInfo.hbm.xml</value>
                <value>com/lcy/po/MessageInfo.hbm.xml</value>
                <value>com/lcy/po/MagazineEditorAdmin.hbm.xml</value>
                <value>com/lcy/po/BoardInfo.hbm.xml</value>
                <value>com/lcy/po/ExpertAssess.hbm.xml</value>
                <value>com/lcy/po/AuthorAdmin.hbm.xml</value>
            </list>
        </property>
    </bean>

    <bean id="basedao" class="com.lcy.dao.BaseDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory"></ref>
        </property>
    </bean>

</beans>
滿天的星座
滿天的星座

全部回覆(1)
大家讲道理

本來 Spring 已經幫你把 basedao 注入了你XML裡配置好的物件, 你後面又用

basedao = new BaseDAO();

創建了個新物件, 把那個 Spring 給注入的 bean 替換掉了...

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板