/** * Tries to map all classes in the package specified. Fails if one of the classes is not valid for mapping. * * @param packageName the name of the package to process * @return the Morphia instance */ public synchronized Morphia mapPackage(final String packageName) { return mapPackage(packageName, false); }
/** * Maps all the classes found in the package to which the given class belongs. * * @param clazz the class to use when trying to find others to map * @return this */ public Morphia mapPackageFromClass(final Class clazz) { return mapPackage(clazz.getPackage().getName(), false); }
/** * Default . * * @param mongo mongo * @param db mongo database. * @param entity_package entity package name. */ private MorphiaKit(final MongoClient mongo, final String db, String entity_package) { Preconditions.checkNotNull(mongo, "the mongo object is not null. "); Preconditions.checkNotNull(db, "the mongodb database name is not null."); Morphia morphia = new Morphia(); _datastore = morphia.createDatastore(mongo, db); morphia.mapPackage(entity_package); }
public MongoDbBuildingEntityGateway(MongoClient mongo) { Morphia morphia = new Morphia(); morphia.mapPackage("be.insaneprogramming.cleanarch.entitygatewayimpl.morphia"); this.datastore = morphia.createDatastore(mongo, "cleanarch"); }
final Morphia morphia = new Morphia(); // tell Morphia where to find your classes // can be called multiple times with different packages or classes morphia.mapPackage("org.mongodb.morphia.example"); // create the Datastore connecting to the default port on the local host final Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia_example"); datastore.ensureIndexes();
morphia.mapPackage("package"); } catch (UnknownHostException e) { e.printStackTrace();
public class MongoConfig { private static Datastore datastore; public static Datastore datastore() { if (datastore == null) { initDatastore(); } return datastore; } public static void initDatastore() { final Morphia morphia = new Morphia(); // Tell Morphia where to find our models morphia.mapPackage("models"); MongoClient mongoClient = new MongoClient( ConfigFactory.load().getString("mongodb.host"), ConfigFactory.load().getInt("mongodb.port")); datastore = morphia.createDatastore( mongoClient, ConfigFactory.load().getString("mongodb.database")); } }
/** * 创建mongodb连接 * * @param configPath */ public void createConnect(String configPath) { mongoConfig = FileUtil.getConfigXML(configPath, "mongoClientConfig.xml", MongoClientConfig.class); if (mongoConfig == null) { throw new RuntimeException(String.format("mongodb 配置文件 %s/MongoClientConfig.xml 未找到", configPath)); } MongoClientURI uri = new MongoClientURI(mongoConfig.getUrl()); mongoClient = new MongoClient(uri); morphia = new Morphia(); morphia.mapPackage(""); initDao(); }