经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Spring(三)使用JdbcTemplate对象完成查询
来源:cnblogs  作者:零晨三点半  时间:2018/11/10 3:23:40  对本文有异议

查询银行账户的数量
1.建立一个项目导入jar包(ioc aop dao 连接池 数据库驱动 ),拷贝容器对应的配置文件到src下
2.在配置文件中开启组件扫描
3.写一个DAO接口定义一个查询方法
4.定义一个JdbcTemplate的成员变量
4.1在类上加@Repository标注
4.2注入JdbcTemplate,JdbcTemplate创建时要使用到dataSource
4.3使用模板完成查询

  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <beans xmlns="http://www.springframework.org/schema/beans"
  3. 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. 4 xmlns:context="http://www.springframework.org/schema/context"
  5. 5 xmlns:lang="http://www.springframework.org/schema/lang"
  6. 6 xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. 7 xmlns:util="http://www.springframework.org/schema/util"
  8. 8 xmlns:task="http://www.springframework.org/schema/task"
  9. 9 xmlns:aop="http://www.springframework.org/schema/aop"
  10. 10 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
  11. 11 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
  12. 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  13. 13 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
  14. 14 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
  15. 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
  16. 16 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
  17. 17 <!-- 开启组件扫描 -->
  18. 18 <context:component-scan base-package="com.xcz"></context:component-scan>
  19. 19 <!-- 开启标注形式的mvc -->
  20. 20 <mvc:annotation-driven></mvc:annotation-driven>
  21. 21 <!-- 配置视图处理器 -->
  22. 22 <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  23. 23 <property name="prefix" value="/WEB-INF/"></property>
  24. 24 <property name="suffix" value=".jsp"></property>
  25. 25 </bean>
  26. 26 <!-- 引入外部资源 -->
  27. 27 <context:property-placeholder location="classpath:db.properties"/>
  28. 28 <!-- 配置数据源 -->
  29. 29 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  30. 30 <property name="driverClassName" value="${driverClassName}"></property>
  31. 31 <property name="url" value="${url}"></property>
  32. 32 <property name="username" value="${jdbc.username}"></property>
  33. 33 <property name="password" value="${jdbc.password}"></property>
  34. 34 </bean>
  35. 35 <!-- 定义一个模板 -->
  36. 36 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  37. 37 <constructor-arg index="0" ref="dataSource"></constructor-arg>
  38. 38 </bean>
  39. 39 </beans>
xml
  1. 1 driverClassName=oracle.jdbc.OracleDriver
  2. 2 url=jdbc:oracle:thin:@127.0.0.1:1521:xe
  3. 3 jdbc.username=
  4. 4 jdbc.password=
  5. 5 maxActive=20
db.properties
  1. 1 package com.xcz.dao;
  2. 2
  3. 3 public interface XdlBankAccountDao {
  4. 4 int getBankAccount();
  5. 5 }
dao
  1. 1 package com.xcz.impl;
  2. 2
  3. 3 import org.springframework.beans.factory.annotation.Autowired;
  4. 4 import org.springframework.jdbc.core.JdbcTemplate;
  5. 5 import org.springframework.stereotype.Repository;
  6. 6
  7. 7 import com.xcz.dao.XdlBankAccountDao;
  8. 8
  9. 9 @Repository("bankDao")
  10. 10 public class XdlBankAccountImpl implements XdlBankAccountDao{
  11. 11 @Autowired
  12. 12 private JdbcTemplate jdbcTemplate;
  13. 13 @Override
  14. 14 public int getBankAccount() {
  15. 15 String sql = "select count(1) from xdl_bank_account";
  16. 16 return jdbcTemplate.queryForInt(sql);
  17. 17 }
  18. 18 }
impl
  1. 1 package com.xcz.test;
  2. 2
  3. 3 import org.springframework.context.ApplicationContext;
  4. 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. 5
  6. 6 import com.xcz.dao.XdlBankAccountDao;
  7. 7 import com.xcz.impl.XdlBankAccountImpl;
  8. 8
  9. 9 public class TestXdlBankAccount {
  10. 10 public static void main(String[] args) {
  11. 11 ApplicationContext ioc = new ClassPathXmlApplicationContext("mvc.xml");
  12. 12 XdlBankAccountDao count = ioc.getBean("bankDao", XdlBankAccountImpl.class);
  13. 13 System.out.println(count.getBankAccount());
  14. 14 }
  15. 15 }
test

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号