spring 3 jdbcTemplate 总是null~~~
webmvc正常。只是dao层方面。我只写了一个类,没有接口,只是想做一下简单连接。代码如下:@RepositorypublicclassTestDaoImp{priva...
web mvc 正常。只是dao层方面。我只写了一个类,没有接口,只是想做一下简单连接。代码如下:
@Repository
public class TestDaoImp {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(@Qualifier(value="dataSource") DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void query() {
// TODO Auto-generated method stub
jdbcTemplate.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。。。不知道为什么。。
我是不是漏掉什么配置了??? 展开
@Repository
public class TestDaoImp {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(@Qualifier(value="dataSource") DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void query() {
// TODO Auto-generated method stub
jdbcTemplate.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。。。不知道为什么。。
我是不是漏掉什么配置了??? 展开
展开全部
你没有配置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>
可以参考一下
<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>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
配置方面貌似没漏什么
会不会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);
}
配置方面貌似没漏什么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
The Spring Festival is a traditional Chinese Festival. People living far away all go back home to have a get-together.Most families make delicious food to enjoy themselves. What's more,people buy new clothes and shoes for the children as well as gifts for the elderly, friends and relatives.On Spring Festival Eve,all family members eat dinner together.The traditional food is jiaozi.Waking up on New Year, everybody dresses up. First they extend greetings to their parents. Then each child will get money as a New Year gift, wrapped up in red paper.People are all excited during the Spring Festival.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Debug 跟踪一下, 是不是jdbcTemplate没有值
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询