Spring(三)使用JdbcTemplate对象完成查询

时间:2023-03-08 19:42:33

查询银行账户的数量
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