[SQL]Rowmapper to query SQL in JDBC

阅读更多
@Repository("DaoImpl")
public class DaoImpl implements IDao {

	private static Logger dbLogger = LoggerFactory.getLogger("DBDOWN_ALERTLOG");
	private static final Logger logger = LoggerFactory.getLogger(DaoImpl.class);
	
	@Autowired
	@Qualifier("jdbcTemplate")
	JdbcTemplate jdbcTemplate;
	
	@Override
	public list query() {
		try {
			logger.info("-----Start to query DB for all user's privilege------");
			List list = jdbcTemplate.query(Constant.Query, new RowMapper() {
				
					@Override
					public UserMode mapRow(ResultSet rs, int rowNum) throws SQLException {
						
							UserMode user = new UserMode();
                                                        String username = rs.getString(Constant.username);
							.....	
							
							user.setUsername(username);
						....
						
						}
						
						logger.info("-----End to query DB for all user's privilege------");	
						return user;
					}
					
				});
				
				return list;
		}catch (DataAccessException e) {
			dbLogger.error("DB is down. Exception is [{}]", e.getMessage());
			throw e;
		} catch (Exception e) {
			logger.error("-- Error occur, exception is [{}]", e.getMessage());
			throw e;
		}
		
	}

 

你可能感兴趣的:(jdbc,RowMapper)