コード例 #1
0
 @RequestMapping(
     value = {"/dump"},
     method = RequestMethod.GET)
 public String dump(Model model) {
   String sqlInfo = "?";
   String sqlData = "";
   JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
   try {
     sqlInfo =
         (String)
             JdbcUtils.extractDatabaseMetaData(
                 dataSource,
                 new DatabaseMetaDataCallback() {
                   public Object processMetaData(DatabaseMetaData dbmd)
                       throws SQLException, MetaDataAccessException {
                     String info =
                         dbmd.getDatabaseProductName() + " " + dbmd.getDatabaseProductVersion();
                     return info;
                   }
                 });
     List<Map<String, Object>> data = jdbcTemplate.queryForList("select * from customer");
     sqlData = data.toString();
   } catch (MetaDataAccessException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   model.addAttribute("sqlinfo", sqlInfo);
   model.addAttribute("sqldata", sqlData);
   String mongoInfo = mongoTemplate.getDb().getMongo().debugString();
   final StringBuilder mongoData = new StringBuilder();
   mongoData.append("[");
   mongoTemplate.execute(
       Customer.class.getName(),
       new CollectionCallback<String>() {
         public String doInCollection(DBCollection collection)
             throws MongoException, DataAccessException {
           for (DBObject dbo : collection.find()) {
             mongoData.append(mongoData.length() > 1 ? ", " : "");
             mongoData.append(dbo.toString());
           }
           return null;
         }
       });
   mongoData.append("]");
   model.addAttribute("mongoinfo", mongoInfo);
   model.addAttribute("mongodata", mongoData);
   return "dump";
 }
コード例 #2
0
  public void run() {

    if (mongoTemplate.collectionExists(Person.class)) {
      mongoTemplate.dropCollection(Person.class);
    }

    mongoTemplate.createCollection(Person.class);

    Person p = new Person("John", 39);
    Account a = new Account("1234-59873-893-1", Account.Type.SAVINGS, 123.45D);
    p.getAccounts().add(a);

    mongoTemplate.insert(p);

    List<Person> results = mongoTemplate.findAll(Person.class);
    System.out.println("Results: " + results);
  }