查詢銀行賬戶的數量 1.建立一個項目導入jar包(ioc aop dao 連接池 資料庫驅動 ),拷貝容器對應的配置文件到src下 2.在配置文件中開啟組件掃描 3.寫一個DAO介面定義一個查詢方法 4.定義一個JdbcTemplate的成員變數 4.1在類上加@Repository標註 4.2註入 ...
查詢銀行賬戶的數量
1.建立一個項目導入jar包(ioc aop dao 連接池 資料庫驅動 ),拷貝容器對應的配置文件到src下
2.在配置文件中開啟組件掃描
3.寫一個DAO介面定義一個查詢方法
4.定義一個JdbcTemplate的成員變數
4.1在類上加@Repository標註
4.2註入JdbcTemplate,JdbcTemplate創建時要使用到dataSource
4.3使用模板完成查詢
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:lang="http://www.springframework.org/schema/lang" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xmlns:util="http://www.springframework.org/schema/util" 8 xmlns:task="http://www.springframework.org/schema/task" 9 xmlns:aop="http://www.springframework.org/schema/aop" 10 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 11 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 14 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 16 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 17 <!-- 開啟組件掃描 --> 18 <context:component-scan base-package="com.xcz"></context:component-scan> 19 <!-- 開啟標註形式的mvc --> 20 <mvc:annotation-driven></mvc:annotation-driven> 21 <!-- 配置視圖處理器 --> 22 <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 23 <property name="prefix" value="/WEB-INF/"></property> 24 <property name="suffix" value=".jsp"></property> 25 </bean> 26 <!-- 引入外部資源 --> 27 <context:property-placeholder location="classpath:db.properties"/> 28 <!-- 配置數據源 --> 29 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 30 <property name="driverClassName" value="${driverClassName}"></property> 31 <property name="url" value="${url}"></property> 32 <property name="username" value="${jdbc.username}"></property> 33 <property name="password" value="${jdbc.password}"></property> 34 </bean> 35 <!-- 定義一個模板 --> 36 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 37 <constructor-arg index="0" ref="dataSource"></constructor-arg> 38 </bean> 39 </beans>xml
1 driverClassName=oracle.jdbc.OracleDriver 2 url=jdbc:oracle:thin:@127.0.0.1:1521:xe 3 jdbc.username= 4 jdbc.password= 5 maxActive=20db.properties
1 package com.xcz.dao; 2 3 public interface XdlBankAccountDao { 4 int getBankAccount(); 5 }dao
1 package com.xcz.impl; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.jdbc.core.JdbcTemplate; 5 import org.springframework.stereotype.Repository; 6 7 import com.xcz.dao.XdlBankAccountDao; 8 9 @Repository("bankDao") 10 public class XdlBankAccountImpl implements XdlBankAccountDao{ 11 @Autowired 12 private JdbcTemplate jdbcTemplate; 13 @Override 14 public int getBankAccount() { 15 String sql = "select count(1) from xdl_bank_account"; 16 return jdbcTemplate.queryForInt(sql); 17 } 18 }impl
1 package com.xcz.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.xcz.dao.XdlBankAccountDao; 7 import com.xcz.impl.XdlBankAccountImpl; 8 9 public class TestXdlBankAccount { 10 public static void main(String[] args) { 11 ApplicationContext ioc = new ClassPathXmlApplicationContext("mvc.xml"); 12 XdlBankAccountDao count = ioc.getBean("bankDao", XdlBankAccountImpl.class); 13 System.out.println(count.getBankAccount()); 14 } 15 }test