<?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);
}
}