1.加入spring相关架包以及ibatis相关一个架包
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-logging-1.1.1.jar
log4j-1.2.14.jar
spring.jar
c3p0-0.9.1.2.jar
ibatis.rar
2.主要配置
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:context="http://www.springframework.org/schema/context" 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-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 通过注解方式把类交给spring --> <context:component-scan base-package="com.ssi" /> <!-- 把Ibatis集成到spring --> <!-- 减少数据库连接 采用数据源 C3P0比DBCP(dbcp+pool)效率高一点 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="root" /> <property name="minPoolSize" value="1" /> <property name="maxPoolSize" value="20" /> <property name="maxIdleTime" value="25000" /> <property name="acquireIncrement" value="1" /> <property name="acquireRetryDelay" value="1000" /> <property name="acquireRetryAttempts" value="30" /> <property name="initialPoolSize" value="3" /> <property name="idleConnectionTestPeriod" value="1800" /> <property name="testConnectionOnCheckin" value="true" /> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:SqlMapConfig.xml</value> </property> <property name="useTransactionAwareDataSource"> <value>true</value> </property> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <!--Spring很重要的一个服务:配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 使用基于注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
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 cacheModelsEnabled="true" useStatementNamespaces="true"/> <!-- 此处开始添加SqlMap配置文件 --> <sqlMap resource="com/ssi/bean/Users.xml" /> </sqlMapConfig>
Users.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap> <!-- 该属性是为了减少书写com.elifefly.User的次数而已 --> <typeAlias alias="Users" type="com.ssi.bean.Users" /> <!-- 条件查询 parameterClass标识传入参数类型 忽略大小写 --> <select id="selectUserById" parameterClass="int" resultClass="Users"> select * from users where id=#id# </select> <!-- 带多个参数 查询--> <parameterMap class="java.util.HashMap" id="parameterMap"> <parameter property="name" /> <parameter property="password" /> </parameterMap> <select id="selectUserByMap" parameterMap="parameterMap" resultClass="Users"> select * from users where name=? and password=? </select> </sqlMap>
详细下载实例....