請教關於ibatis的問題

xiaowang發表於2006-08-21
小弟最近剛學IBATIS,打算用它用它做一個小應用.我是這麼使用SQLmap的,不知道對不對,還希望banq給點意見.
先定義一個類用於封裝SqlMap初試化程式.
public class BaseIbatisDao{

private static BaseIbatisDao instance = new BaseIbatisDao();
private static Logger log = Logger.getLogger(BaseIbatisDao.class.getName());
protected static final SqlMapClient sqlMap;

static{
try{
log.debug("Attemping to initialize SqlMap!!!");
String resource = "SqlMapConfig.xml";//在WEB-INF/classes目錄下查詢該檔案
Reader reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
log.debug("Initialized SqlMap!!!");
}catch(Exception e){
log.error("Error intializing BaseIbatisDao!!!", e);
e.printStackTrace();
throw new RuntimeException("Error initializing BaseIbatisDao.class Cause: " + e);
}
}
protected BaseIbatisDao(){

}
public static BaseIbatisDao getInstance(){
return instance;
}

}

接著就在具體類中這麼使用:
public class UserBaseIbatisDAOImpl extends BaseIbatisDao implements UserDAO{

Log logger = LogFactory.getLog(this.getClass());

public List getAllUser(){
List user = null;
try{
user = sqlMap.queryForList("User.getAll", null);
}catch(SQLException e){
logger.error("Error getting all User!!!", e);
}
return user;
}

public User getUser(String num){
User user = null;
try{
user = (User)sqlMap.queryForObject("User.getByNum", num);
}catch(SQLException e){
logger.error("Error getting User!!!", e);
}
return user;
}

public void insert(User user){
try{
sqlMap.insert("User.insert", user);
}catch(SQLException e){
logger.error("error inserting User!!!", e);
}
}


}
不知道這樣使用什麼問題.望大家指點指點.謝謝!

相關文章