• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
鼎盛工作室
每天提高一点点,每天积累一点点,每天一点进步,有目标有计划的奋斗一生,每天追逐梦想,软件人生,人生软件。
博客园    首页    新随笔    联系   管理    订阅  订阅
Spring:使用JdbcTemplate的简单实例-基于注释

1. web.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  8.   
  9.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  10.   
  11.     <welcome-file-list>  
  12.   
  13.        <welcome-file>index.jsp</welcome-file>  
  14.   
  15.     </welcome-file-list>  
  16.   
  17.     <servlet>  
  18.   
  19.        <servlet-name>Dispatcher</servlet-name>  
  20.   
  21.        <servlet-class>  
  22.   
  23.            org.springframework.web.servlet.DispatcherServlet   
  24.   
  25.        </servlet-class>  
  26.   
  27.        <init-param>  
  28.   
  29.            <param-name>contextConfigLocation</param-name>  
  30.   
  31.            <param-value>/WEB-INF/applicationContext.xml</param-value>  
  32.   
  33.        </init-param>  
  34.   
  35.     </servlet>  
  36.   
  37.     <servlet-mapping>  
  38.   
  39.        <servlet-name>Dispatcher</servlet-name>  
  40.   
  41.        <url-pattern>*.html</url-pattern>  
  42.   
  43.     </servlet-mapping>  
  44.   
  45.     <filter>  
  46.   
  47.        <filter-name>SetCharacterEncoding</filter-name>  
  48.   
  49.        <filter-class>  
  50.   
  51.            com.lz.filter.SetCharacterEncodingFilter   
  52.   
  53.        </filter-class>  
  54.   
  55.        <init-param>  
  56.   
  57.            <param-name>encoding</param-name>  
  58.   
  59.            <param-value>utf-8</param-value>  
  60.   
  61.        </init-param>  
  62.   
  63.     </filter>  
  64.   
  65.     <filter-mapping>  
  66.   
  67.        <filter-name>SetCharacterEncoding</filter-name>  
  68.   
  69.        <url-pattern>/*</url-pattern>  
  70.   
  71.     </filter-mapping>  
  72.   
  73. </web-app>  

 

2. applicationContext.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xmlns:tx="http://www.springframework.org/schema/tx"  
  8.   
  9.     xmlns:jee="http://www.springframework.org/schema/jee"  
  10.   
  11.     xmlns:context="http://www.springframework.org/schema/context"  
  12.   
  13.     xmlns:aop="http://www.springframework.org/schema/aop"  
  14.   
  15.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  16.   
  17.                      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  18.   
  19.                      http://www.springframework.org/schema/tx    
  20.   
  21.                      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd    
  22.   
  23.                      http://www.springframework.org/schema/jee    
  24.   
  25.                      http://www.springframework.org/schema/jee/spring-jee-2.5.xsd    
  26.   
  27.                      http://www.springframework.org/schema/context    
  28.   
  29.                      http://www.springframework.org/schema/context/spring-context-2.5.xsd    
  30.   
  31.                      http://www.springframework.org/schema/aop    
  32.   
  33.                      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  34.   
  35.     
  36.   
  37.     <context:component-scan base-package="com.lz" />  
  38.   
  39.     <bean  
  40.   
  41.        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
  42.   
  43.     <bean id="viewResolver"  
  44.   
  45.        class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  46.   
  47.        <property name="viewClass">  
  48.   
  49.            <value>org.springframework.web.servlet.view.JstlView</value>  
  50.   
  51.        </property>  
  52.   
  53.        <property name="prefix">  
  54.   
  55.            <value>/</value>  
  56.   
  57.        </property>  
  58.   
  59.        <property name="suffix">  
  60.   
  61.            <value>.jsp</value>  
  62.   
  63.        </property>  
  64.   
  65.     </bean>  
  66.   
  67.     <bean id="dataSource"  
  68.   
  69.        class="org.apache.commons.dbcp.BasicDataSource">  
  70.   
  71.        <property name="driverClassName"  
  72.   
  73.            value="oracle.jdbc.driver.OracleDriver">  
  74.   
  75.        </property>  
  76.   
  77.        <property name="url"  
  78.   
  79.            value="">  
  80.   
  81.        </property>  
  82.   
  83.        <property name="username" value=""></property>  
  84.   
  85.        <property name="password" value=""></property>  
  86.   
  87.     </bean>  
  88.   
  89.     
  90.   
  91.     <bean id="transactionManager"  
  92.   
  93.        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  94.   
  95.        <property name="dataSource" ref="dataSource" />  
  96.   
  97.     </bean>  
  98.   
  99.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  100.   
  101.        <tx:attributes>  
  102.   
  103.            <tx:method name="find*" read-only="true"></tx:method>  
  104.   
  105.            <tx:method name="*"></tx:method>  
  106.   
  107.        </tx:attributes>  
  108.   
  109.     </tx:advice>  
  110.   
  111.     <aop:config proxy-target-class="true">  
  112.   
  113.        <aop:advisor  
  114.   
  115.            pointcut="execution(* com.lz.service.impl.*Impl.*(..))"  
  116.   
  117.            advice-ref="txAdvice"></aop:advisor>  
  118.   
  119.     </aop:config>  
  120.   
  121. </beans>  

 

3. JdbcBaseDao.java

  1. package com.lz.dao.base;   
  2.   
  3. import javax.annotation.Resource;   
  4.   
  5. import javax.sql.DataSource;   
  6.   
  7. import org.springframework.jdbc.core.support.JdbcDaoSupport;   
  8.   
  9.     
  10.   
  11. public class JdbcBaseDao extends JdbcDaoSupport {   
  12.   
  13.     @Resource(name = "dataSource")   
  14.   
  15.     public void setSuperDataSource(DataSource dataSource) {   
  16.   
  17.        super.setDataSource(dataSource);   
  18.   
  19.     }   
  20.   
  21. }   

 

4. TestDao.java

  1. package com.lz.dao;   
  2.   
  3. import java.util.List;   
  4.   
  5. import org.springframework.stereotype.Repository;   
  6.   
  7. import com.lz.dao.base.JdbcBaseDao;   
  8.   
  9.   
  10. @Repository("testDao")   
  11.   
  12. public class TestDao extends JdbcBaseDao {   
  13.   
  14.   
  15.     @SuppressWarnings("unchecked")   
  16.   
  17.     public List find() {   
  18.   
  19.        return getJdbcTemplate().queryForList("SELECT * FROM TEST");   
  20.   
  21.     }   
  22.   
  23. }   

 

5. TestController.java

  1. package com.lz.web;   
  2.   
  3.     
  4.   
  5. import javax.servlet.http.HttpServletRequest;   
  6.   
  7.     
  8.   
  9. import org.springframework.beans.factory.annotation.Autowired;   
  10.   
  11. import org.springframework.stereotype.Controller;   
  12.   
  13. import org.springframework.web.bind.annotation.RequestMapping;   
  14.   
  15.     
  16.   
  17. import com.lz.dao.TestDao;   
  18.   
  19.     
  20.   
  21. @Controller  
  22.   
  23. public class TestController {   
  24.   
  25.     @Autowired  
  26.   
  27.     private TestDao testDao;   
  28.   
  29.     
  30.   
  31.     private String formView = "test";   
  32.   
  33.     
  34.   
  35.     @RequestMapping("/test.html")   
  36.   
  37.     public String view(HttpServletRequest request) {   
  38.   
  39.        request.setAttribute("list", testDao.find());   
  40.   
  41.        return formView;   
  42.   
  43.     }   
  44.   
  45. }   
  46.   
  47.     

5. index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.   
  3.     pageEncoding="UTF-8"%>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6.   
  7. <html>  
  8.   
  9.     <head>  
  10.   
  11.        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  12.   
  13.        <title>Insert title here</title>  
  14.   
  15.     </head>  
  16.   
  17.     <body>  
  18.   
  19.        <form name="form1" method="post" action="test.html">  
  20.   
  21.            <input type="submit" name="Submit" value="提交">  
  22.   
  23.        </form>  
  24.   
  25.     </body>  
  26.   
  27. </html>  

 

6. test.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.   
  3.     pageEncoding="UTF-8"%>  
  4.   
  5. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  6.   
  7. <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>  
  8.   
  9. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>  
  10.   
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  12.   
  13. <html>  
  14.   
  15.     <head>  
  16.   
  17.        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  18.   
  19.        <title>Insert title here</title>  
  20.   
  21.     </head>  
  22.   
  23.     <body>  
  24.   
  25.        <table>  
  26.   
  27.            <c:forEach var="list" items="${list}">  
  28.   
  29.               <tr>  
  30.   
  31.                   <td>  
  32.   
  33.                      ${list.TEST_ID}   
  34.   
  35.                   </td>  
  36.   
  37.                   <td>  
  38.   
  39.                      ${list.TEST_NAME}   
  40.   
  41.                   </td>  
  42.   
  43.               </tr>  
  44.   
  45.            </c:forEach>  
  46.   
  47.        </table>  
  48.   
  49.     </body>  
  50.   
  51. </html>  

 

posted on 2009-08-24 11:38  鼎盛工作室  阅读(1373)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3