| Version 2 (modified by chenyang, 14 years ago) (diff) |
|---|
单元测试应用示例——测试工具类
工具类cn.com.pcgames.gamehall2.util.SqlHelper是游戏大厅DAO层辅助工具类.例如:SqlHelper?.buildInsert(List<FieldDesc?> fields, String tablename,String[] excludes);
根据数据库表元信息,表名生成sql插入语句,使用示例代码如cn.com.pcgames.gamehall2.repository.impl.GameDAOImpl
public class GameDAOImpl extends AbstractRepository<Game> implements GameDAO {
private List<FieldDesc> tableFields;
public void setTableFields(List<FieldDesc> tableFields) {
this.tableFields = tableFields;
}
public void init() {
this.tableFields = SqlHelper.listFields(simpleJdbcTemplate, "game");
}
public GameDAOImpl() {
super(Game.class);
}
public long createGame(Game game) {
long gameId = idGenerator.generate("game", "gameId");
game.setGameId(gameId);
//SqlHelper生成插入语句
String sql = SqlHelper.buildInsert(tableFields, "game", null);
Object[] params = SqlHelper.getParameters(game, tableFields, null);
int result = simpleJdbcTemplate.update(sql, params);
return result == 1 ? gameId : 0;
}
......
}
![(please configure the [header_logo] section in trac.ini)](http://www1.pconline.com.cn/hr/2009/global/images/logo.gif)