| 1 | | 单元测试应用示例——测试工具类 |
| | 1 | == 单元测试应用示例——测试工具类 == |
| | 2 | 工具类cn.com.pcgames.gamehall2.util.SqlHelper是游戏大厅DAO层辅助工具类.例如:SqlHelper.buildInsert(List<FieldDesc> fields, String tablename,String[] excludes);[[BR]] |
| | 3 | 根据数据库表元信息,表名生成sql插入语句,使用示例代码如cn.com.pcgames.gamehall2.repository.impl.GameDAOImpl[[BR]] |
| | 4 | |
| | 5 | {{{ #!java |
| | 6 | public class GameDAOImpl extends AbstractRepository<Game> implements GameDAO { |
| | 7 | |
| | 8 | private List<FieldDesc> tableFields; |
| | 9 | |
| | 10 | public void setTableFields(List<FieldDesc> tableFields) { |
| | 11 | this.tableFields = tableFields; |
| | 12 | } |
| | 13 | |
| | 14 | public void init() { |
| | 15 | this.tableFields = SqlHelper.listFields(simpleJdbcTemplate, "game"); |
| | 16 | } |
| | 17 | |
| | 18 | public GameDAOImpl() { |
| | 19 | super(Game.class); |
| | 20 | } |
| | 21 | |
| | 22 | public long createGame(Game game) { |
| | 23 | long gameId = idGenerator.generate("game", "gameId"); |
| | 24 | game.setGameId(gameId); |
| | 25 | //SqlHelper生成插入语句 |
| | 26 | String sql = SqlHelper.buildInsert(tableFields, "game", null); |
| | 27 | Object[] params = SqlHelper.getParameters(game, tableFields, null); |
| | 28 | int result = simpleJdbcTemplate.update(sql, params); |
| | 29 | |
| | 30 | return result == 1 ? gameId : 0; |
| | 31 | } |
| | 32 | |
| | 33 | ...... |
| | 34 | } |
| | 35 | }}} |