iBATIS与Spring的集成

构造JavaBean如下(get,set方法省略):

public class Student {
	private Integer stu_id;

	private String stu_name;

	private Integer stu_age;

	private float stu_score;

	private Date stu_birth;
}

 在JavaBean包里新建一个Student.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="student">

	<typeAlias alias="Student" type="com.mengya.bean.Student" />

	<insert id="save" parameterClass="Student">
		insert into student
		values(#stu_id#,#stu_name#,#stu_age#,#stu_score#,#stu_birth#)
	</insert>

	<delete id="delStuByID" parameterClass="int">
		delete from student where stu_id = #stu_id#
	</delete>

	<update id="update" parameterClass="Student">
		update student set
		stu_name=#stu_name#,stu_age=#stu_age#,stu_score=#stu_score#,stu_birth=#stu_birth#
		where stu_id=#stu_id#
	</update>

	<select id="queryAll" resultClass="Student">
		select * from student
	</select>

	<select id="queryById" resultClass="Student" parameterClass="int">
		select * from student where stu_id = #stu_id#
	</select>

</sqlMap>

 构造dao接口如下:

public interface StudentDao {
	public void save(Student stu);

	public void delete(int stu_id);

	public void update(Student stu);

	public Student queryByPK(int stu_id);

	public List<Student> queryAll();
}

 对dao的实现:继承Spring提供的org.springframework.orm.ibatis.support.SqlMapClientDaoSupport

public class StudentDaoImple extends SqlMapClientDaoSupport implements
		StudentDao {

	public void delete(int stu_id) {
		getSqlMapClientTemplate().delete("delStuByID", stu_id);
	}

	public List<Student> queryAll() {
		return getSqlMapClientTemplate().queryForList("queryAll");
	}

	public Student queryByPK(int stu_id) {
		return (Student) getSqlMapClientTemplate().queryForObject("queryById",
				stu_id);
	}

	public void save(Student stu) {
		getSqlMapClientTemplate().insert("save", stu);
	}

	public void update(Student stu) {
		getSqlMapClientTemplate().update("update", stu);
	}

}

 构造service接口:

public interface StudentService {
	public void save(Student stu);

	public void delete(int stu_id);

	public void update(Student stu);

	public Student queryByPK(int stu_id);

	public List<Student> queryAll();
}

 对service的实现:

public class StudentServiceImple implements StudentService {

	private StudentDao studao;

	public void setStudao(StudentDao studao) {
		this.studao = studao;
	}

	public void delete(int stu_id) {
		this.studao.delete(stu_id);
	}

	public List<Student> queryAll() {
		return studao.queryAll();
	}

	public Student queryByPK(int stu_id) {
		return studao.queryByPK(stu_id);
	}

	public void save(Student stu) {
		studao.save(stu);
	}

	public void update(Student stu) {
		studao.update(stu);
	}

}

 iBATIS的sqlMapConfig.xml配置如下:

<!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/mengya/bean/Student.xml" />
</sqlMapConfig>

 Spring的applicationContext.xml配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	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.5.xsd
           	http://www.springframework.org/schema/context           
           	http://www.springframework.org/schema/context/spring-context-2.5.xsd
           	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/mp"></property>
		<property name="username" value="root"></property>
		<property name="password" value="123"></property>
		<property name="maxActive" value="10"></property>
		<property name="minIdle" value="2"></property>
		<property name="maxWait" value="300"></property>
	</bean>

	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation"
			value="classpath:sqlMapConfig.xml">
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save" propagation="REQUIRED" />
			<tx:method name="delete" propagation="REQUIRED" />
			<tx:method name="update" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="allMethod"
			expression="execution(* com.mengya.service.imple.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" />
	</aop:config>

	<bean id="studao" class="com.mengya.dao.imple.StudentDaoImple">
		<property name="sqlMapClient" ref="sqlMapClient"></property>
	</bean>

	<bean id="stuMght"
		class="com.mengya.service.imple.StudentServiceImple">
		<property name="studao" ref="studao"></property>
	</bean>

	<bean id="studentAction" class="com.mengya.web.StudentAction"
		scope="prototype">
		<property name="stuMght" ref="stuMght"></property>
	</bean>

</beans>

 Struts2的action如下:

public class StudentAction extends ActionSupport {
	private StudentService stuMght;

	private Student stu;

	private List<Student> stuList;

	public StudentService getStuMght() {
		return stuMght;
	}

	public void setStuMght(StudentService stuMght) {
		this.stuMght = stuMght;
	}

	public String list() {
		this.stuList = stuMght.queryAll();
		return "list";
	}

	public Student getStu() {
		return stu;
	}

	public void setStu(Student stu) {
		this.stu = stu;
	}

	public List<Student> getStuList() {
		return stuList;
	}

	public void setStuList(List<Student> stuList) {
		this.stuList = stuList;
	}

}

struts.xml的配置如下:

<struts>
	<constant name="struts.i18n.encoding" value="gbk" />
	<constant name="struts.objectFactory" value="spring" />
	<package name="mengya" extends="struts-default">
		<action name="student_*" class="studentAction" method="{1}">
			<result name="list">/list.jsp</result>
		</action>
	</package>
</struts>

 web.xml配置如下:

<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>springEncoding</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>gbk</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>springEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值