亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問為什么使用spring 3 jdbcTemplate 卻總是null?

請問為什么使用spring 3 jdbcTemplate 卻總是null?

斯蒂芬大帝 2022-01-05 20:07:49
web mvc 正常。只是dao層方面。我只寫了一個類,沒有接口,只是想做一下簡單連接。代碼如下:@Repositorypublic class TestDaoImp {private JdbcTemplate jdbcTemplate;@Autowiredpublic void setDataSource(@Qualifier(value="dataSource") DataSource dataSource) {this.jdbcTemplate = new JdbcTemplate(dataSource);}public void query() {// TODO Auto-generated method stubjdbcTemplate.queryForInt("select count(*) from bar_ip");}}然后是xml配置:<annotation-driven /><context:component-scan base-package="sdf.dsf" /><beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><beans:property name="driverClassName"><beans:value>com.mysql.jdbc.Driver</beans:value></beans:property><beans:property name="url"><beans:value>jdbc:mysql://localhost:3306/bar</beans:value></beans:property><beans:property name="username"><beans:value>root</beans:value></beans:property><beans:property name="password"><beans:value>125202505</beans:value></beans:property></beans:bean>在TestDaoImp 的query()方法中的jdbcTemplate總是為null。。。不知道為什么。。我是不是漏掉什么配置了???
查看完整描述

2 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

jdbcTemplate報空指針?
會不會setDataSource()沒執行,檢驗一下
再確定<context:component-scan base-package="sdf.dsf" />搜索到了TestDaoImp 這個類
或者換換寫法試試看吧
public void setDataSource(@Qualifier(value="dataSource") DataSource dataSource) {
this.dataSource=dataSource;
newJdbcTemplate();
}
public void newJdbcTemplate()
{
this.jdbcTemplate = new JdbcTemplate(this.dataSource);
}
配置方面貌似沒漏什么

 


查看完整回答
反對 回復 2022-01-10
?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

你沒有配置sessionFactory
可以參考一下
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
<property name="packagesToScan" value="com.idos.entity" />
</bean>


查看完整回答
反對 回復 2022-01-10
  • 2 回答
  • 0 關注
  • 507 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號