使用Spring的jdbcTemplate进一步简化JDBC操作

☆樱花仙子☆ 提交于 2019-12-20 01:29:17

spring 第一篇(1-1):让java开发变得更简单(下)

这个波主虽然只发了几篇,但是写的很好

上面一篇文章写的很好,其中提及到了Spring的jdbcTemplate,templet方式我之前已经有点了解了,但是Spring的还不知道,这次真的又学到了Spring的

使用Spring的jdbcTemplate进一步简化JDBC操作

 配置文件

Spring mvc中jdbcDaoSupport和jdbcTemplate的使用

 


  1. package xm.zjl.dao;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.jdbc.core.JdbcTemplate;  
  8. import org.springframework.stereotype.Repository;  
  9.   
  10. /** 
  11.  * 测试标签dao 
  12.  *  
  13.  * @author zjl 
  14.  * 
  15.  */  
  16. @Repository  
  17. public class TagTestDao{  
  18.     @Autowired  
  19.     private JdbcTemplate jdbcTemplate;  
  20.       
  21.       
  22.     public List getList(){  
  23.         List list = new ArrayList();  
  24.         String sql = "select * from user";  
  25.         jdbcTemplate.execute(sql);  
  26.         return list;  
  27.     }  
  28.   
  29. }  

2.配置文件:

  1. <beans xmlns="http://www.springframework.org/schema/beans"    
  2.  xmlns:context="http://www.springframework.org/schema/context"    
  3.  xmlns:p="http://www.springframework.org/schema/p"    
  4.  xmlns:mvc="http://www.springframework.org/schema/mvc"    
  5.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  6.  xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.       http://www.springframework.org/schema/context    
  9.       http://www.springframework.org/schema/context/spring-context.xsd    
  10.       http://www.springframework.org/schema/mvc    
  11.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">    
  12.      <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->    
  13.      <mvc:annotation-driven />    
  14.      <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->    
  15.      <context:component-scan base-package="xm.zjl.*" />    
  16.      <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->    
  17.      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />  
  18.       
  19.      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  20.          <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  21.          <property name="url" value="jdbc:mysql://localhost:3306/mydata"></property>  
  22.          <property name="username" value="root"></property>  
  23.          <property name="password" value="root"></property>  
  24.      </bean>  
  25.        
  26.       
  27.      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  28.         <property name="dataSource" ref="dataSource"/>  
  29.      </bean>  
  30.           
  31.       
  32. </beans>    

2.继承JdbcDaoSupport类,但是dataSource没有设置成功(注解方式),若果有方法请留言,多谢

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!