当前位置: 首页 > 工具软件 > Morphia > 使用案例 >

Morphia采用

郑富
2023-12-01

Morphia配合mongoDB喜欢Hibernate关系型数据库.也能够想到Morphia于MongoDB它调用上述的基础上,mongoDB语法.

于java直接通话Morphia的api它会变得更加简单快捷.


1.加mongoDB和Morphia的Jar包.

//创建连接源
Mongo mongo = null;
		try {
			mongo = new Mongo();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		Morphia morphia = new Morphia();
		Datastore ds = morphia.createDatastore(mongo, "test");

//查找
		for(User user:ds.find(User.class, "password", "admin")){
			System.out.println(user);
		}
		System.out.println(ds.find(User.class, "password", "admin").get());

		//插入

		User user1 = new User("00018", "hello1", "hello1");
		User user2 = new User("00019", "hello2", "hello2");
		List<User> list = new ArrayList<>();
		list.add(user1);
		list.add(user2);
		ds.save(list);

	// 改动
		Query<User> query =
				ds.createQuery(User.class).field("uid").equal("00013");
		UpdateOperations<User> uo =
				ds.createUpdateOperations(User.class).set("password",
						"IDONTKONW").set("userName", "IDONTKONW");
		ds.update(query, uo);


		/**
		 * 删除
		 * 
		 */
		// ds.delete(User.class, "00018");
		Query<User> query = ds.createQuery(User.class).field("userName").contains("t");
		ds.delete(query);


版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/lcchuguo/p/4910197.html

 类似资料: