1:导入spring与ibatis需要用的包。
2:新建一个POJO类。例如:
1. package cn.itcast;
2.
3. public class Student {
4. private Integer id;
5.
6. private String firstname;
7.
8. private String lastname;
9.
10. public String getFirstname() {
11. return firstname;
12. }
13.
14. public void setFirstname(String firstname) {
15. this.firstname = firstname;
16. }
17.
18. public Integer getId() {
19. return id;
20. }
21.
22. public void setId(Integer id) {
23. this.id = id;
24. }
25.
26. public String getLastname() {
27. return lastname;
28. }
29.
30. public void setLastname(String lastname) {
31. this.lastname = lastname;
32. }
33. }
3:根据这个POJO类写一个sqlmap_student.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="studetnt">
<select id="select.student" resultClass="cn.itcast.Student" parameterClass="java.lang.Integer">
select id,firstname,lastname from student where id=#value#
</select>
<insert id="insertStudent" parameterClass="cn.itcast.Student">
insert into student
(
firstname,
lastname
)
values
(
#firstname#,
#lastname#
)
</insert>
</sqlMap>
4:创建一个总的sqlMap-config.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"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
maxRequests="128"
maxSessions="64"
maxTransactions="32"
useStatementNamespaces="false"/>
<!-- ================================================================ -->
<!-- 导入配置资源 ( 数据库模块配置文件) -->
<!-- ================================================================ -->
<sqlMap resource="sqlmap-student.xml" />
</sqlMapConfig>
5:创建一个有关数据源的properties文件取名为:jdbc.properties
# Properties file
datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
datasource.username=omsdata
datasource.password=omsdata_2010
datasource.maxActive=100
datasource.maxIdle=10
datasource.maxWait=120000
datasource.whenExhaustedAction=1
datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false
6:配置spring的配置文件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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
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.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.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">
<!-- ==================================================== -->
<!-- 配置多资源文件 -->
<!-- ==================================================== -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:meyacom.properties</value>
</list>
</property>
</bean>
<!-- ==================================================== -->
<!-- 配置自动代理事务,拦截系统所有服务方法 -->
<!-- ==================================================== -->
<aop:config>
<aop:advisor
pointcut="execution(* com.meyacom.core.*.biz.service.*.*(..))"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*" />
<tx:method name="save*" />
<tx:method name="create*" />
<tx:method name="add*" />
<tx:method name="update*" />
<tx:method name="modify*" />
<tx:method name="delete*" />
<tx:method name="remove*" />
<tx:method name="import*" />
<tx:method name="export*" />
<tx:method name="execute*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- ==================================================== -->
<!-- 系统数据访问层公共模块配置文件 (包括:增、删、改、查) -->
<!-- ==================================================== -->
<bean id="dao" class="com.meyacom.core.common.integration.dao.impl.CMDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<!-- ==================================================== -->
<!-- 系统服务层公共模块配置文件 (包括:增、删、改、查) -->
<!-- ==================================================== -->
<bean id="service"
class="com.meyacom.core.common.biz.service.impl.CMServiceImpl">
<property name="dao" ref="dao" />
</bean>
<!-- ==================================================== -->
<!-- 导入系统配置文件模块配置文件 (包括:增、删、改、查) -->
<!-- ==================================================== -->
<import resource="classpath:application-core.xml" />
<!--这样写的好处是不会将所有的文件统一的放到applicationContext中,而是分开放。增强代码的可读-->
</beans>
第二个spring配置文件为:
application-core.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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
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.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.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">
<!-- ==================================================== -->
<!-- 配置多资源文件 -->
<!-- ==================================================== -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:meyacom-jdbc.properties</value>
</list>
</property>
</bean>
<!-- ==================================================== -->
<!-- 连接数据源的资源配置文件(连接地址、用户、密码、大小等) -->
<!-- ==================================================== -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${datasource.driverClassName}"/>
<property name="url" value="${datasource.url}"/>
<property name="username" value="${datasource.username}"/>
<property name="password" value="${datasource.password}"/>
<property name="maxActive" value="${datasource.maxActive}" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:meyacom-sqlmap.xml</value>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="lobHandler" ref="lobHandler" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="lobHandler" lazy-init="true"
class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
<!-- ==================================================== -->
<!-- 系统审查日志公共模块配置文件 (包括:增、删、改、查) -->
<!-- ==================================================== -->
<bean id="auditLogger" class="org.apache.log4j.Logger">
<constructor-arg index="0" value="auditLogger" />
</bean>
<!-- ==================================================== -->
<!-- 系统日志输出公共模块配置文件 (包括:增、删、改、查) -->
<!-- ==================================================== -->
<bean id="errorLogger" class="org.apache.log4j.Logger">
<constructor-arg index="0" value="errorLogger" />
</bean>
</beans>
7然后写基层的接口,和service,最后调用,基层接口和service我已经放到了spring的配置文件中。可以不用配置了。