构造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>
分享到:
相关推荐
《iBATIS-SqlMaps-2_cn.rar》是一款关于iBATIS框架的中文教程资源,主要针对想要深入了解和使用iBATIS与Spring集成的初学者。这个压缩包包含了一个PDF文档,即《iBATIS-SqlMaps-2_cn.pdf》,它是iBATIS SQL Maps 2的...
通过编写单元测试或集成测试来验证ibatis与Spring的整合是否成功。 #### 五、总结 通过以上步骤,我们可以将ibatis与Spring框架很好地整合在一起,利用Spring的强大功能来简化ibatis的配置和管理。这种整合方式...
Spring与iBATIS的集成 iBATIS似乎已远离众说纷纭的OR框架之列,通常人们对非常流行的Hibernate情有独钟。但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比...
Spring通过DAO模式提供了对iBATIS的集成,使得SqlMapClient对象能够被Spring管理。SqlMapClient是iBATIS的核心,负责执行SQL映射文件中的SQL语句。在Spring中,我们可以配置Spring容器来创建并管理SqlMapClient实例...
5. **iBatis与Spring集成**:配置Spring的SqlSessionFactoryBean,将SqlSessionTemplate或SqlSessionDaoSupport注入到需要使用iBatis的类中。 6. **测试与调试**:编写JUnit测试用例,确保各个组件的正常工作,逐步...
7. **测试与调试**:整合完成后,可以通过单元测试或集成测试验证Ibatis 和 Spring 是否能正常交互,确保SQL执行、事务处理和对象映射等功能都按预期工作。 通过这样的整合,我们可以充分利用Ibatis的灵活性和...
在"struts2+ibatis+spring集成"的例子中,这三者通常是这样协同工作的: 1. **Spring** 配置:首先,需要配置Spring的ApplicationContext,声明Struts2和iBatis的相关bean,如Action类、DAO接口及其实现、...
"iBATIS学习总结 - 郭睿的专栏 - CSDN.NET_files"和"iBATIS与Spring集成及环境搭建 - 振华 - ITeye技术网站_files"可能是相关文章的图片或辅助资源。 通过这些资料,开发者可以系统地学习和掌握iBATIS 2.x版本的...
最近想在最新的Spring5.0中集成ibatis(不是mybatis),发现已经不在支持SqlmapClientTemplate和SqlmapClientFactoryBean,于是搞了这个工具jar来进行支持如下配置 <bean id="sqlMapClient" class="org.spring...
以上就是 Spring 与 iBATIS 整合集成的主要步骤和知识点。通过这样的集成,可以利用 Spring 的强大功能管理和协调整个应用程序,同时利用 iBATIS 的灵活性处理数据库操作,实现高效的企业级应用开发。在实际项目中,...
5. **整合iBatis与Spring**:通过Spring的SqlSessionFactoryBean,配置数据源和MyBatis的配置文件,将DAO接口与Mapper XML关联起来。 6. **部署与测试**:将所有配置文件、类库和应用代码打包成WAR文件,部署到应用...
5. 将iBatis与Spring集成:使用MyBatis-Spring的MapperScannerConfigurer扫描Mapper接口,Spring会自动创建对应的MapperFactoryBean。 通过这个"Struts1+Spring+iBatis-jar包",开发者可以避免手动下载和管理各个...
4. 集成iBATIS:在Spring配置文件中配置SqlSessionFactory,指定MyBatis的配置文件和数据源。在Mapper接口中定义数据库操作方法,并在XML文件中编写对应的SQL语句。 5. 测试与运行:完成上述配置后,可以创建测试...
4. **事务管理**:`Spring`的事务管理器可以与`iBatis`无缝集成。通过配置`PlatformTransactionManager`,我们可以实现全局的事务控制,确保数据的一致性。在业务层方法上添加`@Transactional`注解,即可开启事务,...
### Spring与iBatis集成开发详解 #### 一、引言 在Java企业级应用开发领域,Spring框架因其强大的依赖注入(DI)和面向切面编程(AOP)能力而备受青睐;而iBatis(现称为MyBatis)则以其简洁的SQL映射功能而闻名。...