Mongodb的使用

 1、引入jar包

<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
			<version>1.7.1.RELEASE</version>
		</dependency>

2、配置文件mongodb.properties

#mongodb配置文件
mongodb.database=robot1
mongodb.replicaSet = 118.25.35.113:27017

 

3、配置文件 spring-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:jee="http://www.springframework.org/schema/jee"
       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-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee-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/data/mongo
        http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
        http://www.springframework.org/schema/data/repository
		http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd
        ">

    <!-- 自动扫描 -->
    <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:config/mongodb.properties" />
    <context:annotation-config />
    <!-- 设置Spring使用自动扫描功能,使"base-package"中的对象可以使用Annotation配置 -->
    <context:component-scan base-package="com.yz.robot.*" />

    <mongo:mongo id="mongo" replica-set="${mongodb.replicaSet}" />
    <mongo:db-factory id="mongoDbFactory" dbname="${mongodb.database}"
                      mongo-ref="mongo" />

    <!-- mongo模板操作对象 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
        <!-- 配置读写分离的模式 -->
        <!--<property name="readPreference" ref="secondaryPreferredReadPreference"></property>-->
    </bean>

</beans>

4、controller引用,这里只是做一个简单demo,上传本地文件夹中文档到mongodb,并数据库中做记录

package com.yz.robot.controller;

import com.mongodb.DB;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;
import com.yz.robot.dao.model.TLawyer;
import com.yz.robot.dao.model.TLawyerCase;
import com.yz.robot.service.TLawyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("tlawyer")
public class TLawyerController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private TLawyerService tLawyerService;
    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/save")
    public Object list() throws Exception{

        DB db = mongoTemplate.getDb();
        GridFS gridFS = new GridFS(db);

        List<TLawyer> lawyerList = tLawyerService.getLawyerList();
        String filepath = "C:\\Users\\Administrator\\Desktop\\律师查询\\";//D盘下的file文件夹的目录
        File file = new File(filepath);//File类型可以是文件也可以是文件夹
        File[] fileList = file.listFiles();//将该目录下的所有文件放置在一个File类型的数组中
        int i = 0;
        for (File file1 : fileList){
            String name = file1.getName();
            String lawyerId = getLawyerId(name,lawyerList);
            if (lawyerId != null){
                System.out.println(file1.getAbsolutePath());
                File[] files = file1.listFiles();
                for (File file2 : files){
                    // 案例的绝对地址
                    String filePath = file2.getAbsolutePath();
                    // uuid作为文件的id
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    File imageFile = new File(filePath);
                    GridFSInputFile gfsFile = gridFS.createFile(imageFile);
                    gfsFile.setId(uuid);
                    gfsFile.setFilename(file2.getName());
                    gfsFile.save();

                    TLawyerCase tLawyerCase = new TLawyerCase();
                    tLawyerCase.setLawyerId(lawyerId);
                    tLawyerCase.setFileId(uuid);
                    int j = file2.getName().lastIndexOf(".");
                    String substring = file2.getName().substring(0, j);
                    tLawyerCase.setCaseTitle(substring);
                    tLawyerService.updateLawyerCase(tLawyerCase);

                    i++;
                    System.out.println(i);
                }

            }
        }
        
       return null;
    }

 

先写这么多,后面修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值