spring mongodb 之简单crud -凯发k8网页登录

专注于javaweb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

spring mongodb 之简单crud

官方文档参考:  
用myeclipse添加maven-project工程,在这之前你可能需要配置你的m2eclipse
1.将maven配置成你自己安装的版本。


2.设置user settings
当然也可以修改仓库默认目录,打maven安装目录conf/setting.xml
节点里
添加:d:/myrepository即可


pom.xml

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0modelversion>
  <groupid>com.testgroupid>
  <artifactid>mongodbartifactid>
  <version>0.0.1-snapshotversion>
  <name>springmongodbname>
  <description>this is test casedescription>
  <build>
    <plugins>
      <plugin>
        <artifactid>maven-compiler-pluginartifactid>
        <configuration>
          <source>1.6source>
          <target>1.6target>
        configuration>
      plugin>
    plugins>
  build>
  
  <repositories>
        <repository>
            <id>spring-milestoneid>
            <name>spring maven milestone repositoryname>
            <url>http://maven.springframework.org/milestoneurl>
        repository>
    repositories>
 
    <dependencies>
        <dependency>
            <groupid>junitgroupid>
            <artifactid>junitartifactid>
            <version>4.8.2version>
            <scope>testscope>
        dependency>
 
        
        <dependency>
            <groupid>org.springframeworkgroupid>
            <artifactid>spring-coreartifactid>
            <version>3.0.5.releaseversion>
        dependency>
 
        <dependency>
            <groupid>org.springframeworkgroupid>
            <artifactid>spring-contextartifactid>
            <version>3.0.5.releaseversion>
        dependency>
 
        
        <dependency>
            <groupid>org.mongodbgroupid>
            <artifactid>mongo-java-driverartifactid>
            <version>2.5.2version>
        dependency>
 
        <dependency>
            <groupid>org.springframework.datagroupid>
            <artifactid>spring-data-mongodbartifactid>
            <version>1.0.0.m2version>
        dependency>
 
        <dependency>
            <groupid>cglibgroupid>
            <artifactid>cglibartifactid>
            <version>2.2version>
        dependency>
 
    dependencies>
    
project>
写完pom.xml后,执行 项目-右键-run-maven install 
下载完jar包,test 成功后,就可以写代码了。
springmongoconfig.java
package com.config;

import org.springframework.context.annotation.bean;
import org.springframework.data.document.mongodb.mongotemplate;
import org.springframework.data.document.mongodb.config.abstractmongoconfiguration;

import com.mongodb.mongo;

public class springmongoconfig extends abstractmongoconfiguration
{
    //主机
    @override
    public mongo mongo() throws exception {
        return new mongo("localhost");
    }
    //数据库
    @override
    public @bean mongotemplate mongotemplate() throws exception {
        return new mongotemplate(mongo(), "test");
    }

}
实体类:
package entity;

import java.util.list;

public class user
{

    private string id;
    private string username;
    private string password;
    //文档中的集合
    private list position;

    // 要有声明默认的构造方法,不然查询的时候会报错(无法实例化)
    public user()
    {

    }

    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 string getpassword()
    {
        return password;
    }

    public void setpassword(string password)
    {
        this.password = password;
    }

    public list getposition()
    {
        return position;
    }

    public void setposition(list position)
    {
        this.position = position;
    }

    public user(string id, string username, string password,
            list position)
    {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.position = position;
    }

    public user(string username, string password, list position)
    {
        super();
        this.username = username;
        this.password = password;
        this.position = position;
    }

}
测试类:
package run;

import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import java.util.set;

import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;
import org.springframework.data.document.mongodb.mongooperations;
import org.springframework.data.document.mongodb.query.criteria;
import org.springframework.data.document.mongodb.query.query;
import org.springframework.data.document.mongodb.query.update;

import com.config.springmongoconfig;

import entity.user;

public class app
{
    private applicationcontext ctx = new annotationconfigapplicationcontext(
            springmongoconfig.class);
    private mongooperations mongooperations = (mongooperations) ctx
            .getbean("mongotemplate");

    @test
    public void testsave()
    {
        list list = new arraylist();
        for(int i =0; i < 100; i  )
        {
            list.add("开发"   i);
            list.add("经理"   i);
            user user = new user("john"   i,"123"   i,list);
            //第一个参数:要保存的表名,默认为实例名user同名的集合(表)
            
//如果表不存在会自动创建, 表名user为关键字不要使用
            mongooperations.save("userinfo", user);
            list.clear();
        }
    }

    @test
    public void testupdate()
    {
        mongooperations.updatefirst("userinfo",
                new query(criteria.where("username").is("john0")),
                update.update("password", "newpassword"));
    }

    @test
    public void testdelete()
    {
        mongooperations.remove("userinfo",new query(criteria.where("id").is("4ffe3486b41f8ed41269a729")),user.class);
    }

    @test
    public void testquery()
    {
        //输出所有表名
        set set = mongooperations.getcollectionnames();
        iterator it =  set.iterator();
        while(it.hasnext())
        {
            system.out.println(it.next());
        }
        //user:要查询的表名
        list users = mongooperations.getcollection("userinfo", user.class);
        system.out.println(users.size());
        for(user u : users)
        {
            system.out.println(u.getid());
        }
    }

}



posted on 2012-07-12 10:42 々上善若水々 阅读(5319) 评论(0)  编辑  收藏 所属分类: 数据库


只有注册用户后才能发表评论。


网站导航:
              
 
网站地图