spring 3.0 + ibatis 2 整合

一、需要用到的包

二、

Suppliers.java                     属性 id ,name 

Suppliers.xml

HelloWordDao.java

HelloWord.java

HelloWordAspect.java:

public class HelloWordAspect {
 public void beforeAdvice() {
  System.out.println("添加之前===before");
 }
 
 public void afterAdvice() {
  System.out.println("添加之后===after");
 }
}

 

SqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
 "
http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
 <settings useStatementNamespaces="true" />

 <sqlMap resource="com/spring/bean/Suppliers.xml" />
</sqlMapConfig>

 

applicationContext.xml:

<?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:aop="
http://www.springframework.org/schema/aop"
  xmlns:tx="
http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
  
http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 <!-- jdbc属性文件 -->
 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:jdbc.properties</value>
   </list>
  </property>
 </bean>
 
 <!-- 数据源 -->
 <!--事实上是因为DriverManagerDataSource建立连接是只要有连接就新建一个connection,根本没有连接池的作用 
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="${db.driver.class}" />
   <property name="url" value="${db.url}" />
   <property name="username" value="${db.username}" />
   <property name="password" value="${db.password}" />
 </bean>
 -->
 <!-- dbcp连接池 -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${db.driver.class}" />
  <property name="url" value="${db.url}" />
  <property name="username" value="${db.username}" />
  <property name="password" value="${db.password}" />
 </bean>
 
 <!-- ibatis配置文件 -->
 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation">
   <value>classpath:SqlMapConfig.xml</value>
  </property>
  <property name="dataSource" ref="dataSource" />
 </bean>
  
 <bean id="helloworddao" class="com.spring.dao.HelloWordDao">
  <property name="sqlMapClient" ref="sqlMapClient"></property>
 </bean>
 
 <bean id="helloword" class="com.spring.service.HelloWord">
  <property name="helloWordDao" ref="helloworddao"></property>
 </bean>
 
 <!-- 切面类 -->
 <bean id="helloAspect" class="com.spring.test.HelloWordAspect"></bean>
 
 <!-- aop实现 -->
 <aop:config>
  <aop:pointcut id="pointcut" expression="execution(* com.spring.service..*(..))"/>
  <aop:aspect ref="helloAspect">
   <aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
   <aop:after pointcut="execution(* com.spring.service..*(..))" method="afterAdvice"/>
  </aop:aspect>
 </aop:config>
 
 <!-- 配置事务管理器 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
 </bean> 
 
 <!-- 配置事务特性 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="find*" read-only="true"/>
   <tx:method name="get*" read-only="true"/>
   <tx:method name="*"/>
  </tx:attributes>
 </tx:advice>
 
 <!-- 配置哪些类的方法需要加事务 -->
 <aop:config>
  <aop:pointcut id="pointcutManager" expression="execution(* com.spring.service..*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutManager"/>
 </aop:config>
</beans>

 

log4j.properties

# This is the configuring for logging displayed in the Application Server
log4j.rootCategory=WARN, stdout,logfile

#stdout configure
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n

#logfile configure
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=${catalina.home}/logs/sniper.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n

# Control logging for other open source packages
# Changing the log level to DEBUG when debug
log4j.logger.org.springframework=INFO
# Changing the log level to DEBUG will display SQL Hibernate generated
log4j.logger.java.sql=DEBUG
log4j.logger.org.hibernate=ERROR
log4j.logger.org.hibernate.SQL=ERROR
log4j.logger.org.hibernate.cache=ERROR
log4j.logger.net.sf.ehcache=ERROR

 

测试类:Test_HelloWord.java

public class Test_HelloWord {
 public static void main(String [] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  HelloWord h = context.getBean("helloword", HelloWord.class);
  List<Suppliers> list = h.findAll();
  for (Suppliers o : list) {
   System.out.println(o);
  }
 }
}

 

输出结果:

2012-04-24 17:20:48,968 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - <Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@a56a7c: startup date [Tue Apr 24 17:20:48 CST 2012]; root of context hierarchy>
2012-04-24 17:20:49,109 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [applicationContext.xml]>
2012-04-24 17:20:49,812 INFO [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - <Loading properties file from class path resource [jdbc.properties]>
2012-04-24 17:20:49,890 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@1b09468: defining beans [propertyConfigurer,dataSource,sqlMapClient,helloworddao,helloword,helloAspect,org.springframework.aop.config.internalAutoProxyCreator,pointcut,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,transactionManager,txAdvice,pointcutManager,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0]; root of factory hierarchy>
添加之前===before
2012-04-24 17:20:52,531 DEBUG [java.sql.Connection] - <{conn-100000} Connection>
2012-04-24 17:20:52,546 DEBUG [java.sql.Connection] - <{conn-100000} Preparing Statement:        select t.* from t_suppliers t WHERE Rownum <10     >
2012-04-24 17:20:52,843 DEBUG [java.sql.PreparedStatement] - <{pstm-100001} Executing Statement:        select t.* from t_suppliers t WHERE Rownum <10     >
2012-04-24 17:20:52,843 DEBUG [java.sql.PreparedStatement] - <{pstm-100001} Parameters: []>
2012-04-24 17:20:52,843 DEBUG [java.sql.PreparedStatement] - <{pstm-100001} Types: []>
2012-04-24 17:20:52,937 DEBUG [java.sql.ResultSet] - <{rset-100002} ResultSet>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Header: [ID, NAME]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [216150, 2011newSupplier8]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [48645, 包头市九福数码科技有限公司12]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [52400, 52400]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [52405, 包头市太平洋计算机科技有限公司]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [54057, 包头市遂通汽车销售服务有限公司]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [52425, 包头市大兴汽车贸易有限公司]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [44238, 呼和浩特云顿影视科技有限公司]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [45012, 包头市华城科教设备有限公司]>
2012-04-24 17:20:53,078 DEBUG [java.sql.ResultSet] - <{rset-100002} Result: [99, 无中标商]>
添加之后===after
id:216150  name:2011newSupplier8
id:48645  name:包头市九福数码科技有限公司12
id:52400  name:52400
id:52405  name:包头市太平洋计算机科技有限公司
id:54057  name:包头市遂通汽车销售服务有限公司
id:52425  name:包头市大兴汽车贸易有限公司
id:44238  name:呼和浩特云顿影视科技有限公司
id:45012  name:包头市华城科教设备有限公司
id:99  name:无中标商


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值