`
xiajin2080
  • 浏览: 35984 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论
  • Airflare: 你这也不对啊,各种报错啊!DocumentHelper哪来的啊 ...
    XML读写
  • myali88: 为什么我用$.getJSON请求返回的始终是一个JSON格式的 ...
    jQuery+JSON

Spring整合iBatis

阅读更多
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);
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics