Spring(三)使用JdbcTemplate对象完成查询
程序员文章站
2024-02-01 19:22:58
查询银行账户的数量 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>
1 driverclassname=oracle.jdbc.oracledriver 2 url=jdbc:oracle:thin:@127.0.0.1:1521:xe 3 jdbc.username= 4 jdbc.password= 5 maxactive=20
1 package com.xcz.dao; 2 3 public interface xdlbankaccountdao { 4 int getbankaccount(); 5 }
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 }
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 }