首页 > 代码库 > springmvc+mongodb+maven 项目测试代码
springmvc+mongodb+maven 项目测试代码
配置请看我另一篇文章,后续还会有,mongodb性能测试结果,一个“快”字
要源码包请留下邮箱
代码构造图
直接上代码
BaseDao.java
package com.yiyuwanglu.basecore.dao; import java.util.List; public interface BaseDao { <T> T findById(Class<T> entityClass, String id); <T> List<T> findAll(Class<T> entityClass); void remove(Object obj); void add(Object obj); void saveOrUpdate(Object obj); }
MongoDBBaseDao.java
package com.yiyuwanglu.basecore.dao.mongodb; import java.util.Collection; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Repository; import com.yiyuwanglu.basecore.dao.BaseDao; import com.yiyuwanglu.basecore.page.Page; /** * mongodb数据泛型dao类 * * @author hjn * @version 1.0 2014-09-16 */ @Repository(value = http://www.mamicode.com/"mongoDBBaseDao")>
Page.javapackage com.yiyuwanglu.basecore.page; import java.io.IOException; import java.io.Serializable; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; public class Page extends SimpleTagSupport implements Serializable { private static final long serialVersionUID = 1L;//序列号 private Integer current;//当前页码 private Long count;//记录总数 private Integer pageCount;//每页记录数 private String path;//页面链接 private String param;//传入的参数 private boolean notQueryCount = false;//为false在翻页时查询记录总数,默认false public Page() { this.current = 1; // 默认第一页 this.count = 0L; // 共多少条记录 this.pageCount = 10; // 默认每页10条记录 } @Override public void doTag() throws JspException, IOException { int pageSize = (int) (this.count / this.pageCount + (this.count % this.pageCount > 0 ? 1 : 0));//共多少页 //显示当前页和总页数 JspWriter out = this.getJspContext().getOut();//指定输入流,用于页面输出分页信息 StringBuffer sb = new StringBuffer();//构建StringBuffer对象,用户拼接分页标签 sb.append("<div class=\"page\">"); sb.append("<ul>"); //如果当前页在第一页,则首页和上一页没有超链接 if (this.current == 1) { sb.append("<li class=\"disabled\">首页</li><li class=\"disabled\">上一页</li>"); } else { sb.append("<li><a href=http://www.mamicode.com/"");>
User.javapackage com.yiyuwanglu.test.entity; import java.util.Date; import org.springframework.data.mongodb.core.mapping.Document; @Document public class User { private String id; private String username; private int age; private Date createTime; public User() { } public User(String username, int age, Date createTime) { super(); this.username = username; this.age = age; this.createTime = createTime; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
BaseTest.javapackage basetest; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:config/spring/applicationContext.xml"}) public class BaseTest { }TestBaseDao.javapackage basetest.dao; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import basetest.BaseTest; import com.yiyuwanglu.basecore.dao.mongodb.MongoDBBaseDao; import com.yiyuwanglu.test.entity.User; public class TestBaseDao extends BaseTest { @Resource(name = "mongoDBBaseDao") MongoDBBaseDao mongoDBBaseDao; /** * 插入单条数据,id自定义 */ public void testAdd() { User user = new User(); Date creatTime = new Date(); user.setCreateTime(creatTime); user.setAge(10); user.setUsername("福東江a"); this.mongoDBBaseDao.add(user); } /** * 插入100万条数据,id自定义 */ @Test public void testAddCollection() { List<User> userList = new ArrayList<User>(); for (int j = 0; j < 10; j++) { for (int i = 0; i < 100000; i++) { User user = new User(); Date creatTime = new Date(); user.setCreateTime(creatTime); user.setAge(10); user.setUsername("冊南"); userList.add(user); } } this.mongoDBBaseDao.add(userList); } @Test public void testFindAll() { List<User> userList = this.mongoDBBaseDao.findAll(User.class); for (User user : userList) { System.out.println("id:" + user.getId() + " username:" + user.getUsername() + " age:" + user.getAge()); } System.out.println("获取全部的数据----------------------"); } @Test public void testFindById() { User user = this.mongoDBBaseDao.findById(User.class, "1234567"); System.out.println(user.getUsername()); System.out.println("获取单个对象----------------------"); } @Test public void testUpdate() { User user = this.mongoDBBaseDao.findById(User.class, "1234567"); user.setUsername("反倒是淮"); this.mongoDBBaseDao.saveOrUpdate(user); User newUser = this.mongoDBBaseDao.findById(User.class, "1234567"); System.out.println(newUser.getUsername()); System.out.println("修改数据成功"); this.mongoDBBaseDao.saveOrUpdate(user); } @Test public void testRemove() { User user = this.mongoDBBaseDao.findById(User.class, "1234567"); this.mongoDBBaseDao.remove(user); User oldUser = this.mongoDBBaseDao.findById(User.class, "1234567"); if (oldUser == null) { System.out.println(oldUser == null); System.out.println("删除对象成功"); } this.mongoDBBaseDao.add(user); } @Test public void testCount() { Query query = new Query(); Criteria criteria = new Criteria(); criteria.and("username").is("福東"); query.addCriteria(criteria); long total = this.mongoDBBaseDao.count(User.class, query); System.out.println("用户总数:" + total); } }springmvc+mongodb+maven 项目测试代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。