所需要的包如资源中
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">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.xzx.servlet.IbatisContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
...........................
</web-app>
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="mysqlDataSource"/>
</property>
</bean>
<bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test3"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:com/xzx/map/SqlMapConfig.xml</value>
<!-- <value>classpath:com/xzx/map/userMap.xml</value>-->
</property>
<property name="dataSource" ref="mysqlDataSource"/>
</bean>
<bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="proxyTargetClass" value="true"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="load*">readOnly</prop>
<prop key="get*">readOnly</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>
<bean id="userDAO" class="com.xzx.dao.UserDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="userService" class="com.xzx.business.UserServiceImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
<bean id="userServiceProxy" parent="baseTxService">
<property name="target">
<ref bean="userService"/>
</property>
</bean>
</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>
<sqlMap resource="com/xzx/map/userMap.xml"/>
</sqlMapConfig>
userMap.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 namespace="user">
<typeAlias alias="user" type="com.xzx.bean.User"/>
<typeAlias alias="score" type="com.xzx.bean.Score"/>
<resultMap id="userResult" class="user" >
<result property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<resultMap id="userAndScoreResult" class="user" >
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="score" column="id" select="getScoreByUid"/>
</resultMap>
<resultMap id="scoreMap" class="score">
<result property="cid" column="cid"/>
<result property="uid" column="uid"/>
<result property="score" column="score"/>
</resultMap>
<select id="selectAll" resultMap="userResult">
select * from user
</select>
<select id="selectByScore" resultMap="userAndScoreResult">
select u.id,u.name,s.score from user u right join score s on u.id = s.uid
</select>
<select id="selectUserById" parameterClass="int" resultMap="userResult">
select * from user where id = #id#
</select>
<select id="getScoreByUid" parameterClass="int" resultClass="int">
select score from score where uid = #uid#
</select>
<delete id="deleteUserById" parameterClass="int">
delete from user where id = #id#
</delete>
<insert id="addUser" parameterClass="user">
insert into user (name) values(#name#)
</insert>
<update id="updateUser" parameterClass="user">
update user set name=#name# where id = #id#
</update>
</sqlMap>
这里没有集成struts。因此用监听来初始化并获得bean
package com.xzx.servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class IbatisContextListener implements ServletContextListener{
private static WebApplicationContext wac = null;
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
loadBeanConfig(arg0);
}
public void loadBeanConfig(ServletContextEvent event){
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
}
public static Object getBeanByName(String beanName){
return wac.getBean(beanName);
}
}
然后servlet中调用如下
UserService us = (UserService)IbatisContextListener.getBeanByName("userServiceProxy");
User user = new User();
user.setName("ibatisandspring-name");
try {
// us.addUser(user);
User u = us.getUserById(10);
System.out.println(u.getName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ibatis与spring的集成
最新推荐文章于 2024-08-31 18:03:44 发布