getting Single record from a relational DB
The Service:
The Service:
public Map<String, Object> getRatingDetailBySSO(//path or query parameters) throws Exception { String query = //provide SQL query here; return getSingleRecord(query,//pass your parameters here); }
The command:
public Map<String, Object> getSingleRecord(String sql, Object... params) { Handle h = null; try { h = dbi.open(); Map<String, Object> hm = new HashMap<String, Object>(); Map<String, Object> hmp = new HashMap<String, Object>(); hmp.put("total", 0); hm.put("data", hmp); long startTime = System.currentTimeMillis(); List<Map<String, Object>> rs = h.select(sql, params); long endTime = System.currentTimeMillis(); long duration = (endTime - startTime); if (rs == null || rs.size() < 1) return hm; Map<String, Object> row = rs.get(0); return row; } catch (Exception e) { //handle exception } finally { if (h != null) { h.close(); } } }
getting Multiple records from a relational DB
The Service
public List<Map<String, Object>> getRatingSummaryByBusiness(//query and path parameters here) throws Exception { String query = //SQL query; return getMultipleRecords(query, //pass parameters here); }
the command:
public List<Map<String, Object>> getMultipleRecords(String sql, Object... params) { Handle h = null; try { h = dbi.open(); Map<String, Object> hm = new HashMap<String, Object>(); Map<String, Object> hmp = new HashMap<String, Object>(); hm.put("data", hmp); long startTime = System.currentTimeMillis(); List<Map<String, Object>> rs = h.select(sql, params); long endTime = System.currentTimeMillis(); long duration = (endTime - startTime); h.close(); return rs; } catch (Exception e) { //handle exception here } finally { if (h != null) { h.close(); } }
Get single record from Mongo
Service
Path in a form "/data/{collection}/{object_name}/{object_id}", method = RequestMethod.GET public Map<String, Object> getObject( //path parames here) throws Exception { Map<String, Object> entity = new HashMap<String, Object>(); entity.put(//search criteria); entity.put("collection", collection); entity.put("data", getCollectionObjectByCriteria(//search criteria, collection, objectName, objectId)); return entity; }
public Map<String, Object> getCollectionObjectByCriteria(String criteria, String collectionName, String objectName, String objectId) throws Exception { MongoClient mongo = mongoClient; MongoDatabase sourcingDB = mongo.getDatabase(mongoDb); MongoCollection<Document> collection = sourcingDB.getCollection(collectionName); BasicDBObject query = new BasicDBObject(//pass your criteria here); query.append(objectName, objectId); TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {}; Map<String, Object> res = new HashMap<String, Object>(); for (Document d : collection.find(query)) { if (d != null) { res = ((HashMap<String, Object>) mapper.readValue(d.toJson(), typeRef)); } } return res; }