【Spring MVC + MongoDB】增删改
【Spring MVC + MongoDB】增删改
guquanjiang 发表于3年前
【Spring MVC + MongoDB】增删改
  • 发表于 3年前
  • 阅读 121
  • 收藏 4
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: MongoDB ,是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式,备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。

mongodb.properties

env=dev

#===============================测试环境====================================
#mongodb
dev.mongodb.host=127.0.0.1
dev.mongodb.port=222



applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
	xmlns:context="http://www.springframework.org/schema/context"    
	xmlns:mongo="http://www.springframework.org/schema/data/mongo"    
	xsi:schemaLocation="http://www.springframework.org/schema/context     
	http://www.springframework.org/schema/context/spring-context-3.0.xsd     
	http://www.springframework.org/schema/data/mongo     
	http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd     
	http://www.springframework.org/schema/beans     
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">     
	<context:component-scan base-package="com.guqj.mongodb" />
	
	<context:property-placeholder location="classpath:mongodb.properties" />
	
	<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
	   <property name="host" value="${${env}.mongodb.host}" />
	   <property name="port" value="${${env}.mongodb.port}" />
	</bean>  
          
	<!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->  
	<mongo:db-factory dbname="user" mongo-ref="mongo" />
    
	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">     
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />     
	</bean>     
    
	<mongo:mapping-converter base-package="com.guqj.mongodb.model" />
	<mongo:repositories base-package="com.guqj.mongodb.dao.impl" />
	<context:annotation-config />  
</beans>



UserDao.java
@Transactional
public interface UserDao {

	public void _test();

	/**
	 * 创建collection
	 */
	public void createCollection();

	/**
	 * 分页
	 * 
	 * @param skip
	 * @param limit
	 * @return
	 */
	public List<User> findList(int skip, int limit);

	/**
	 * 根据age查询
	 * 
	 * @param age
	 * @return
	 */
	public List<User> findListByAge(int age);

	/**
	 * 根据ID查询User
	 * 
	 * @param id
	 * @return
	 */
	public User findOne(String id);

	/**
	 * 根据名字查询User
	 * 
	 * @param username
	 * @return
	 */
	public User findOneByUsername(String username);

	/**
	 * 新增
	 * 
	 * @param entity
	 */
	public void insert(User entity);

	/**
	 * 更新
	 * 
	 * @param entity
	 */
	public void update(User entity);

	/**
	 * 统计
	 * 
	 * @return
	 */
	public long count();

	/**
	 * 根据ID删除
	 * 
	 * @param id
	 */
	public void deleteById(String id);

	/**
	 * 删除Collection
	 */
	public void deleteAll(String collection);
	
	public List<User> findList();
}



UserDAOImpl.java
@Repository
public class UserDAOImpl implements UserDao {

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void _test() {
		Set<String> colls = this.mongoTemplate.getCollectionNames();
		for (String coll : colls) {
			System.out.println("CollectionName=" + coll);
		}
		DB db = this.mongoTemplate.getDb();
		System.out.println("db=" + db.toString());
	}

	@Override
	public void createCollection() {
		if (!mongoTemplate.collectionExists(User.class)) {
			mongoTemplate.createCollection(User.class);
		}
	}

	@Override
	public List<User> findList(int skip, int limit) {
		Query query = new Query();
		query.addCriteria(Criteria.where("age").gt(10)).addCriteria(Criteria.where("zone").is("上海"));
		query.with(new Sort(new Sort.Order(Direction.ASC, "_id")));
		query.skip(skip).limit(limit);
		return mongoTemplate.find(query, User.class);
	}
	
	public List<User> findList() {
		Query query = new Query();
		query.addCriteria(Criteria.where("age").gt(10)).addCriteria(Criteria.where("zone").is("上海"));
		query.with(new Sort(new Sort.Order(Direction.ASC, "_id")));
		return mongoTemplate.find(query, User.class);
	}

	@Override
	public List<User> findListByAge(int age) {
		Query query = new Query();
		query.addCriteria(new Criteria("age").is(age));
		return mongoTemplate.find(query, User.class);
	}

	@Override
	public User findOne(String id) {
		Query query = new Query();
		query.addCriteria(new Criteria("id").is(id));
		return mongoTemplate.findOne(query, User.class);
	}

	@Override
	public User findOneByUsername(String username) {
		Query query = new Query();
		query.addCriteria(new Criteria("username").is(username));
		return mongoTemplate.findOne(query, User.class);
	}

	@Override
	public void insert(User entity) {
		mongoTemplate.insert(entity);
	}

	@Override
	public void update(User entity) {
		Query query = new Query();
		query.addCriteria(new Criteria("_id").is(entity.getId()));
		Update update = new Update();
		update.set("age", entity.getAge());
		update.set("password", entity.getPassword());
		update.set("zone", entity.getZone());
		update.set("special", entity.getSpecial());
		update.set("works", entity.getWorks());
		update.set("username", entity.getUsername());
		this.mongoTemplate.updateFirst(query, update, User.class);
	}

	@Override
	public long count() {
		return mongoTemplate.count(null, "user");
	}

	@Override
	public void deleteById(String id) {
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		mongoTemplate.remove(query, User.class);
	}

	@Override
	public void deleteAll(String collection) {
		mongoTemplate.dropCollection(collection);
	}



标签: SpringMVC Mongodb Nosql
共有 人打赏支持
粉丝 4
博文 10
码字总数 9544
×
guquanjiang
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: