接口IMongDaoCreate:
package com.net.test.mongdb.dao; import java.util.List; import com.net.test.mongdb.entity.User; import com.net.test.mongdb.entity.UserP; public interface IMongDaoCreate { public void create(User user); public void createUsers(List<User> list); public void createUsersArr(List<User> list); public void createUserP(UserP user2); }
实现类MongDaoCreateImp:
package com.net.test.mongdb.dao.imp; import java.util.List; import javax.annotation.Resource; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Repository; import com.net.test.mongdb.dao.IMongDaoCreate; import com.net.test.mongdb.entity.User; import com.net.test.mongdb.entity.UserP; /** * @author *** * @Time:2017年8月4日 上午9:21:23 * @version 1.0 * Function: Create */ @Repository public class MongDaoCreateImp implements IMongDaoCreate { @Resource protected MongoTemplate mongoTemplate; /** * @description 新建单条简单类型数据,不带数组和内置对象 * @param user */ @Override public void create(User user) { mongoTemplate.save(user); } /** * @description 批量新建多条简单类型数据,不带数组和内置对象 * @param objects */ @Override public void createUsers(List<User> objects) { mongoTemplate.insertAll(objects); } /** * @description 批量新建多条带数组和内置对象的复杂数据 * @param list */ @Override public void createUsersArr(List<User> list) { mongoTemplate.insertAll(list); } /** * @description 新建单条简单类型数据,表名与pojo不相同 * @param user2 */ @Override public void createUserP(UserP user2) { mongoTemplate.insert(user2, "user"); } }