mongodb之morphia
lib目錄
morphia-0.99.jar
morphia-logging-slf4j-0.99.jar
morphia-validation-0.99.jar
cglib-nodep-2.1_3.jar
proxytoys-1.0.jar
mongo-2.7.3.jar
slf4j-log4j12-1.6.1.jar
log4j-1.2.15.jar
slf4j-api-1.6.1.jar
morphia-logging-slf4j-0.99.jar
morphia-validation-0.99.jar
cglib-nodep-2.1_3.jar
proxytoys-1.0.jar
mongo-2.7.3.jar
slf4j-log4j12-1.6.1.jar
log4j-1.2.15.jar
slf4j-api-1.6.1.jar
import
org.bson.types.ObjectId;
import com.google.code.morphia.annotations.Id;
public class Address {
@Id
private ObjectId id;
private String street;
private String city;
private String postCode;
private String country;
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this .id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this .street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this .city = city;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this .postCode = postCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this .country = country;
}
}
import com.google.code.morphia.annotations.Id;
public class Address {
@Id
private ObjectId id;
private String street;
private String city;
private String postCode;
private String country;
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this .id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this .street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this .city = city;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this .postCode = postCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this .country = country;
}
}
import
org.bson.types.ObjectId;
import com.google.code.morphia.annotations.Embedded;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
@Entity
public class Hotel {
@Id
private ObjectId id;
private String name;
private int stars;
@Embedded
private Address address;
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public int getStars() {
return stars;
}
public void setStars( int stars) {
this .stars = stars;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this .address = address;
}
}
import com.google.code.morphia.annotations.Embedded;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
@Entity
public class Hotel {
@Id
private ObjectId id;
private String name;
private int stars;
@Embedded
private Address address;
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public int getStars() {
return stars;
}
public void setStars( int stars) {
this .stars = stars;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this .address = address;
}
}
import
java.net.UnknownHostException;
import java.util.Iterator;
import java.util.List;
import org.bson.types.ObjectId;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class Main {
public static void main(String[] args) {
try {
Mongo mongo = new Mongo( " 127.0.0.1 " , 27017 );
Morphia morphia = new Morphia();
// 映射Hotel、Address
morphia.map(Hotel. class ).map(Address. class );
// 创建数据库hotel
Datastore datastore = morphia.createDatastore(mongo, " hotel " );
/* Hotel hotel=new Hotel();
hotel.setName("Jiefang Hotel");
hotel.setStars(3);
Address address=new Address();
address.setStreet("xi'an jiefang street");
address.setCountry("China");
address.setPostCode("710075");
address.setCity("xi'an");
hotel.setAddress(address); */
// 持久化hotel到数据库
// datastore.save(hotel);
// 根据ObjectId查询一条记录
// String hotelId = "4f5dafcbf602eeffc946c648";
// Hotel hote=datastore.get(Hotel.class,new ObjectId(hotelId));
// System.out.println(hote.getName()+"\t"+hote.getAddress().getCountry()+"\t"+hote.getAddress().getStreet());
// find 根据条件查询
/* List<Hotel> hotels=datastore.find(Hotel.class, "stars > ", 3).asList();
for (Iterator<Hotel> iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getName()+"\t"+name.getAddress().getCountry()+"\t"+name.getAddress().getStreet());
} */
// find 根据条件查询 星级大于1 按星级降序排列
/* List<Hotel> hotels = datastore.find(Hotel.class, "stars > ", 1).order(" stars desc ").asList();
for (Iterator<Hotel> iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getName()+"\t"+name.getAddress().getCountry()+"\t"+name.getAddress().getStreet());
} */
// find 根据条件查询 查询酒店名称是Jiefang Hotel
Hotel hotel = datastore.find(Hotel. class , " name = " , " Jiefang Hotel " ).get();
System.out.println(hotel.getName() + " \t " + hotel.getAddress().getCountry() + " \t " + hotel.getAddress().getStreet());
List < Hotel > hotels = datastore.find(Hotel. class ).asList();
for (Iterator < Hotel > iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getId() + " \t " + name.getName() + " \t " + name.getAddress().getCountry() + " \t " + name.getAddress().getStreet());
}
// 删除 King Hotel
// datastore.findAndDelete(datastore.find(Hotel.class,"name","King Hotel"));
/* System.out.println("--------------------删除后------------------");
hotels=datastore.find(Hotel.class).asList();
for (Iterator<Hotel> iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getId()+"\t"+name.getName()+"\t"+name.getAddress().getCountry()+"\t"+name.getAddress().getStreet());
} */
// 修改编号为4f5db225f6027225c6d87629的星级为8星级
datastore.update(datastore.get(Hotel. class , new ObjectId( " 4f5db225f6027225c6d87629 " )), datastore.createUpdateOperations(Hotel. class ).set( " stars " , 8 ));
hotels = datastore.find(Hotel. class ).asList();
for (Iterator < Hotel > iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getId() + " \t " + name.getName() + " \t " + name.getStars() + " \t " + name.getAddress().getCountry() + " \t " + name.getAddress().getStreet());
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
import java.util.Iterator;
import java.util.List;
import org.bson.types.ObjectId;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class Main {
public static void main(String[] args) {
try {
Mongo mongo = new Mongo( " 127.0.0.1 " , 27017 );
Morphia morphia = new Morphia();
// 映射Hotel、Address
morphia.map(Hotel. class ).map(Address. class );
// 创建数据库hotel
Datastore datastore = morphia.createDatastore(mongo, " hotel " );
/* Hotel hotel=new Hotel();
hotel.setName("Jiefang Hotel");
hotel.setStars(3);
Address address=new Address();
address.setStreet("xi'an jiefang street");
address.setCountry("China");
address.setPostCode("710075");
address.setCity("xi'an");
hotel.setAddress(address); */
// 持久化hotel到数据库
// datastore.save(hotel);
// 根据ObjectId查询一条记录
// String hotelId = "4f5dafcbf602eeffc946c648";
// Hotel hote=datastore.get(Hotel.class,new ObjectId(hotelId));
// System.out.println(hote.getName()+"\t"+hote.getAddress().getCountry()+"\t"+hote.getAddress().getStreet());
// find 根据条件查询
/* List<Hotel> hotels=datastore.find(Hotel.class, "stars > ", 3).asList();
for (Iterator<Hotel> iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getName()+"\t"+name.getAddress().getCountry()+"\t"+name.getAddress().getStreet());
} */
// find 根据条件查询 星级大于1 按星级降序排列
/* List<Hotel> hotels = datastore.find(Hotel.class, "stars > ", 1).order(" stars desc ").asList();
for (Iterator<Hotel> iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getName()+"\t"+name.getAddress().getCountry()+"\t"+name.getAddress().getStreet());
} */
// find 根据条件查询 查询酒店名称是Jiefang Hotel
Hotel hotel = datastore.find(Hotel. class , " name = " , " Jiefang Hotel " ).get();
System.out.println(hotel.getName() + " \t " + hotel.getAddress().getCountry() + " \t " + hotel.getAddress().getStreet());
List < Hotel > hotels = datastore.find(Hotel. class ).asList();
for (Iterator < Hotel > iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getId() + " \t " + name.getName() + " \t " + name.getAddress().getCountry() + " \t " + name.getAddress().getStreet());
}
// 删除 King Hotel
// datastore.findAndDelete(datastore.find(Hotel.class,"name","King Hotel"));
/* System.out.println("--------------------删除后------------------");
hotels=datastore.find(Hotel.class).asList();
for (Iterator<Hotel> iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getId()+"\t"+name.getName()+"\t"+name.getAddress().getCountry()+"\t"+name.getAddress().getStreet());
} */
// 修改编号为4f5db225f6027225c6d87629的星级为8星级
datastore.update(datastore.get(Hotel. class , new ObjectId( " 4f5db225f6027225c6d87629 " )), datastore.createUpdateOperations(Hotel. class ).set( " stars " , 8 ));
hotels = datastore.find(Hotel. class ).asList();
for (Iterator < Hotel > iterator = hotels.iterator(); iterator.hasNext();) {
Hotel name = (Hotel) iterator.next();
System.out.println(name.getId() + " \t " + name.getName() + " \t " + name.getStars() + " \t " + name.getAddress().getCountry() + " \t " + name.getAddress().getStreet());
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}