Spring整合iBatis

本文介绍了一个使用Spring框架整合iBatis的示例应用,包括配置文件、DAO接口及实现类等内容。该示例展示了如何配置数据源、事务管理、SQL映射等关键组件,并通过一个具体的Person实体类操作实例说明了整个流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.applicationContext-common.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-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="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="system"/>
<property name="password" value="xiajinjin"/>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<!-- 配置哪些类的哪些方法进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.xiajin.dao.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>

<!-- SqlMapClient对象的配置 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"><value>classpath:sqlMapConfig.xml</value></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

2.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 lazyLoadingEnabled="true" useStatementNamespaces="true" />
<sqlMap resource="com/xiajin/maps/Person.xml" />
</sqlMapConfig>


3.Person.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="Person">

<typeAlias alias="Person" type="com.xiajin.model.Person" />
<!-- Use primitive wrapper type (e.g. Integer) as parameter and allow results to
be auto-mapped results to Person object (Java Bean) properties -->
<select id="getPerson" parameterClass="int"
resultClass="com.xiajin.model.Person">
SELECT id as id, first_Name as firstName, lastName as lastName,
weightInKilograms as weightInKilograms, heightInMeters as
heightInMeters FROM PERSON WHERE id = #id#
</select>
<insert id="insertPerson" parameterClass="Person">
<selectKey resultClass="int" keyProperty="id">
<![CDATA[SELECT PERSON_SEQUENCE.NEXTVAL AS ID FROM DUAL]]>
</selectKey>
insert into
person(id,first_Name,lastName,weightInKilograms,heightInMeters)
values(#id#,#firstName#,#lastName#,#weightInKilograms#,#heightInMeters#)
</insert>

</sqlMap>



4.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

5.IPersonDAO.java
public interface IPersonDAO {

public void savePerson(Person person);
}


6.IPersonDAOImpl.java
]public class IPersonDAOImpl extends SqlMapClientDaoSupport implements IPersonDAO {

@Override
public void savePerson(Person person) {

getSqlMapClientTemplate().insert(person.getClass().getSimpleName()+".insertPerson",person);

}

}

7.applicationContext-beans.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-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="personDAOImpl" class="com.xiajin.dao.impl.IPersonDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>


</beans>



8.Spring_iBatis.java

public class Spring_iBatis {

private static BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");

public static void main(String[] args) {
IPersonDAO personDAO=(IPersonDAO)factory.getBean("personDAOImpl");
Person person=new Person();
person.setFirstName("abcd");
person.setLastName("efghi");
person.setWeightInKilograms(2);
person.setHeightInMeters(4);
personDAO.savePerson(person);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值