java - SSH整合时 getHibernateTemplate()为空
问题描述
@Repositorypublic 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.xsdhttp://www.springframework.org/schema/contexthttp://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- /> <bean 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 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 class='com.lcy.dao.BaseDAO'><property name='sessionFactory'> <ref bean='sessionFactory'></ref></property> </bean></beans>
问题解答
回答1:本来 Spring 已经帮你把 basedao 注入了你XML里配置好的对象, 你后面又用
basedao = new BaseDAO();
创建了个新对象, 把那个 Spring 给注入的 bean 替换掉了...
相关文章:
1. java - Spring boot启动时报错?2. angular.js - 有没有不需要先git clone xxxx的angular2的教程?3. php - C语言算法题-开灯问题 代码有点看不懂求大神指点?4. java - 当在子类中声明一个父类中存在的变量后,自动调用的父类构造函数不起作用。5. docker-remote-api - docker 有哪些Web管理UI6. 用Java写发送邮件的程序,经常被当做垃圾邮件处理怎么解决7. a标签将 display 设置为 block 后,其内部内容无法将a标签宽度撑开8. node.js - mongodb启动的问题9. android - recyclerview显示错乱10. angular.js - 报这个错是什么原因呢?没有显示,因为报错,可是controller里没有这个错