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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.xcz"></context:component-scan>
<!-- 开启标注形式的mvc -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图处理器 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 引入外部资源 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 定义一个模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
</beans>

xml

 driverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:xe
jdbc.username=
jdbc.password=
maxActive=20

db.properties

 package com.xcz.dao;

 public interface XdlBankAccountDao {
int getBankAccount();
}

dao

 package com.xcz.impl;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; import com.xcz.dao.XdlBankAccountDao; @Repository("bankDao")
public class XdlBankAccountImpl implements XdlBankAccountDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int getBankAccount() {
String sql = "select count(1) from xdl_bank_account";
return jdbcTemplate.queryForInt(sql);
}
}

impl

 package com.xcz.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xcz.dao.XdlBankAccountDao;
import com.xcz.impl.XdlBankAccountImpl; public class TestXdlBankAccount {
public static void main(String[] args) {
ApplicationContext ioc = new ClassPathXmlApplicationContext("mvc.xml");
XdlBankAccountDao count = ioc.getBean("bankDao", XdlBankAccountImpl.class);
System.out.println(count.getBankAccount());
}
}

test

最新文章

  1. 科蓝软件急招前端开发、PHP、.NET工程师
  2. 【BZOJ 3531】【SDOI 2014】旅行
  3. css3动画 bug
  4. 2016.07.04,英语,《Vocabulary Builder》Unit 23
  5. ArcGIS API for Silverlight代码中使用Template模板
  6. Tableau学习笔记之二
  7. 学生管理系统(list)
  8. mongodb启动关闭;
  9. HDU1250:Hat&#39;s Fibonacci
  10. python分支
  11. day 29 元类
  12. c/c++ 线性表之单向循环链表
  13. 线程(Thread)
  14. js中的setTimeout第三个参数
  15. 003.NFS配置实例
  16. 移植到windows下的iconv
  17. python-day39--mysql基本操作
  18. 执行Docker命令报错解决办法
  19. Gitlab+Jenkins学习之路(十)之Jenkins按角色授权和Pipeline
  20. 【vue.js】windows下安装vue.js

热门文章

  1. ASP.NET Core实现 随处可见的基本身份认证
  2. Haskell学习-函数式编程初探
  3. 聊聊 JUC 并发包
  4. 神经网络——反向传播BP算法公式推导
  5. Docker最全教程之Python爬网实战(二十一)
  6. 查看多核CPU各核的状态
  7. Docker 创建 Crucible4.6.1 以及与 Crowd3.3.2 实现 SSO 单点登录
  8. python 简史
  9. Yii2 解决2006 MySQL server has gone away问题
  10. 栈到CLR