精华内容
参与话题
问答
  • spring整合mongodb

    2019-03-05 11:59:16
    该压缩文件为spring整合mongodb的demo工程&直接使用java整合mongodb,兼容所有版本的mongodb,亲测可用。
  • spring 整合mongodb

    2019-08-11 01:11:58
    NULL 博文链接:https://mukeliang.iteye.com/blog/2228281
  • Spring整合MongoDB

    千次阅读 2015-08-10 17:08:03
    1、Maven导入依赖 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven

    1、Maven导入依赖

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>ren.benben</groupId>
      <artifactId>diary</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>diary Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <!-- 设置变量:用于统一管理Library的版本 -->
            <javax.version>7.0</javax.version>
            <javax.servlet.version>3.1.0</javax.servlet.version>
            <javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
            <log4j.version>1.2.16</log4j.version>
            <org.slf4j.version>1.7.1</org.slf4j.version>
            <junit.version>4.12</junit.version>
            <org.htmlparser.version>2.1</org.htmlparser.version>
            <org.springframework.version>4.1.1.RELEASE</org.springframework.version>
            <spring-data-mongodb.version>1.7.1.RELEASE</spring-data-mongodb.version>
            <org.mongodb.version>3.0.3</org.mongodb.version>
            <javax.validation.version>1.1.0.Final</javax.validation.version>
            <org.hibernate.version>5.0.2.Final</org.hibernate.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>${javax.version}</version>
                <scope>provided</scope>
            </dependency>
    
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${javax.servlet.jstl.version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>${javax.servlet.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
                <version>${org.slf4j.version}</version>
            </dependency>
    
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.htmlparser</groupId>
                <artifactId>htmlparser</artifactId>
                <version>${org.htmlparser.version}</version>
            </dependency>
    
            <!-- spring framework -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${org.springframework.version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-mongodb</artifactId>
                <version>${spring-data-mongodb.version}</version>
            </dependency>
    
            <!-- javax.validation相关  -->
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>${javax.validation.version}</version>
            </dependency>
            <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-validator</artifactId>  
                <version>${org.hibernate.version}</version>  
            </dependency>  
    
    
        </dependencies>
      <build>
        <finalName>diary</finalName>
      </build>
    </project>
    

    我们这里使用的Spring基础框架是4.1.x版本,使用1.7.x版本的Spring Data MongoDB对MongoDB进行CRUD操作。在使用Spring Data MongoDB框架时,需要使用到JSR-303 Validation验证标准。于是我们在这里引入 javax.validation的Jar和Hibernate Validator。值得注意的是,这里使用到的Hibernate Validator,不是我们要引入Hibernate框架,而仅仅是引入JSR-303的实现。

    2、Spring对MongoDB的配置

    2.1 我的配置

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:task="http://www.springframework.org/schema/task"  
        xmlns:mongo="http://www.springframework.org/schema/data/mongo"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
            http://www.springframework.org/schema/data/mongo       
            http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd
            http://www.springframework.org/schema/task 
            http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    
        <context:component-scan base-package="ren.benben.diary" />  
    
        <mongo:mongo host="127.0.0.1" port="27017" />  
    
        <!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->  
        <mongo:db-factory dbname="diary" mongo-ref="mongo" />  
    
        <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->  
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">  
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />  
        </bean>  
    
        <context:annotation-config />  
    
    </beans>
    

    上面是我使用的配置。

    2.2 mongo:mongo

    这里通过mongo:mongo标签来配置数据库的地址和端口,此标签默认的bean名称为mongo。如果你还需要配置MongoDB的其它参数,可以使用mongo:options标签。

    <mongo:mongo host="localhost" port="27017">
        <mongo:options connections-per-host="8"
                       threads-allowed-to-block-for-connection-multiplier="4"
                       connect-timeout="1000"
                       max-wait-time="1500}"
                       auto-connect-retry="true"
                       socket-keep-alive="true"
                       socket-timeout="1500"
                       slave-ok="true"
                       write-number="1"
                       write-timeout="0"
                       write-fsync="true"/>
     </mongo:mongo/>

    当然,在设置MongoDB参数时,也可以使用Java属性文件。

    <context:property-placeholder location="classpath:/com/myapp/mongodb/config/mongo.properties"/>
    
    <mongo:mongo host="${mongo.host}" port="${mongo.port}">
      <mongo:options
         connections-per-host="${mongo.connectionsPerHost}"
         threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
         connect-timeout="${mongo.connectTimeout}"
         max-wait-time="${mongo.maxWaitTime}"
         auto-connect-retry="${mongo.autoConnectRetry}"
         socket-keep-alive="${mongo.socketKeepAlive}"
         socket-timeout="${mongo.socketTimeout}"
         slave-ok="${mongo.slaveOk}"
         write-number="1"
         write-timeout="0"
         write-fsync="true"/>
    </mongo:mongo>

    2.3 mongo:db-factory

    当你设置完MongoDB的基本参数后,你还得设置一个mongo数据工厂。此过程是由mongo:db-factory完成的,此标签的主要作用是通过它来取得mongo实例,dbname为diary的数据库名,没有的话会自动创建。这里mongo-ref引用的就是上文mongo:mongo这个bean。
    mongo:db-factory标签默认的bean名称为mongoDbFactory。

    <mongo:db-factory dbname="diary" mongo-ref="mongo" />  

    如果你的DB需要用户名和密码才能访问的话,也可以通过mongo:db-factory来设置。

    <mongo:db-factory id="anotherMongoDbFactory"
                      host="localhost"
                      port="27017"
                      dbname="database"
                      username="benben"
                      password="123456"/>

    2.4 MongoTemplate

    设置完mongo:db-factory后,我们就可以设置MongoTemplate了。MongoTemplate是Spring对MongoDB进行CRUD操作的主要工具类,其作用类似Spring整合Hibernate时的HibernateTemplate。

        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">  
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />  
        </bean>  

    到这里为止,我们获取到MongoTemplate,配置就告一段落。

    3、实体类与映射

    新建一个文章Article实体类,对应数据库的article集合。

    package ren.benben.diary.article.model;
    
    import java.sql.Timestamp;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.mapping.Document;
    
    /**
     * 文章
     * @ClassName: Article 
     * @Description: TODO
     * @author 笨笨 
     * @date 2015年7月29日 下午2:19:58 
     *
     */
    @Document(collection = "article")
    public class Article {
        @Id
        private String id;
        private String title;
        private String descript;
        private String link;
        private String[] tags;
        private Timestamp createTime;
        private Timestamp updateTime;
        private Integer state;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getDescript() {
            return descript;
        }
    
        public void setDescript(String descript) {
            this.descript = descript;
        }
    
        public String getLink() {
            return link;
        }
    
        public void setLink(String link) {
            this.link = link;
        }
    
        public String[] getTags() {
            return tags;
        }
    
        public void setTags(String[] tags) {
            this.tags = tags;
        }
    
        public Timestamp getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Timestamp createTime) {
            this.createTime = createTime;
        }
    
        public Integer getState() {
            return state;
        }
    
        public void setState(Integer state) {
            this.state = state;
        }
    
        public Timestamp getUpdateTime() {
            return updateTime;
        }
    
        public void setUpdateTime(Timestamp updateTime) {
            this.updateTime = updateTime;
        }
    
    }
    

    3.1 @Document

    使用@Document注解将实体类与数据库的集合相互映射。collection的值是数据库的集合名称。
    加上@Document注解后,Spring的自动扫描功能就能识别出你的映射的实体类了。

    3.2 _id属性

    MongoDB需要我们为所有的文档都加上’_id’属性,其实就相当于SQL中的主键。所以我们在创建实体类时,也需要为实体类加一个id映射。

    Spring Data MongoDB提供了多种方式,在实体类中映射_id

    • 直接在实体类中定义一个名为id的字符串属性
    private String id;
    • 为实体类中的任意属性加上@Id注解
    @Id private String id;
    @Id private String x;

    如果没特殊的需要,我们最好是使用@Id注解为标注名为id的属性作为_id,像我们demo中的实体类一样。这样程序的可读性高点。

    3.3 @Field

    实体类的非_id属性,不加任何注解。可以同名的映射到数据库的集合中。如果你非得跟数据库中的不一样,Spring也提供了@Field为实现。
    如,我们想把上文中的link属性,保存到集合的url属性中。

    @Field("url")
    private String link;

    4、DAO基类及使用

    4.1 IBaseDao接口及其实现

    package ren.benben.diary.api;
    
    import java.util.List;
    
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.data.mongodb.core.query.Update;
    /**
     * IBaseDao接口
     * @ClassName: IBaseDao 
     * @Description: TODO
     * @author 笨笨 
     * @date 2015年8月10日 下午3:15:40 
     * 
     * @param <T>
     */
    public interface IBaseDao<T> {
        /**
         * 获取查询数据的数量
         * @Title: findCount 
         * @Description: TODO
         * @param @param query
         * @param @return   
         * @return Long   
         * @throws
         */
        public Long findCount(Query query);
        /**
         * 查询数据
         * @Title: find 
         * @Description: TODO
         * @param @param query
         * @param @return   
         * @return List<T>   
         * @throws
         */
        public List<T> find(Query query);
        /**
         * 分页查询数据
         * @Title: findList 
         * @Description: TODO
         * @param @param start
         * @param @param limit
         * @param @return   
         * @return List<T>   
         * @throws
         */
        public List<T> findList(int start, int limit);
        /**
         * 根据id获取单个数据
         * @Title: findOne 
         * @Description: TODO
         * @param @param id
         * @param @return   
         * @return T   
         * @throws
         */
        public T findOne(String id);
        /**
         * 插入一条数据
         * @Title: insert 
         * @Description: TODO
         * @param @param entity   
         * @return void   
         * @throws
         */
        public void insert(T entity);
        /**
         * 更新数据
         * @Title: update 
         * @Description: TODO
         * @param @param entity
         * @param @throws Exception   
         * @return void   
         * @throws
         */
        public void update(T entity) throws Exception;
        /**
         * 更新数据
         * @Title: update 
         * @Description: TODO
         * @param @param query
         * @param @param update   
         * @return void   
         * @throws
         */
        public void update(Query query,Update update);
        /**
         * 删除数据
         * @Title: remove 
         * @Description: TODO
         * @param @param entity   
         * @return void   
         * @throws
         */
        public void remove(T entity);
    }
    
    package ren.benben.diary.api;
    
    import java.lang.reflect.ParameterizedType;
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Sort;
    import org.springframework.data.domain.Sort.Direction;
    import org.springframework.data.domain.Sort.Order;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.data.mongodb.core.query.Criteria;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.data.mongodb.core.query.Update;
    
    import ren.benben.diary.api.util.MongoFactory;
    
    /**
     * IBaseDao接口实现
     * @ClassName: BaseDao 
     * @Description: TODO
     * @author 笨笨
     * @date 2015年8月10日 下午3:57:18 
     * 
     * @param <T>
     */
    public class BaseDao<T> implements IBaseDao<T> {
        private Class<T> clazz;
    
        public BaseDao() {
            ParameterizedType type = (ParameterizedType) getClass()
                    .getGenericSuperclass();
            clazz = (Class<T>) type.getActualTypeArguments()[0];
        }
    
        @Autowired
        private MongoTemplate mongoTemplate;
        @Autowired
        private MongoFactory mongoFactory;
    
        public List<T> find(Query query){
            return mongoTemplate.find(query, clazz);
        }
    
        public List<T> findList(int skip, int limit) {
            Query query = new Query();
            query.with(new Sort(new Order(Direction.ASC, "_id")));
            query.skip(skip).limit(limit);
            return find(query);
        }
    
    
        public T findOne(String id) {
            Query query = new Query();
            query.addCriteria(new Criteria("_id").is(id));
            return mongoTemplate.findOne(query, clazz);
        }
    
        public void insert(T entity) {
            mongoTemplate.insert(entity);
        }
    
    
        public void update(T entity) throws Exception {
            Map<String, Object> map = mongoFactory.converObjectToParams(entity);
    
            Query query = new Query();
            query.addCriteria(new Criteria("_id").is(map.get("id")));
            Update update = (Update) map.get("update");
    
            this.update(query, update);
        }
    
    
        public void update(Query query,Update update){
            mongoTemplate.updateFirst(query, update, clazz);
        }
    
        public Long findCount(Query query){
            return mongoTemplate.count(query, clazz);
        }
    
        public void remove(T entity){
            mongoTemplate.remove(entity);
        }
    
        public MongoTemplate getMongoTemplate() {
            return mongoTemplate;
        }
    }
    

    在DAO基类实现上,我们自己写了一个MongoFactory的工具类。此类主要是在做更新的时候使用。在update(T entity)方法中,做到类似HibernateTemplate一样,通过MongoFactory获取到entity的id及entity值不为空的属性,以id作为条件,更新这些属性。

    相关的两个辅助工具类如下:

    package ren.benben.diary.api.util;
    
    import java.lang.reflect.Field;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.mongodb.core.query.Update;
    import org.springframework.stereotype.Component;
    
    /**
     * Mongo辅助类
     * @ClassName: MongoFactory 
     * @Description: TODO
     * @author 笨笨 
     * @date 2015年8月10日 下午4:02:07 
     *
     */
    @Component
    public class MongoFactory {
    
        @Autowired
        private ObjectParams objectParams;
    
        /**
         * 把实体对象转为MongoDB更新需要的值
         * @Title: converObjectToParams 
         * @Description: TODO
         * @param @param obj
         * @param @return
         * @param @throws Exception   
         * @return Map<String,Object>   
         * @throws
         */
        public Map<String, Object> converObjectToParams(Object obj) throws Exception{
            Map<String, Object> map = new HashMap<String, Object>();
            Update update = new Update();
            Map<String, String> params = objectParams.createParams(obj);
            String id = params.get("id");
            Set<Map.Entry<String, String>> set = params.entrySet();
            for (Iterator<Map.Entry<String, String>> it = set.iterator(); it.hasNext();) {
                Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
                if(!entry.getKey().equals("id")){
                    update.set(entry.getKey(), entry.getValue());
                }
            }
    
            map.put("id", id);
            map.put("update", update);
    
            return map;
        }
    
    
    }
    
    package ren.benben.diary.api.util;
    
    import java.lang.reflect.Field;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.stereotype.Component;
    
    /**
     * 实体参数映射工具
     * @ClassName: ObjectParams 
     * @Description: TODO
     * @author 笨笨 
     * @date 2015年8月10日 下午4:05:35 
     *
     */
    @Component
    public class ObjectParams {
        private  String javaType = "java";
        /**
         * 获取查询的参数
         * 
         * @param object
         * @return
         * @throws Exception
         */
        public  Map<String, String> createParams(Object object) throws Exception {
            Map<String, String> params = new HashMap<String, String>();
    
            setIntoParams(params,object, null);
            return params;
        }
    
        private  void setIntoParams(Map<String, String> params,Object object, String fatherName) throws IllegalAccessException,
                Exception {
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field file : fields) {
                boolean accessFlag = file.isAccessible();
                file.setAccessible(true);
                String name = file.getName();
                Object value = file.get(object);
    
                if(file.getType().getName().equals("java.lang.Class")){
                    break;
                }else if(file.getType().getName().contains(javaType)){
                    if(fatherName != null && !fatherName.equals(" ")){
                        name = fatherName+"."+name;
                    }
    
                    if(value != null){
                        params.put(name, value+"");
                    }
    
    
                }else{
                    if(value != null){
                        setIntoParams(params,file.get(object), name);
                    }
    
                }
                file.setAccessible(accessFlag);
            }
        }
    }
    

    4.2 使用BaseDao

    建立Article实体类的Dao。

    package ren.benben.diary.article.dao;
    
    import java.util.List;
    
    import ren.benben.diary.api.IBaseDao;
    import ren.benben.diary.api.util.Pagination;
    import ren.benben.diary.article.model.Article;
    
    /**
     * Article的Dao接口
     * @ClassName: IArticleDao 
     * @Description: TODO
     * @author 笨笨 
     * @date 2015年8月10日 下午4:09:25 
     *
     */
    public interface IArticleDao extends IBaseDao<Article>{
        /**
         * 根据link属性获取一条数据
         * @Title: findOneByLink 
         * @Description: TODO
         * @param @param link
         * @param @return   
         * @return Article   
         * @throws
         */
        public Article findOneByLink(String link);
        /**
         * 根据link属性与title属性进行或运算获取一条数据
         * @Title: findOneByLinkOrTitle 
         * @Description: TODO
         * @param @param link
         * @param @param title
         * @param @return   
         * @return Article   
         * @throws
         */
        public Article findOneByLinkOrTitle(String link,String title);
        /**
         * 根据state属性获取createTime为最新的数据
         * @Title: findNewestByState 
         * @Description: TODO
         * @param @param state
         * @param @return   
         * @return Article   
         * @throws
         */
        public Article findNewestByState(Integer state);
        /**
         * 根据state属性获取数据的数量
         * @Title: findNewestByState 
         * @Description: TODO
         * @param @param state
         * @param @return   
         * @return Article   
         * @throws
         */
        public Long findCount(Integer state);
        /**
         * 分页获取数据
         * @Title: findListByPage 
         * @Description: TODO
         * @param @param page
         * @param @param state
         * @param @return   
         * @return List<Article>   
         * @throws
         */
        public List<Article> findListByPage(Pagination page,Integer state);
    }
    
    package ren.benben.diary.article.dao.impl;
    
    
    import java.util.List;
    
    import org.springframework.data.domain.Sort;
    import org.springframework.data.domain.Sort.Direction;
    import org.springframework.data.mongodb.core.query.Criteria;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.data.mongodb.core.query.Update;
    import org.springframework.stereotype.Repository;
    
    import ren.benben.diary.api.BaseDao;
    import ren.benben.diary.api.constant.Constant;
    import ren.benben.diary.api.util.Pagination;
    import ren.benben.diary.article.dao.IArticleDao;
    import ren.benben.diary.article.model.Article;
    
    /**
     * Article的Dao实现
     * @ClassName: ArticleDao 
     * @Description: TODO
     * @author 笨笨 
     * @date 2015年8月10日 下午4:14:21 
     *
     */
    @Repository  
    public class ArticleDao extends BaseDao<Article> implements IArticleDao {
    
        public Article findOneByLink(String link) {
            Query query = new Query(Criteria.where("link").is(link));
    
            return getMongoTemplate().findOne(query, Article.class);
        }
    
        public Article findNewestByState(Integer state){
            Query query = createQuery(state);
            query.limit(1).with(new Sort(new Sort.Order(Sort.Direction.DESC,"createTime")));
    
             List<Article> list = this.find(query);
    
             if(list.size()>0){
                 return list.get(0);
             }
    
            return null;
        }
    
        public List<Article> findListByPage(Pagination page,Integer state) {  
            Query query = createQuery(state);
            query.with(page.getSort());
            query.skip(page.getSkip()).limit(page.getPageSize());// 从skip开始,取多少条记录  
    
            return this.find(query);  
        }
    
        public Long findCount(Integer state){
            Query query = createQuery(state);
            return this.findCount(query);
        }
    
        private Query createQuery(Integer state){
            Query query = new Query();
            query.addCriteria(Criteria.where("state").is(state));  
            return query;
        }
    
        public Article findOneByLinkOrTitle(String link, String title) {
            Query query = new Query(
                    new Criteria().orOperator(Criteria.where("title").is(title),    Criteria.where("link").is(link))
                    );
            return getMongoTemplate().findOne(query, Article.class);
        }
    
    }
    

    分页辅助类:

    package ren.benben.diary.api.util;
    
    import java.util.List;
    
    import org.springframework.data.domain.Sort;
    import org.springframework.data.domain.Sort.Direction;
    
    import ren.benben.diary.api.constant.Constant;
    
    /**
     * 分页辅助类
     * @ClassName: Pagination 
     * @Description: TODO
     * @author 潘广伟 
     * @date 2015年8月10日 下午4:15:51 
     *
     */
    public class Pagination {
        /** 每页数量 */
        private Integer pageSize;
        /** 开始位置 */
        private Integer skip;
        /** 当前页 */
        private Integer currentPage;
        /** 总页数 */
        private Long totalPage;
        /** 查询到的总数据量 */
        private Long totalNumber;
        /** 数据集 */
        private List items;
        /** 排序 */
        private Sort sort;
    
        public Pagination(int currentPage, Long totalNumber) {
            this.pageSize = Constant.PAGE_SIZE;
            this.currentPage = currentPage;
            this.totalNumber = totalNumber;
            this.totalPage = totalNumber / pageSize + 1;
            this.skip = (currentPage - 1) * pageSize;
            this.sort = new Sort(new Sort.Order(Direction.ASC, "_id"));
        }
    
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    
        public Long getTotalPage() {
            return totalPage;
        }
    
        public void setTotalPage(Long totalPage) {
            this.totalPage = totalPage;
        }
    
        public Long getTotalNumber() {
            return totalNumber;
        }
    
        public void setTotalNumber(Long totalNumber) {
            this.totalNumber = totalNumber;
        }
    
        public List getItems() {
            return items;
        }
    
        public void setItems(List items) {
            this.items = items;
        }
    
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getSkip() {
            return skip;
        }
    
        public void setSkip(Integer skip) {
            this.skip = skip;
        }
    
        public Sort getSort() {
            return sort;
        }
    
        public void setSort(String dir, boolean isAsc) {
            if (isAsc) {
                this.sort = new Sort(new Sort.Order(Direction.ASC, dir));
            } else {
                this.sort = new Sort(new Sort.Order(Direction.DESC, dir));
            }
    
        }
    
    }
    

    5、测试及小优化

    5.1 测试数据插入及_class属性的移除

    单元测试代码:

    package diary;
    
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import ren.benben.diary.api.util.DateFactory;
    import ren.benben.diary.article.dao.IArticleDao;
    import ren.benben.diary.article.model.Article;
    
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:/applicationContext.xml")
    public class MongoTest {
        @Autowired private IArticleDao articleDao;
        @Autowired private DateFactory factory;
    
        @Test
        public void testInsert(){   
            try {
                Article article = new Article();
                article.setTitle("标题1");
                article.setLink("http://benben.ren");
                article.setState(1);
                article.setCreateTime(factory.now());
                articleDao.insert(article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    测试结果:
    这里写图片描述

    我们看到测试结果中,除了我们想要保存的属性值外,还多了一个_class的属性。这是由Spring默认帮我们加到集合中的。它可能并不是我们需要的,且这个数据还占着存储空间。如果我们想把这个属性去掉,可以通过配置MappingMongoConverter的typeMapper来实现。

    <bean id="mappingContext"
            class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
        <!-- 默认Mongodb类型映射 -->
        <bean id="defaultMongoTypeMapper"
            class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
            <constructor-arg name="typeKey">
                <null /><!-- 这里设置为空,可以把 spring data mongodb 多余保存的_class字段去掉 -->
            </constructor-arg>
        </bean>
    
    
        <bean id="mappingMongoConverter"
            class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
            <constructor-arg name="mappingContext" ref="mappingContext" />
            <property name="typeMapper" ref="defaultMongoTypeMapper" />
        </bean>
    
    
        <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
            <constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
        </bean>  
    

    5.2 测试简单查询及类型转换器

    单元测试代码:

        @Test
        public void testFindOne() {
            try {
                Article article = articleDao.findOne("55c85750a192cfe9d93e1cf1");
    
                System.out.println(article.getTitle());
                System.out.println(article.getCreateTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    测试结果:

    信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    org.springframework.core.convert.ConverterNotFoundException: No converter found 
    capable of converting from type java.util.Date to type java.sql.Timestamp
        at org.springframework.core.convert.support.GenericConversionService.handleConv
    erterNotFound(GenericConversionService.java:311)
        at org.springframework.core.convert.support.GenericConversionService.convert(Ge
    nericConversionService.java:192)
        at org.springframework.core.convert.support.GenericConversionService.convert(Ge
    nericConversionService.java:173)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPoten
    tiallyConvertedSimpleRead(MappingMongoConverter.java:821)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValu
    e(MappingMongoConverter.java:1187)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$2
    00(MappingMongoConverter.java:78)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbP
    ropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1133)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValue
    Internal(MappingMongoConverter.java:869)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWith
    PersistentProperty(MappingMongoConverter.java:282)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWith
    PersistentProperty(MappingMongoConverter.java:270)
        at org.springframework.data.mapping.model.BasicPersistentEntity.doWithPropertie
    s(BasicPersistentEntity.java:309)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(Map
    pingMongoConverter.java:270)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(Map
    pingMongoConverter.java:231)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(Map
    pingMongoConverter.java:191)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(Map
    pingMongoConverter.java:187)
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(Map
    pingMongoConverter.java:78)
        at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doW
    ith(MongoTemplate.java:2200)
        at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(M
    ongoTemplate.java:1837)
        at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.
    java:1654)
        at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.ja
    va:563)
        at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.ja
    va:558)
        at ren.benben.diary.api.BaseDao.findOne(BaseDao.java:56)
        at diary.MongoTest.testFindOne(MongoTest.java:41)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57
    )
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
    .java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.
    java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.j
    ava:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.ja
    va:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.jav
    a:17)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallba
    cks.evaluate(RunBeforeTestMethodCallbacks.java:72)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbac
    ks.evaluate(RunAfterTestMethodCallbacks.java:81)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(Spr
    ingRepeat.java:72)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(Spr
    ingJUnit4ClassRunner.java:216)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(Spr
    ingJUnit4ClassRunner.java:82)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbac
    ks.evaluate(RunBeforeTestClassCallbacks.java:60)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallback
    s.evaluate(RunAfterTestClassCallbacks.java:67)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJU
    nit4ClassRunner.java:162)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestRef
    erence.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:3
    8)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRu
    nner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRu
    nner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.
    java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner
    .java:192)

    这里报无法将java.util.Date转换成java.sql.Timestamp。原因是我们实体类中的时间是Timestamp类型的,而Spring Data默认的时间类型是Date。
    这里我们需要一个转换器。

    package ren.benben.diary.api.converter;
    
    import java.sql.Timestamp;
    import java.util.Date;
    
    import org.springframework.core.convert.converter.Converter;
    
    /**
     * Timestamp转换器
     * 将Date转换成Timestamp
     * @ClassName: TimestampConverter 
     * @Description: TODO
     * @author 笨笨 
     * @date 2015年8月10日 下午4:45:19 
     *
     */
    public class TimestampConverter implements Converter<Date, Timestamp>{
    
        public Timestamp convert(Date date) {
            if(date != null){
                return new Timestamp(date.getTime());
            }
            return null;
        }
    
    }
    

    配置TimestampConverter转换器到Spring中。由于我们在上面做移除_class属性优化的时候,使用到了MappingMongoConverter,而MongoTemplate只有一个mongoConverter属性,所以我们需要将DefaultMongoTypeMapper引入新的Converter中。

        <!-- 默认Mongodb类型映射 -->
        <bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
            <constructor-arg name="typeKey">
                <null /><!-- 这里设置为空,可以把 spring data mongodb 多余保存的_class字段去掉 -->
            </constructor-arg>
        </bean>
    
    
        <!-- 转换器:1、去掉write的_class字段 2、TimestampConverter -->
        <mongo:mapping-converter id="mongoConverter" base-package="ren.benben.diary.api.converter" type-mapper-ref="defaultMongoTypeMapper">
            <mongo:custom-converters>
              <mongo:converter>
                    <bean class="ren.benben.diary.api.converter.TimestampConverter" />
              </mongo:converter>
    
            </mongo:custom-converters>
        </mongo:mapping-converter> 
    
    
        <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->  
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">  
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />  
            <constructor-arg name="mongoConverter" ref="mongoConverter" />  
        </bean>  

    再次测试得到结果:
    这里写图片描述

    5.3 使用分页查询

    使用代码(Service层使用)

        public Pagination findArticles(Integer currentPage, Integer state) {
            Long count = articleDao.findCount(state);
            Pagination page = new Pagination(currentPage, count);
            page.setSort("creatTime", false);
    
            page.setItems(articleDao.findListByPage(page, state));
    
            return page;
        }

    这里是获取文章总数,获取currentPage页count条数据。并按creatTime降序排序。
    具体就不详细展开了。

    5.4 注意

    BaseDao中做更新数据的时候,我们使用到了java映射机制来获取实体对象的值,并更新这些值。如果更新的属性值不是太多的话,建议不要使用BaseDao的update(T entity)方法,因为通过映射获取值没有直接获取来得快。

    6、链接

    Mongodb的简单使用:
    http://blog.csdn.net/jrainbow/article/details/39163843

    Spring MVC的简单用法:
    http://blog.csdn.net/jrainbow/article/details/46618077

    HibernateTemplate与HibernateDaoSupport:
    http://blog.csdn.net/jrainbow/article/details/10526617

    展开全文
  • Spring 整合 MongoDB

    2019-11-27 13:27:24
    pom.xml 导入依赖 <dependency> <groupId>org.springframework.boot</groupId>...spring-boot-starter-data-mongodb</artifactId> </dependency> 初始化 //导入 @Aut...

    pom.xml 导入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    

    初始化

    //导入
    @Autowired
    private MongoTemplate mongoTemplate;
    
    //用来封装所有条件的对象
    Query query = new Query();
    
    //用来构建条件
    Criteria criteria = new Criteria();
    
    //调用方法,看编译器提示,自行选择
    mongoTemplate.
    

    查询操作

    //模糊查询,Pattern.CASE_INSENSITIVE(忽视大小写)
    Pattern pattern = Pattern.compile("^.*"+这里拼接你的查询条件字符串+".*$", Pattern.CASE_INSENSITIVE);
    
    //模糊查询的条件,用 and 相连
    criteria.and().regex()
    //query.addCriteria 封装条件
    query.addCriteria(criteria.and("这里填写你MongoDB集合中的key").regex(pattern));
    
    //精准查询的条件,用 and 相连
    criteria.and().is()
    //query.addCriteria 封装条件
    query.addCriteria(criteria.and("你MongoDB中的key").is("你的条件");
    
    //某个值之间的查询条件
    //gt:大于
    //lt:小于
    //gte:大于或等于
    //lte:小于或等于
    //Sort:排序
    //ne:不等于
    //大于方法
    Criteria gt = Criteria.where("你MongoDB中的key").gt("你的条件");
    //小于方法
    Criteria lt = Criteria.where("你MongoDB中的key").lt("你的条件");
    //andOperator() 将同 key 的多个条件组合,注:每个 query只能出现一次
    query.addCriteria(new Criteria().andOperator(gt,lt));
    //正序排序
    query.with(new Sort(Sort.Direction.ASC, "loginTime"));
    //倒序排序
    query.with(new Sort(Sort.Direction.DESC, "loginTime"));
    

    重写 find 方法(存在软删除时使用)

    @Component
    public class MongoDBTemp extends MongoTemplate {
    
        public MongoDBTemp(MongoDbFactory mongoDbFactory) {
            super(mongoDbFactory);
        }
    
        @Override
        public <T> List<T> find(Query query, Class<T> entityClass) {
            query.addCriteria(Criteria.where("delete").is(false));
            return super.find(query, entityClass);
        }
        
        @Override
        public <T> T findOne(Query query, Class<T> entityClass) {
            query.addCriteria(Criteria.where("delete").is(false));
            return super.findOne(query, entityClass);
        }
    }
    
    展开全文
  • Spring整合Mongodb

    2018-05-22 10:47:39
    pom工程:1、环境准备1.1...org.mongodb&lt;/groupId&gt; &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt; &lt;version&gt;${mongodb.java.version}&lt;/version...

    pom工程:

    1、环境准备

    1.1用到的jar

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>${mongodb.java.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>${data.mongodb.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb-cross-store</artifactId>
        <version>${data.mongodb.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb-log4j</artifactId>
        <version>${data.mongodb.version}</version>
    </dependency
    
    >

    1.2配置文件:

    mongo.host=192.168.0.205
    mongo.port=27017
    mongo.defaultDbName=sdzn
    mongo.user=
    mongo.pwd=
    mongo.connectionsPerHost=10
    mongo.threadsAllowedToBlockForConnectionMultiplier=5
    mongo.minConnectionsPerHost=5
    mongo.connectTimeout=10000
    mongo.maxWaitTime=120000
    mongo.socketTimeout=0
    mongo.socketKeepAlive=true
    
    mongo.description=test
    mongo.maxConnectionIdleTime=1500
    mongo.maxConnectionLifeTime=0
    #mongo slave
    mongo.heartbeatSocketTimeout=1000
    mongo.heartbeatConnectTimeout=1500
    mongo.minHeartbeatFrequency=5
    mongo.heartbeatFrequency=10
    配置服务器需要的ip、端口号、数据库名称、用户、密码(创建的无密码登录服务器),其他的都用默认配置就足够了。
    1.3 mongodb.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <context:component-scan base-package="com.mongo" />
        <!-- 获取配置资源 -->
        <context:property-placeholder location="classpath:mongodb-context-config.properties" />
        
        
        <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}" >
            <mongo:options 
                connections-per-host="${mongo.connectionsPerHost}" 
                threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" 
                connect-timeout="${mongo.connectTimeout}" 
                max-wait-time="${mongo.maxWaitTime}" 
                auto-connect-retry="${mongo.autoConnectRetry}" 
                socket-keep-alive="${mongo.socketKeepAlive}" 
                socket-timeout="${mongo.socketTimeout}" 
                slave-ok="${mongo.slaveOk}" 
                write-number="1" 
                write-timeout="0" write-fsync="true"/> 
        </mongo:mongo>
        <!-- 设置使用的数据库 名-->
        <mongo:db-factory dbname="test" mongo-ref="mongo"/>
        <!-- mongodb的模板 -->
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
           <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
        </bean>
    </beans>
    2、常用操作:
    (crud)

    public class test(){

    @Autowried
        private MongoTemplate mongoTemplate;//直接注入MongoTemplate
     @Before
         public void testBefore() {
    
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mvc-servlet.xml");
       mongoTemplate = (MongoTemplate) context.getBean("mongoTemplate");
    }
    
    //新增
    @Test
    public void addTest(){
        User user= nuew User();
        user.setName("张三");
        user.setAge(22);
        mongoTemoplate.insert(user);
    }
    
    //查询
    @Test  
    public void selectTest(){
    //用到了Query和Criteria

    Query query = new Query();

    Criteria criteria = where("age").gt(22);//查询年龄等于22的用户;

    query.addCriteria(criteria);

    List<User> users = mongodbTemplate.find(query,User.class);//查询

    //其他查询条件

     // criteria.and("name").is("cuichongfei");等于
            // List<String> interests = new ArrayList<String>();
            // interests.add("study");
            // interests.add("linux");
            // criteria.and("interest").in(interests); in查询
            // criteria.and("home.address").is("henan"); 内嵌文档查询
            // criteria.and("").exists(false); 列存在
            // criteria.and("").lte(); 小于等于
            // criteria.and("").regex(""); 正则表达式
            // criteria.and("").ne(""); 不等于
    //多条件查询
    Query query = new Query();
    Criteria criteria = new Criteria();

    criteria.andOperator(Criteria.where("age").gt(22),Criteria.where("name").regex("张.*"));

    query.addCriteria(criteria);

    List<User> users = mongodbTemplate.find(query,User.class);

    //andOperator 并且,orOperator 或者,(sql中的and、or);
    
    //排序(按照年龄倒序查询)
    query.sort().on("age,Order.Desc)

    //查询全部
    mongodbTemplate.findAll(User.class);

    //查询数量
    mongodbTemplate.count(query,User.class);

    //只查询两个字段(在query中添加条件)
    query.fields.include("name").include("age");

    }
    
    //删除
     @Test
    public void delTest(){
    
    Query query = new Quer();
    
    
    Criteria criteria = where("age").gt(22);//年龄等于22的用户;
    query.addCriteria(criteria);

    mongodbTemplate.remove(query,User.class);

    }

    //修改
    @Test
    public void updateTest(){

    Query query = new Query();

    query.addCriteria(where("id").is(1));

    Update update = new Update();

    update.set("age",23);

    update.unset("name");

    mongodbTemplate.updateMulit(query,update,User.class);//修改id=1的年龄,并删除name字段
     
    // age值加2
            update.inc("age", 2);
            // update.set("name", "zhangsan"); 直接赋值
            // update.unset("name"); 删去字段
            // update.push("interest", "java"); 把java追加到interest里面,interest一定得是数组
            // update.pushAll("interest", new String[]{".net","mq"})
            // 用法同push,只是pushAll一定可以追加多个值到一个数组字段内
            // update.pull("interest", "study"); 作用和push相反,从interest字段中删除一个等于value的值
            // update.pullAll("interest", new String[]{"sing","dota"})作用和pushAll相反
            // update.addToSet("interest", "study") 把一个值添加到数组字段中,而且只有当这个值不在数组内的时候才增加
            // update.rename("oldName", "newName") 字段重命名
    
            // 只更新第一条记录,age加2,name值更新为zhangsan
            mongoTemplate.updateFirst(query, new Update().inc("age", 2).set("name", "zhangsan"), User.class);
    
            // 批量更新,更新所有查询到的数据
            mongoTemplate.updateMulti(query, update, User.class);
    }
    }


    @Id          文档的唯一标识,在mongodb中为ObjectId,它是唯一的,通过时间戳+机器标识+进程ID+自增计数器(确保同一秒内产生的Id不会冲突)构成。

    @Document       把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。

    @DBRef        声明类似于关系数据库的关联关系。ps:暂不支持级联的保存功能,当你在本实例中修改了DERef对象里面的值时,单独保存本实例并不能保存DERef引用的对象,它要另外保存,如下面例子的Person和Account。

    @Indexed       声明该字段需要索引,建索引可以大大的提高查询效率。

    @CompoundIndex    复合索引的声明,建复合索引可以有效地提高多字段的查询效率。

    @GeoSpatialIndexed 声明该字段为地理信息的索引。

    @Transient      映射忽略的字段,该字段不会保存到mongodb。

    @PersistenceConstructor   声明构造函数,作用是把从数据库取出的数据实例化为对象。该构造函数传入的值为从DBObject中取出的数据。







    展开全文
  • Spring 整合Mongodb

    2017-09-28 11:06:49
    1、项目采用Maven 的方式。所需要的jar包只需要配置即可。 2、与Mongodbspring整合相关的两个jar包: <groupId>org.mongodb <artifactId>mongo-java-driver ${mongo-java-drive

    1、项目采用Maven 的方式。所需要的jar包只需要配置即可。
    2、与Mongodb,spring整合相关的两个jar包:

    <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>${mongo-java-driver.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>${spring-data-mongodb.version}</version>
    </dependency>

    3、项目结构如下图所示:
    这里写图片描述

    4、pom.xml配置如下所示:

    <dependencies>
            <!-- 日志 -->
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                    <version>${slf4j.version}</version>
                </dependency>
             <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-jms</artifactId>
                <version>${spring.version}</version>   
                <scope>compile</scope>  
            </dependency>  
            <dependency>
                <groupId>org.apache.xbean</groupId>
                <artifactId>xbean-spring</artifactId>
                <version>${xbean-spring.version}</version>
            </dependency>
            <!-- <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-spring</artifactId>
                <version>${spring.version}</version>
            </dependency> -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpclient.version}</version>
            </dependency>
           <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongo-java-driver</artifactId>
                <version>${mongo-java-driver.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-mongodb</artifactId>
                <version>${spring-data-mongodb.version}</version>
            </dependency>
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>  
            </dependency> 
            <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                    <scope>test</scope>
            </dependency>

    5、添加spring相关的配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
        <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <!-- 允许JVM参数覆盖 -->
            <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
            <!-- 忽略没有找到的资源文件 -->
            <property name="ignoreResourceNotFound" value="true" />
            <!-- 配置资源文件 -->
            <property name="locations">
                <list>
                    <value>classpath:config.properties</value>
                    <value>classpath:common.properties</value>
                </list>
            </property>
        </bean>
    
        <!-- spring 注释代替配置,自动扫描的基础包,将扫描该包以及所有子包下的所有类 -->
        <context:component-scan base-package="com.newsoft">
            <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Controller" />
        </context:component-scan>
    
    
    </beans>
    

    6、添加Mongodb相关的配置文件。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mongo="http://www.springframework.org/schema/data/mongo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/data/mongo
            http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
        ">
    
        <!-- 1.7版本+不支持mongo,支持mongo-client,options为client-options -->
        <!-- Usage of 'UserCredentials' with 'MongoClient' is no longer supported -->
        <!-- 配置mongodb连接池 ,创建mongoClient实例-->
        <!--credentials="mongodb://${mongo.user}:${mongo.pwd}@${mongo.host}:${mongo.port}/${mongo
        .defaultDbName}"为什么不起作用
        如果需要验证,mongo-client需要加这句: credentials="${mongo.user}:${mongo.pwd}@${mongo.defaultDbName}"
        -->
        <mongo:mongo-client id="mongoClient"  host="${mongo.host}" port="${mongo.port}">
            <mongo:client-options
                    connections-per-host="${mongo.connectionsPerHost}"
                    min-connections-per-host="${mongo.minConnectionsPerHost}"
                    threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                    connect-timeout="${mongo.connectTimeout}"
                    max-wait-time="${mongo.maxWaitTime}"
                    socket-keep-alive="${mongo.socketKeepAlive}"
                    socket-timeout="${mongo.socketTimeout}"
                    description="${mongo.description}"
                    max-connection-idle-time="${mongo.maxConnectionIdleTime}"
                    max-connection-life-time="${mongo.maxConnectionLifeTime}"
                    heartbeat-socket-timeout="${mongo.heartbeatSocketTimeout}"
                    heartbeat-connect-timeout="${mongo.heartbeatConnectTimeout}"
                    min-heartbeat-frequency="${mongo.minHeartbeatFrequency}"
                    heartbeat-frequency="${mongo.heartbeatFrequency}"
    
            />
        </mongo:mongo-client>
    
        <!-- dbname="${mongo.defaultDbName}" host="${mongo.host}"
                          port="${mongo.port}" username="${mongo.user}" password="${mongo.pwd}"
                          mongo-ref="mongoClient" -->
        <mongo:db-factory id="mongoDbFactory"
                          dbname="${mongo.defaultDbName}"
                          mongo-ref="mongoClient"/>
    
        <!-- 1.Spring提供的mongodb操作模板-->
        <mongo:template id="mongoTemplate" db-factory-ref="mongoDbFactory" write-concern="NORMAL"/>
        <!-- 2.Spring提供的mongodb操作模板-->
        <!--<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg ref="mongoDbFactory" />
        </bean>-->
    
    
        <!-- MongoDB GridFS Template 支持,操作mongodb存放的文件 -->
        <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"
                                 base-package="cn.idongjia.web.dao"/>
        <mongo:gridFsTemplate id="gridFsTemplcate" db-factory-ref="mongoDbFactory" converter-ref="converter" />
    
        <!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
        <mongo:repositories base-package="com.newsoft.model" />
    
        <!--激活注解-->
        <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
    
    </beans>
    #mongodb
    mongo.host=127.0.0.1
    mongo.port=27017
    mongo.defaultDbName=log
    mongo.user=liuc
    mongo.pwd=199116
    mongo.connectionsPerHost=100
    mongo.threadsAllowedToBlockForConnectionMultiplier=5
    mongo.minConnectionsPerHost=5
    #连接超时时间
    mongo.connectTimeout=10000
    #等待时间
    mongo.maxWaitTime=120000
    #Socket超时时间
    mongo.socketTimeout=0
    mongo.socketKeepAlive=true
    
    mongo.description= test mongodb database
    mongo.maxConnectionIdleTime=1500
    mongo.maxConnectionLifeTime=0
    #mongo slave
    mongo.heartbeatSocketTimeout=1000
    mongo.heartbeatConnectTimeout=1500
    mongo.minHeartbeatFrequency=5
    mongo.heartbeatFrequency=10

    本项目将Mongodb相关的增删查改操作进行了一层封装。只需传入所需参数调用即可。

    public class ObjectDaoImpl<T> implements ObjectDao<T>{
    
        @Autowired
        protected MongoOperations mongoOperations;
    
        @Override
        public void save(T t) {
            mongoOperations.insert(t);
        }
    
        @Override
        public T saveReturnT( T t){
            mongoOperations.insert(t);
            //如果t对象中包含id主键,则插入数据后,返回对象t,包含自增的主键id内容
            return t;
        }
    
        @Override
        public int update(Query query, Update update,Class<T> tclass) {
            WriteResult result = mongoOperations.updateFirst(query, update, tclass);
            return result.getN();
        }
    
        @Override
        public Pager page(Pager pager, Query query,Class<T> tclass) {
            int skipCount = (pager.getPageNumber() > 0 ? pager.getPageNumber() - 1 : 0) * pager.getPageSize();
            query.skip(skipCount);
            query.limit(pager.getPageSize());
            List<T> list = mongoOperations.find(query, tclass);
            pager.setResult(list);
            pager.setTotalCount(list.size());
            return pager;
        }
    
        @Override
        public T get(Query query, Class<T> tclass) {
            T t = mongoOperations.findOne(query,tclass);
            return t;
        }
    
        @Override
        public void remove(Query query, Class<T> tclass) {
            mongoOperations.remove(query, tclass);
    
        }
    
    }

    实体类中的注释详解:
    @Id - 文档的唯一标识,在mongodb中为ObjectId,它是唯一的,通过时间戳+机器标识+进程ID+自增计数器(确保同一秒内产生的Id不会冲突)构成。

    @Document - 把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。@Document(collection=”mongodb”) mongodb对应表

    @DBRef - 声明类似于关系数据库的关联关系。ps:暂不支持级联的保存功能,当你在本实例中修改了DERef对象里面的值时,单独保存本实例并不能保存DERef引用的对象,它要另外保存,如下面例子的Person和Account。

    @Indexed - 声明该字段需要索引,建索引可以大大的提高查询效率。

    @CompoundIndex - 复合索引的声明,建复合索引可以有效地提高多字段的查询效率。

    @GeoSpatialIndexed - 声明该字段为地理信息的索引。

    @Transient - 映射忽略的字段,该字段不会保存到mongodb。

    @PersistenceConstructor - 声明构造函数,作用是把从数据库取出的数据实例化为对象。该构造函数传入的值为从DBObject中取出的数据


    具体详情可以下载本项目进行查看。http://download.csdn.net/download/u012151597/9999239

    展开全文
  • Spring整合mongodb

    2019-10-17 10:05:24
    Spring整合mongodb 第一步 引入对应的jar包 我这里是基于jar是2.0以上做的配置,在2.0以上的包需要注意Spring的版本需要5.2以上 <dependency> <groupId>org.mongodb</groupId> <...
  • Spring 整合MongoDB

    2020-05-18 17:14:24
    1、在pom.xml文件中添加mongoDB的依赖: 2、通过xml配置MongoTemplate 3、AbstractMongodbDao类,实现MongoDb的CRUD操作 4、测试 4.1 MongoUser 实体类,对应db中的集合user 4.2 UserMongoDao 4.3...

空空如也

1 2 3 4 5 ... 20
收藏数 1,207
精华内容 482
关键字:

spring整合mongodb

spring 订阅