spring的aspect 能对spring包中spring3 jdbctemplatee类中的方法进行拦截吗

Spring三种事宜处理方式_基于spring aop 权限管理系统原形_基础知识的温习__脚本百事通
稍等,加载中……
^_^请注意,有可能下面的2篇文章才是您想要的内容:
Spring三种事宜处理方式
基于spring aop 权限管理系统原形
基础知识的温习
Spring三种事宜处理方式
Spring三种事务处理方式1、用原始的transactionfactorybean的,代理dao事务处理
2、用aop:config声明要进行事务增强的切面,用tx:advice声明具体方法的事务属性,及应用到的事务管理器
3、使用@transactional注解配置声明事务(最简单实用的方法)
如有一代表用户的域对象user:package com.import java.io.public class user implements serializable{private int user_private string user_private string user_private string user_....//省略set、get方法}
user的数据库操作接口:package com.import com.domain.public interface userdao {public void adduser(user user);}
有一继承spring jdbc支持类的userdao接口实现类,实现添加一个user的方法。它需要注入一个spring jdbc模板类jdbctemplate:package com.dao.import com.domain.import com.dao.import org.springframework.jdbc.core.support.
public class userjdbcdao extends jdbcdaosupport implements userdao{public void adduser(user user){string sql = "insert into user(user_name,user_password,user_desc) values(?,?,?)";object[] params = new object[]{user.getuser_name(),user.getuser_password(),user.getuser_desc()} ;this.getjdbctemplate().update(sql, params);}}
以上dao层的类对应的bean的基本配置文件app_dao.xml如下(数据源的属性放入了外部的资源文件"prop.properties"):
&bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"&&property name="location" value="classpath:prop.properties"/&&/bean&&!--数据源--&&bean id="datasource" class="mons.dbcp.basicdatasource"destroy-method="close"&&property name="driverclassname" value="${jdbc.driverclassname}"/&&property name="url" value="${jdbc.url}"/&&property name="username" value="${jdbc.username}"/&&property name="password" value="${jdbc.password}"/&&/bean&&!--spring jdbc模板bean,它注入了上面的数据源--&&bean id="jdbctemplate" class="org.springframework.jdbc.core.jdbctemplate"&&property name="datasource" ref="datasource"/&&/bean&
&!--user数据操作的bean声明,它注入了上面的spring jdbc模板bean:jdbctemplate--&&bean id="userjdbcdao"class="com.dao.jdbc.userjdbcdao"&&property name="jdbctemplate" ref="jdbctemplate"/& &/bean&&/beans&
这里我简单地模拟业务类(业务接口userservice省略):package com.service.import com.dao.import com.domain.import com.service.
public class userserviceimpl implements userservice {pripublic void setuserdao(userdao userdao){this.userdao =}public void adduser(user user){this.userdao.adduser(user);}}
为了在业务类中使用事务管理功能,有如下几个方法:1、用原始的transactionfactorybean的app.xml基本配置:
&import resource="classpath:app_dao.xml"/&&!--导入dao层的配置--&&!--spring jdbc的事务管理bean,引入了dbcp数据源--&&bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"&&property name="datasource" ref="datasource"/&&/bean&&!--业务类bean--&&bean id="userserviceimpltarget" class="com.service.impl.userserviceimpl"&&property name="userdao" ref="userjdbcdao"/&&/bean&
&!--应用原始的transactionfactorybean进行事务管理bean的声明--&&bean id="userserviceimpl"class="org.springframework.transaction.interceptor.transactionproxyfactorybean"&&property name="transactionmanager" ref="txmanager"/&&!--指定事务管理bean--&&property name="target" ref="userserviceimpltarget"/&&!--指定业务bean--&&property name="transactionattributes"&&!--事务的属性设置列表--&&props&&prop key="add*"&propagation_required,isolation_serializable&/prop&&!--设置事务为只读时,添加数据将会产生异常--&&!--&prop key="add*"&propagation_required,isolation_serializable,readonly&/prop&--&&/props&&/property&&/bean&
测试:......userserviceimpl usi = (userserviceimpl)ctx.getbean("userserviceimpl");......
2、用tx/aop命名空间配置:
&?xml version="1.0" encoding="utf-8"?&&beans .....xmlns:tx="http://www.springframework.org/schema/tx"xsp:schemalocation="http://www.springframework.org/schema/beans...........http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"&
&import resource="classpath:app_dao.xml"/&
&bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"&&property name="datasource" ref="datasource"/&&/bean&
&bean id="userserviceimpltarget" class="com.service.impl.userserviceimpl"&&property name="userdao" ref="userjdbcdao"/&&/bean&
&!--应用tx/aop命名空间进行事务声明--&&!--用tx:advice声明具体方法的事务属性,及应用到的事务管理器--&&tx:advice id="txadvice" transaction-manager="txmanager"&&tx:attributes&&tx:method name="add*" read-only="true"/&&/tx:attributes&&/tx:advice&&!--用aop:config声明要进行事务增强的切面--&&aop:config&&aop:pointcut id="servicemethod"expression="execution(* com.service..add*(..))"/&&aop:advisor pointcut-ref="servicemethod" advice-ref="txadvice"/&&/aop:config&&/beans&
测试:.......userservice usi = (userservice)ctx.getbean("userserviceimpltarget");..........
3、使用@transactional注解配置声明事务(最简单实用的方法):
在需要事务管理增强的业务类加入@transactional注解标记,如:......import org.springframework.transaction.annotation. //注解式事务
@transactional(readonly=false) //对业务类进行事务增强的标注public class userserviceimpl implements userservice {...........}
再在配置文件中用&tx:annotation-driven&驱动自动为标记@transactional注解的类织入事务管理增强:&import resource="classpath:app_dao.xml"/&
&bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"&&property name="datasource" ref="datasource"/&&/bean&
&!--注解式事务配置驱动--&&tx:annotation-driven transaction-manager="txmanager" proxy-target-class="true"/&
&!--业务类bean的实现类标注了@transactional注解,所以会被tx:annotation-driven注解驱动自动织入事务增强--&&bean id="userservice" class="com.service.impl.userserviceimpl"&&property name="userdao" ref="userjdbcdao"/&&/bean&
测试:.........userserviceimpl usi = (userserviceimpl)ctx.getbean("userservice");.........
基于spring aop 权限管理系统原形
基于spring aop 权限管理系统原型
此权限管理系统把待访问的业务层方法做为权限管理中的资源,通过spring aop 对接口方法进行拦截,来实现权限的管理,可以实现细粒度的权限控制。在上文体验了spring aop 一些特性,aop 接口:MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice 实现这三个接口分别对方法执行前,后,执行中抛异常等情况进行的,我们要是想做overload 这样的操作时,要用MethodInterceptor 接口,此接口好在有返回值,public Object invoke(
MethodInvocation invocation)
throws Throwable
{//.}上文做法有些牵强业务逻辑还有throws PermissionDeniedException 感觉不爽,现在用MethodInterceptor 接口,来写这个demo,把权限与业务分开。advice 如下:public class PermissionCheckAroundAdvice implements MethodInterceptor {
SecurityManager securityMgr = new SecurityManager();
* @param securityMgr The securityMgr to set.
public void setSecurityMgr(SecurityManager securityMgr) {
this.securityMgr = securityM
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("(被调用方法接口类名: "
+ invocation.getMethod().getDeclaringClass().getName() + ")");
System.out.println("(被调用方法名:" + invocation.getMethod().getName()+ ")");
String methodName = invocation.getMethod().getDeclaringClass()
.getName() + "." + invocation.getMethod().getName();
System.out.println("(被调用方法全名:" + methodName + ")");
System.out.println("有否权限:(" + securityMgr.checkPermission(methodName)+ ")");
if(securityMgr.checkPermission(methodName))
return invocation.proceed();
System.out.println("Goodbye! NO Permission!(by " + this.getClass().getName() + ")");
return "--";
}}服务层业务接口修改如下:public interface Service {
public String getBeanInfo();}服务层业务实现类如下:public class ServiceBean implements Service {
* @param bean The bean to set.
public void setBean(ResourceBean bean) {
this.bean =
public String getBeanInfo(){
String result="";
result+= bean.getMethod1();
result+= bean.getMethod2();
result+= bean.getMethod3();
}}资源层,接口 ,类如下:public interface Resource {}public interface ResourceBean extends Resource{
public void theMethod();
public String getMethod1();
public String getMethod2();
public String getMethod3();}public class ResourceBeanImpl implements ResourceBean,InitializingBean{
public void theMethod(){
System.out.println(this.getClass().getName()
+ "." + new Exception().getStackTrace()[0].getMethodName()
+ " says HELLO!");
public String getMethod1(){
return "张三";
public String getMethod2(){
return "李四";
public String getMethod3(){
return "王五";
public void afterPropertiesSet() throws Exception {
System.out.println("事件监听:类ResourceBeanImpl属性设置完毕");
}}权限管理类:public class User {
List privilages = new java.util.ArrayList();
public User(){
* @param privilages The privilages to set.
public void setPrivilages(List privilages) {
this.privilages =
public String getName(){
public void setName(String name){
this.name=
public boolean isPermission(String pri){
java.util.Iterator it = privilages.iterator();
String p = "";
boolean pass=
while(it.hasNext()){
p=(String)it.next();
System.out.println(p);
if(p.equals(pri)){
}}public class SecurityManager {
public void setUser(User user){
this.user =
public boolean checkPermission(String privilege){
return checkPermission(user,privilege);
public boolean checkPermission(User user, String privilege){
return user.isPermission(privilege);
}}配置文件:&?xml version="1.0" encoding="UTF-8"?&&!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&&beans&
&!--CONFIG--&
&bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean"&
&property name="proxyInterfaces"&
&value&com.jhalo.jsecurity.aop.ResourceBean&/value&
&/property&
&property name="target"&
&ref local="beanTarget"/&
&/property&
&property name="interceptorNames"&
&value&permissionAroundAdvisor&/value&
&/property&
&bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean"&
&property name="proxyInterfaces"&
&value&com.jhalo.jsecurity.aop.Service&/value&
&/property&
&property name="target"&
&ref local="serviceBean"/&
&/property&
&property name="interceptorNames"&
&value&permissionAroundAdvisor&/value&
&/property&
&!--CLASS--&
&bean id="resourceMgr" class="com.jhalo.jsecurity.aop.ResourceManager"/&
&bean id="beanTarget" class="com.jhalo.jsecurity.aop.ResourceBeanImpl"/&
&bean id="beanTarget2" class="com.jhalo.jsecurity.aop.ResourceBean2Impl"/&
&bean id="user" class="com.jhalo.jsecurity.aop.User"&
&property name="name"&
&value&tester&/value&
&/property&
&property name="privilages"&
&value&com.jhalo.jsecurity.aop.ResourceBean.getMethod3&/value&
&value&com.jhalo.jsecurity.aop.Service.getBeanInfo&/value&
&value&com.jhalo.jsecurity.aop.ResourceBean.getMethod1&/value&
&/property&
&bean id="securityMgr" class="com.jhalo.jsecurity.aop.SecurityManager"&
&property name="user"&
&ref local="user"/&
&/property&
&bean id="serviceBean" class="com.jhalo.jsecurity.aop.ServiceBean"&
&property name="bean"&
&!-- &ref local="beanTarget"/&--&
&ref local="bean"/&
&/property&
&!--ADVISOR--&
&!--Note: An advisor assembles pointcut and advice--&
&!-- permission around advisor --&
&bean id="permissionAroundAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"&
&property name="advice"&
&ref local="thePermissionAroundAdvice"/&
&/property&
&property name="pattern"&
&value&.*&/value&
&/property&
&!--ADVICE--&
&bean id="thePermissionCheckBeforeAdvice" class="com.jhalo.jsecurity.aop.PermissionCheckAdvice"/&
&bean id="thePermissionThrowsAdvice" class="com.jhalo.jsecurity.aop.PermissionThrowsAdvice"/&
&bean id="thePermissionAroundAdvice" class="com.jhalo.jsecurity.aop.PermissionCheckAroundAdvice"&
&property name="securityMgr"&
&ref local="securityMgr"/&
&/property&
&/bean&&/beans&User 所拥有的权限是在spring 配置文件中手工配置的,在实际应用中不可行,可以从DB中取得。测试类:public class SpringAopTest {
public static void main(String[] args) {
//Read the configuration file
ApplicationContext ctx
= new FileSystemXmlApplicationContext("springconfig.xml");String name = "";Service sb = (Service)ctx.getBean("service");//
System.out.println("---"+ctx.isSingleton("service")+"---");
name = sb.getBeanInfo();
System.out.println("test result::" +name);
基础知识的温习
基础知识的复习1. SAN
 SAN(Storage Area Network的简称)直译过来就是存储区域网络,它采用光纤通道(Fibre Channel)技术,通过光纤通道交换机连接存储阵列和服务器主机,建立专用于数据存储的区域网络。SAN网络存储是一种高速网络或子网络,SAN存储系统提供在计算机与存储系统之间的数据传输。
NAS存储网络
NAS是通过网线连接的磁盘阵列,具备磁盘阵列的所有主要特征:高容量、高效能、高可靠。
这是一种直接与主机系统相连接的存储设备,如作为服务器的计算机内部硬件驱动。
2.硬中断和软中断
编程异常通常叫做软中断 软中断是通讯进程之间用来模拟硬中断的一种信号通讯方式。 中断源发中段请求或软中断信号后,CPU
或接收进程在适当的时机自动进行中断处理或完成软中断信号对应的功能。
硬中断是硬件实现的中断,是程序运行时设备对它的中断
3.简述ISO OSI的物理层Layer1,链路层Layer2,网络层Layer3的任务。
网络层:资料传送的目的地寻址,再选择出传送资料的最佳路线;链路层:负责网络上资料封包如何传送的方式;物理层:在设备与传输媒介之间建立及终止连接。参与通讯过程使得资源可以在共享的多用户中有效分配,对信号进行调制或转换使得用户设备中的数字信号定义能与信道上实际传送的数字信号相匹配
网络层为建立网络连接和为上层提供服务,应具备以下主要功能.
① 路由选择和中继.
② 激活,终止网络连接.
③ 在一条数据链路上复用多条网络连接,多采取分时复用技术.
④ 差错检测与恢复.
⑤ 排序,流量控制.
⑥ 服务选择.
⑦ 网络管理.
4.什么是进程和线程?有何区别?线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源.一个线程可以创建和撤销另一个线程;同一个进程中的多个线程之间可以并发执行.
TCP/IP四层模型和OSI七层模型
表1-1是 TCP/IP四层模型和OSI七层模型对应表。我们把OSI七层网络模型和Linux TCP/IP四层概念模型对应,然后将各种网络协议归类。
TCP/IP四层模型和OSI七层模型对应表
OSI七层网络模型
Linux TCP/IP四层概念模型
对应网络协议
应用层(Application)
TFTP, FTP, NFS, WAIS
表示层(Presentation)
Telnet, Rlogin, SNMP, Gopher
会话层(Session)
传输层(Transport)
网络层(Network)
IP, ICMP, ARP, RARP, AKP, UUCP
数据链路层(Data Link)
FDDI, Ethernet, Arpanet, PDN, SLIP, PPP
物理层(Physical)
IEEE 802.1A, IEEE 802.2到IEEE 802.11
6. 面向连接(TCP)的套接字的系统调用时序图。
AS: socket()建立流式套接字,返回套接字号SS。
↓BS: bind(),套接字SS与本地地址相连。
↓CS: listen(),通知TCP服务器准备好接受连接。
↓DS: accpet(),接受连接等待客户端的连接。
AC: socket()建立流式套接字号SC。
↓ES: 建立连接,accpet()返回得到新的套接字,如NS。
BC: connection(),将套接字S与远地主机连接。
&------------------ Connect to ES_FS
↓FS: recv() & send(),在套接字NS上读写数据直到完成交换。
CC: send() & recv(),在套接字上读写数据直到数据交换完成。
↓GS: closesocket(), 关闭套接字NS。
DC: closesocket(),关闭套接字SC,结束TCP对话。
Goto: C to D
↓HS: closesocket(),关闭最初套接字SS,服务结束。
8.New delete 与malloc free 的联系与区别?
答案:都是在堆(heap)上进行动态的内存操作。用malloc函数需要指定内存分配的字节数并且不能初始化对象,new 会自动调用对象的构造函数。delete 会调用对象的destructor,而free 不会调用对象的destructor.
9.观察者模式的缺点
(1) 如果一个被观察者对象有很多直接和间接的观察者的话,将所有的观察者都通知到会花费很多时间。
(2)如果在被观察者之间有循环依赖的话, 被观察者会触发它们之间进行循环调用,导致系统崩溃。在使用观察考模式时要特别注意这一点。
(3)如果对观察者的通知是通过另外的线程进 行异步投递的话,系统必须保证投递是以自恰(?)的方式进行的。
(4)虽然观察者模式可以随时使观察者知道所观察的对象发生了变化,但是观察者 模式没有相应的机制使观察者知道所观察的对象是怎么发生变化的。
如果您想提高自己的技术水平,欢迎加入本站官方1号QQ群:&&,&&2号QQ群:,在群里结识技术精英和交流技术^_^
本站联系邮箱:本文使用Spring2.5.6.SEC02和JDK1.4作为讲解环境。
Spring框架中org.springframework.jdbc.core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,其他模板类都是基于它封装完成的。
Spring除了提供JdbcTemplate核心类外,还提供了基于JdbcTemplate实现的NamedParameterJdbcTemplate类用于支持命名参数绑定、 SimpleJdbcTemplate类用于支持JDK5+的可变参数及自动装箱拆箱等特性。本文主要介绍JdbcTemplate核心类。
JdbcTemplate类主要提供以下四类方法:
execute方法:用于执行任何SQL语句,一般用于执行DDL语句;
update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句,batchUpdate方法用于执行批处理相关语句;
query方法及queryForXXX方法:用于执行查询相关语句;
call方法:用于执行存储过程、函数相关语句。
下面将主要介绍query方法及queryForXXX方法的返回值和相关异常。
首先我们先大概了解下query方法及queryForXXX方法的调用层次。
下图是不需要设置sql参数的方法的调用层次:
下图是需要设置sql参数的方法的调用层次:
其中Object[] args就是设置参数用的,而int[] argTypes则是指定参数类型,参数类型定义在java.sql.Types类中。
接下来我们来看看不需要设置sql参数的查询方法在0行1列,1行1列和5行1列结果集中的返回值
List query
(String sql,
RowMapper rowMapper)
大小为0的List
大小为1的List
大小为5的List
Map queryForMap
(String sql)
org.springframework.dao.
EmptyResultDataAccessException: &Incorrect result size: expected 1, actual 0
以列名为key,大小为1的Map
org.springframework.dao.
IncorrectResultSizeDataAccessException: &Incorrect result size: expected 1, actual 5
Object queryForObject
(String sql,
RowMapper rowMapper)
org.springframework.dao.
EmptyResultDataAccessException: &Incorrect result size: expected 1, actual 0
org.springframework.dao.
IncorrectResultSizeDataAccessException:
Incorrect result size: expected 1, actual 5
Object queryForObject
(String sql,
Class requiredType)
org.springframework.dao.
EmptyResultDataAccessException:
Incorrect result size: expected 1, actual 0
类型为requiredType的Object
org.springframework.dao.
IncorrectResultSizeDataAccessException: &Incorrect result size: expected 1, actual 5
long queryForLong
(String sql)
org.springframework.dao.
EmptyResultDataAccessException: &Incorrect result size: expected 1, actual 0
org.springframework.dao.
IncorrectResultSizeDataAccessException: &Incorrect result size: expected 1, actual 5
int queryForInt
(String sql)
org.springframework.dao.
EmptyResultDataAccessException: &Incorrect result size: expected 1, actual 0
org.springframework.dao.
IncorrectResultSizeDataAccessException: &Incorrect result size: expected 1, actual 5
List queryForList
(String sql,
Class elementType)
大小为0的List
大小为1的List
大小为5的List
List queryForList
(String sql)
大小为0的List
List中包含1个以列名为key的Map
List中包含5个以列名为key的Map
&&& 其中下面这些查询方法支持多列结果集:
List query(String sql, RowMapper rowMapper)
Map queryForMap(String sql)
Object queryForObject(String sql, RowMapper rowMapper)
List queryForList(String sql)
其他不支持多列结果集的查询方法则会抛出IncorrectResultSetColumnCountException异常。
设置sql参数的查询方法的返回值和上述结果类似。
从上面的结果可以看出只有返回值类型为List的方法可以接收零到多行结果集而不抛出异常,所以在使用query方法及queryForXXX方法时需要注意处理EmptyResultDataAccessException和IncorrectResultSizeDataAccessException这两个异常,这两个异常反映出数据库表中数据可能出现了缺失或冗余。如果返回值不符合期望值时,则需要排查业务流程或者数据了。
最后我们来看看RowMapper接口,这个接口的实现类的功能是将结果集中的每一行数据封装成用户定义的结构,所以在查询方法中经常会被用到。
Spring框架为我们提供了BeanPropertyRowMapper/ParameterizedBeanPropertyRowMapper,ColumnMapRowMapper和SingleColumnRowMapper这三大便利类。
BeanPropertyRowMapper类与ParameterizedBeanPropertyRowMapper类的功能完全相同,当POJO对象和数据库表字段完全对应或者驼峰式与下划线式对应时,该类会根据构造函数中传递的class来自动填充数据。只是ParameterizedBeanPropertyRowMapper类使用泛型需要JDK5+支持。这里需要注意虽然这两个类提供了便利,但是由于使用反射导致性能下降,所以如果需要高性能则还是需要自己去实现RowMapper接口来包装数据。
ColumnMapRowMapper类返回一个List对象,对象中的每一个元素都是一个以列名为key的Map对象。
SingleColumnRowMapper类也返回一个List对象,对象中的每个元素是数据库中的某列的值。注意结果集必须是单列,不然会抛出IncorrectResultSetColumnCountException异常。
现在在Spring2.5.6.SEC02和JDK1.4环境下建个项目来了解下上述三个便利类。
建立数据库表:
create table T_SPRINGJDBC_TEST
my_varchar VARCHAR2(10),
插入5条测试数据:
新建一个Java项目,准备依赖Jar包,Spring配置文件appContext.xml,配置数据源。
建立POJO对象:
public class MyPojo {
private long my_N
private Date my_D
private String my_V
//本文在此处省略了setter和getter方法
public String toString() {
return "MyPojo :{my_Number=" + my_Number + ", my_Date=" + my_Date
+ ", my_Varchar=" + my_Varchar + "}";
建立带main方法的类
先来了解BeanPropertyRowMapper类:
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"classpath:appContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List result = jdbcTemplate.query("select * from t_springjdbc_test",
new BeanPropertyRowMapper(MyPojo.class));
System.out.println(result);
输出结果如下:
[MyPojo :{my_Number=1, my_Date= 01:00:00.0, my_Varchar=a}, MyPojo :{my_Number=2, my_Date= 02:00:00.0, my_Varchar=b}, MyPojo :{my_Number=3, my_Date= 03:00:00.0, my_Varchar=c}, MyPojo :{my_Number=4, my_Date= 04:00:00.0, my_Varchar=d}, MyPojo :{my_Number=5, my_Date= 05:00:00.0, my_Varchar=e}]
再来了解ColumnMapRowMapper类:
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"classpath:appContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List result = jdbcTemplate.query("select * from t_springjdbc_test",
new ColumnMapRowMapper());
System.out.println(result);
输出结果为:
[{MY_NUMBER=1, MY_VARCHAR=a, MY_DATE= 01:00:00.0}, {MY_NUMBER=2, MY_VARCHAR=b, MY_DATE= 02:00:00.0}, {MY_NUMBER=3, MY_VARCHAR=c, MY_DATE= 03:00:00.0}, {MY_NUMBER=4, MY_VARCHAR=d, MY_DATE= 04:00:00.0}, {MY_NUMBER=5, MY_VARCHAR=e, MY_DATE= 05:00:00.0}]
最后了解SingleColumnRowMapper类:
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"classpath:appContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List result = jdbcTemplate.query(
"select my_number from t_springjdbc_test",
new SingleColumnRowMapper());
System.out.println(result);
输出结果为:
[1, 2, 3, 4, 5]
现在大家初步了解JdbcTemplate类中查询方法了吧!
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区

我要回帖

更多关于 spring3 jdbctemplate 的文章

 

随机推荐