ibatis和Spring整合的详细例子,数据库用的是mysql,开发环境是Eclipse3.2:
1.首先把用到的包导入进来,此例用的是spring-framework-1.2.7,iBATIS_DBL-2.1.7.597,mysql的数
据库连接包用的是mysql-connector-java-5.0.3-bin.jar.
2.建POJO类,在此我们用的是一个Student类.
package cn.itcast;
public class Student implements java.io.Serializable {
private Integer id;
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
3.POJO的映射文件Student.xml,在这里面只有两个功能,即根据student的id检索出一个Student对象,另一
个就是向数据库插入一条记录(一个Student对象),注意:此应用程序中所有的配置文件(xml文件和
properties文件都放在configfile包下面).
<?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 ">
<!--这是POJO映射文件的根元素-->
<sqlMap namespace="Student">
<!--select元素的id属性用来标识此元素,resultClass属性的值是Java类的全限定名(即包括类的包名)。
resultClass属性可以让您指定一个Java类,根据ResultSetMetaData将其自动映射到JDBC的ResultSet。
只要是Java Bean的属性名称和ResultSet的列名匹配,属性自动赋值给列值。
parameterClass属性是参数的类型,此属性的值是Java类的全限定名(即包括类的包名)。
它是可选的,但强烈建议使用。它的目的是 限制输入参数的类型为指定的Java类,并
优化框架的性能。-->
<select id="getStudentById" resultClass="cn.itcast.Student" parameterClass="int">
select id,firstname,lastname from student where id=#value#
</select>
<insert id="insertStudent" parameterClass="cn.itcast.Student">
insert into student(firstname,lastname) values(#firstname#,#lastname#)
</insert>
</sqlMap>
4.建一个SqlMap的配置文件sql-map-config.xml,sqlMap元素的resource属性告诉Spring去哪找POJO映射文件.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd ">
<sqlMapConfig>
<sqlMap resource="configfile/Student.xml" />
</sqlMapConfig>
5.jdbc.properties文件,存储数据库连接的driver,url,username,password等信息,
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/itcast
jdbc.username=root
jdbc.password=
6.Spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd ">
<beans>
<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句-->
<bean id="propertyConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>configfile/jdbc.properties</value>
</property>
</bean>
<!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<!--根据dataSource和configLocation创建一个SqlMapClient-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>configfile/sql-map-config.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!--根据sqlMapClien创建一个SqlMapClient模版类-->
<bean id="sqlMapClientTemplate"
class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!--将上面的模版类织入到我们的DAO对象中-->
<bean id="studentDao" class="cn.itcast.StudentDaoSqlMap">
<property name="sqlMapClientTemplate">
<ref bean="sqlMapClientTemplate" />
</property>
</bean>
</beans>
7.StudentDaoSqlMap 是一个DAO,它负责和数据库的交互,在这里实现了查询单条记录和插入单条记录的功能.
package cn.itcast;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
public class StudentDaoSqlMap {
//[1]
private SqlMapClientTemplate sqlMapClientTemplate;
public SqlMapClientTemplate getSqlMapClientTemplate() {
return sqlMapClientTemplate;
}
public void setSqlMapClientTemplate(
SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
//此方法的返回值与Student.xml的select元素的resultClass对应.
public Student getStudent(Integer id) {
return (Student) sqlMapClientTemplate.queryForObject("getStudentById",id);
//注意:queryForObject方法返回一个Object,第一个参数与Student.xml的select元素
//的id属性值对应,第二个参数的类型与Student.xml的select元素的parameterClass
//属性值对应.
}
public Object insertStudent(Student student) {
return sqlMapClientTemplate.insert("insertStudent", student);
}
}
8.下面写一个带main函数的类来测试上面的代码.代码非常简单就不再解释了.
package cn.itcast;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
//[2]
ApplicationContext factory = new ClassPathXmlApplicationContext(
"applicationContext.xml");
StudentDaoSqlMap studentDao = (StudentDaoSqlMap) factory
.getBean("studentDao");
//插入一个student
Student student = new Student();
student.setFirstname("tian");
student.setLastname("xiangdong");
studentDao.insertStudent(student);
//查询出id是1的Student对象.
//Student student = studentDao.getStudent(1);
//System.out.println(student.getId());
//System.out.println(student.getFirstname());
//System.out.println(student.getLastname());
}
}
[2]spring在web里还是用WebApplicationContextUtils.getWebApplicationContext()获取上下文,配置文件放到WEB-INF文件夹下比较舒服