第一步:sql-map-spring-config.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close">
<property name="driver">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="driverUrl">
<value>jdbc:mysql:///test</value>
</property>
<property name="user">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
<property name="prototypeCount">
<value>5</value>
</property>
<property name="maximumConnectionCount">
<value>10</value>
</property>
<property name="minimumConnectionCount">
<value>2</value>
</property>
<property name="trace">
<value>true</value>
</property>
<property name="verbose">
<value>true</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlmap-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDao" class="com.warefont.dao.EmployeeDaoSpring">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
</beans>
第二步:sqlmap-config.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/warefont/pojo/EmployeeSpring.xml"/>
</sqlMapConfig>
第三步:EmployeeSpring.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="employee">
<insert id="insert_employee" parameterClass="com.warefont.pojo.Employee">
insert into employee(name,gender,department) values
(#name#,#gender#,#department#)
</insert>
<select id="getEmployeet" resultClass="com.warefont.pojo.Employee">
select id, name, gender,department from employee
</select>
<delete id="delEmployee" parameterClass="int">
delete from employee where id=#value#
</delete>
<update id="updateEmployee" parameterClass="com.warefont.pojo.Employee">
update employee set name=#name#,gender=#gender#,department=#department#
where id=#id#
</update>
</sqlMap>
第四步:应用
package com.warefont.dao;
import java.sql.SQLException;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.warefont.pojo.Employee;
import com.warefont.util.*;
public class EmployeeDaoSpring extends SqlMapClientDaoSupport{
public void insertEmp(Employee emp){
getSqlMapClientTemplate().insert("insert_employee", emp);
}
}
注意这里的继承类。
第五步:配置文件注册到程序中
package com.warefont.util;
import java.io.File;
import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
* User: ChenZQ
* Date: 2009-10-20
* Time: 12:28:31
*/
public class SpringHelper {
private static ApplicationContext context;
private static String configFile = "classpath:sql-map-spring-connfig.xml";
public static void setConfigFile(String file) {
configFile = file;
}
private SpringHelper() {
}
public static ApplicationContext getContext() {
if (context == null) {
context = new FileSystemXmlApplicationContext(configFile);
}
return context;
}
public static Object getBean(String beanName) {
return getContext().getBean(beanName);
}
}
第六步:测试
package com.warefont.test;
import com.warefont.dao.EmployeeDao;
import com.warefont.dao.EmployeeDaoSpring;
import com.warefont.pojo.Employee;
import com.warefont.util.SpringHelper;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Employee emp = new Employee();
emp.setDepartment("dept");
emp.setGender("testGender");
emp.setName("hewei00");
//EmployeeDao empDao = new EmployeeDao();
//empDao.insertEmp(emp);
EmployeeDaoSpring d = (EmployeeDaoSpring) SpringHelper.getBean("employeeDao");
d.insertEmp(emp);
}
}
这个是使用spring的ibaties应用,个人感觉这个和spring的JdbcTemplate使用一个样子的