spring所需包:
——————————————————————————————
dist\spring.jar //spring核心包
lib\jakarta-commons\commons-logging.jar//spring日志所需包
lib\log4j\log4j-1.2.15.jar //log4j日志包使用该包需要配置properties文件 具体配置参考?
lib\aspectj\aspectjrt.jar //AOP编程必备包
lib\aspectj\aspectjweaver.jar //AOP编程必备包
以上包为spring配置基本包。
lib\junit\junit-4.4.jar //如需单元测试,加入此包
lib\jakarta-commons\commons-dbcp.jar //DBCP缓冲池框架整合ibatis所需包
lib\jakarta-commons\commons-pool.jar //Apache的通用池Jar包,如果不加入将出现不能创建datasourcebean
dist\resources\spring-beans-2.5.xsd //spring在eclipse中配置提示必备文件 配置xml catalog
——————————————————————————————
ibatis所需包:
——————————————————————
ibatis2.3.4.8.jar //ibatis必备包
log4j-1.2.16.jar //log4j日志输出包(与spring中的log4j包任选一个)
mysql-connector-java-5.1.6-bin.jar //mysql数据库驱动包
——————————————————————
Spring的ibatis配置文件beans-ibatis.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- DHCP方式配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="client" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource"ref="dataSource"/> <property name="configLocation"value="classpath:SqlMapConfig.xml"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"ref="dataSource"/> </bean> <bean id="userDAO" class="dao.imp.UserDAOIMP"> <property name="sqlMapClient"> <ref local="client"/> </property> </bean> <bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="target"> <ref local="userDAO" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> </beans>
SqlMap.xml配置文件:User.xml
<?xml version="1.0"encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQLMap 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap namespace="User"> <typeAlias alias="user"type="vo.User"/> <select id="selectUser"parameterClass="java.lang.String" resultClass="user"> select* from t_user where name = #name# </select> <select id="getUser" parameterClass="java.lang.String"resultClass="user"> <![CDATA[ select name, sex from t_user where name =#name# ]]> </select> <update id="updateUser"parameterClass="user"> <![CDATA[ UPDATE t_user SET name=#name#,sex=#sex#WHERE id = #id# ]]> </update> <insert id="insertUser"parameterClass="user" > INSERT INTO t_user (id,name,sex) VALUES(#id#,#name#,#sex#) </insert> <delete id="deleteUser"parameterClass="java.lang.String"> delete from t_user where id = #value# </delete> </sqlMap>
整合前itabits的SqlMapConfig.xml文件配置:
<?xml version="1.0"encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTDSQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <!--<properties resource="com/ibatis/sample/SqlMap.properties"/>--> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false" /> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test"/> <property name="JDBC.Username" value="root"/> <property name="JDBC.Password" value="root"/> <property name="Pool.MaximumActiveConnections" value="10"/> <property name="Pool.MaximumIdleConnections" value="5"/> <property name="Pool.MaximumCheckoutTime" value="120000"/> <property name="Pool.TimeToWait" value="500"/> <property name="Pool.PingQuery" value="select 1 from ACCOUNT"/> <property name="Pool.PingEnabled" value="false"/> <property name="Pool.PingConnectionsOlderThan" value="1"/> <property name="Pool.PingConnectionsNotUsedFor" value="1"/> </dataSource> </transactionManager> <sqlMap resource="com/ibatis/sample/User.xml"/> </sqlMapConfig> 整合后SqlMapConfig.xml文件配置: <?xml version="1.0"encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTDSQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <sqlMap resource="vo/User.xml"/> </sqlMapConfig>
Spring使用ibatis功能代码:
BeanFactory bf = (BeanFactory)(newClassPathXmlApplicationContext("beans-ibatis.xml")); UserDAOuserdao = (UserDAO)bf.getBean("userDAOProxy"); User user = new User(); user.setId(0); user.setName("aaa"); user.setSex(1); userdao.insert(user);
UserDAO的实现类UserDAOIMP,调用插入方法
@Override publicvoid insert(User user) { System.out.println("UserDAOIMP的insert(user)方法被调用"); getSqlMapClientTemplate().update("insertUser",user); }