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;
}
先写这么多,后面修改