Spring mvc中jdbcDaoSupport和jdbcTemplate的使用

1.注解使用JdbcTemplate:


package xm.zjl.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * 测试标签dao
 * 
 * @author zjl
 *
 */
@Repository
public class TagTestDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	public List getList(){
		List list = new ArrayList();
		String sql = "select * from user";
		jdbcTemplate.execute(sql);
		return list;
	}

}
2.配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
     <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->  
     <mvc:annotation-driven />  
     <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->  
     <context:component-scan base-package="xm.zjl.*" />  
     <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->  
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
    
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     	 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
     	 <property name="url" value="jdbc:mysql://localhost:3306/mydata"></property>
     	 <property name="username" value="root"></property>
     	 <property name="password" value="root"></property>
     </bean>
     
    
     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     	<property name="dataSource" ref="dataSource"/>
     </bean>
     	
    
</beans>  
2.继承JdbcDaoSupport类,但是dataSource没有设置成功(注解方式),若果有方法请留言,多谢



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值