SpringMVC集成MongoDb
(1)pom添加相关依赖
1 <dependency> 2 <groupId>org.springframework.data</groupId> 3 <artifactId>spring-data-mongodb</artifactId> 4 <version>1.10.18.RELEASE</version> 5 </dependency>
(2)注册相关bean
1 package cn.coreqi.config; 2 3 import com.mongodb.Mongo; 4 import com.mongodb.MongoClient; 5 import com.mongodb.MongoCredential; 6 import com.mongodb.ServerAddress; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.data.mongodb.config.AbstractMongoConfiguration; 9 import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 10 import java.util.Arrays; 11 12 @Configuration 13 @EnableMongoRepositories(basePackages = "cn.coreqi.dao.mongodb") 14 public class MongodbConfig extends AbstractMongoConfiguration { 15 16 @Override 17 protected String getDatabaseName() { 18 return "springmongo"; 19 } 20 21 @Override 22 public Mongo mongo() throws Exception { 23 MongoCredential credential = MongoCredential.createCredential("fanqi","springmongo","123456".toCharArray()); 24 return new MongoClient(new ServerAddress("192.168.205.128",27017), Arrays.asList(credential)); 25 } 26 }
(3)编写实体类
1 package cn.coreqi.entities; 2 3 import org.springframework.data.annotation.Id; 4 import org.springframework.data.mongodb.core.mapping.Document; 5 import org.springframework.data.mongodb.core.mapping.Field; 6 7 import java.io.Serializable; 8 9 @Document 10 public class User implements Serializable { 11 @Id 12 private Integer Id; 13 @Field("age") 14 private Integer Age; 15 private String UserName; 16 private String PassWord; 17 private Integer Enabled; 18 19 public User(Integer id, Integer age, String userName, String passWord, Integer enabled) { 20 Id = id; 21 Age = age; 22 UserName = userName; 23 PassWord = passWord; 24 Enabled = enabled; 25 } 26 27 public Integer getId() { 28 return Id; 29 } 30 31 public void setId(Integer id) { 32 Id = id; 33 } 34 35 public Integer getAge() { 36 return Age; 37 } 38 39 public void setAge(Integer age) { 40 Age = age; 41 } 42 43 public String getUserName() { 44 return UserName; 45 } 46 47 public void setUserName(String userName) { 48 UserName = userName; 49 } 50 51 public String getPassWord() { 52 return PassWord; 53 } 54 55 public void setPassWord(String passWord) { 56 PassWord = passWord; 57 } 58 59 public Integer getEnabled() { 60 return Enabled; 61 } 62 63 public void setEnabled(Integer enabled) { 64 Enabled = enabled; 65 } 66 67 @Override 68 public String toString() { 69 return "User{" + 70 "Id=" + Id + 71 ", Age=" + Age + 72 ", UserName='" + UserName + '\'' + 73 ", PassWord='" + PassWord + '\'' + 74 ", Enabled=" + Enabled + 75 '}'; 76 } 77 }
(4)编写dao
1 package cn.coreqi.dao.mongodb; 2 3 import cn.coreqi.entities.User; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.data.mongodb.core.MongoOperations; 6 import org.springframework.data.mongodb.core.query.Criteria; 7 import org.springframework.data.mongodb.core.query.Query; 8 import org.springframework.stereotype.Repository; 9 10 @Repository 11 public class UserMongoDao { 12 @Autowired 13 private MongoOperations mongo; 14 15 public long count(){ 16 return mongo.getCollection("user").count(); 17 } 18 public void addUser(User user){ 19 mongo.save(user,"user"); 20 } 21 public User getById(int id){ 22 return mongo.findById(id,User.class); 23 } 24 public User getByUserName(String userName){ 25 return mongo.findOne(Query.query(Criteria.where("UserName").is(userName)),User.class); 26 } 27 public void delById(int id){ 28 mongo.remove(Query.query(Criteria.where("Id").is(id)),User.class); 29 } 30 }