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);
}
}
分享到:
相关推荐
spring整合ibatis的详细配置步骤
代码中展示了spring整合ibatis框架,使其更好地发挥作用,适合了解过spring的初学者又继续想要学习ibatis框架的同学。
iBatis和Spring整合 iBatis和Spring整合
spring ibatis整合所需jar包
spring集成ibatis的源码 可以运行
spring+ibatis集成开发文档,集成框架。
spring与ibatis整合集成实例 spring与ibatis整合集成实例
Spring与iBATIS的集成 iBATIS似乎已远离众说纷纭的OR框架之列,通常人们对非常流行的Hibernate情有独钟。但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比...
Spring对IBatis的整合 10.3 Spring对IBatis的支持 Spring对IBatis提供了完善的内建支持。使用Spring提供的IBatis辅助类,可以大大简化原有的IBatis访问代码。这些辅助类位于org.springframework.orm.ibatis包下,...
Ibatis和Spring整合例子,实现增删改查功能.
NULL 博文链接:https://baomw.iteye.com/blog/831945
有关Struts2+Spring+Hibernate和Struts2+Spring+Ibatis的整合实例demo 原创,完全基于eclipse开发
Spring + Ibatis 与mysql集群集成
spring_ibatis整合案例
struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例
Spring与iBATIS的集成示例代码,希望对初学者有用
struts2 + spring + ibatis 整合例子,仓库:search.maven.org
SPRING与IBATIS整合采用保留IBATIS事务,可自己控制回滚!