首先需要以下这些jar包:commons-dbcp.jar, commons-logging.jar, commons-pool.jar, ibatis-2.3.0.677.jar, ojdbc14_g.jar, spring.jar
1.sqlMapConfig.xml文件代码:
<?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="user.xml" />
</sqlMapConfig>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@10.2.46.41:1521:orcl</value>
</property>
<property name="username">
<value>jpet</value>
</property>
<property name="password">
<value>1234</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>SqlMapConfig.xml</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="userDAO" class="UserDAO">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="sqlMapClient">
<ref local="sqlMapClient" />
</property>
</bean>
<bean id="userDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="userDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
3.user.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>
<cacheModel id="oneDayProduct" type="MEMORY">
<flushInterval hours="24" />
<property name="reference-type" value="WEAK" />
</cacheModel>
<resultMap id="result" class="User">
<result property="productId" column="productid" columnIndex="1" />
<result property="name" column="name" columnIndex="2" />
<result property="description" column="descn" columnIndex="3" />
<result property="categoryId" column="category" columnIndex="4" />
</resultMap>
<select id="getUserByName" resultMap="result">
select productid, name,
descn, category from product where productid = #value#
</select>
</sqlMap>
4.User.java文件代码如下
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String productId;
private String categoryId;
private String name;
private String description;
/* JavaBeans Properties */
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId.trim();
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
/* Public Methods */
public String toString() {
return getName();
}
}
5.UserDAO.java文件代码
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class UserDAO extends SqlMapClientDaoSupport {
public User getUser(String username) throws DataAccessException {
return (User) getSqlMapClientTemplate().queryForObject(
"getUserByName", username);
}
}
6.Test.java文件代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UserDAO userDAO = (UserDAO) app.getBean("userDAO");
User user = userDAO.getUser("FI-SW-01");
System.out.println(user);
}
}