springboot+mongo

一、mongoDB安装

  1.下载历史版本 ,win32/mongodb-win32-x86_64-2008plus-ssl-3.XX-signed.msi

 2.双击执行,默认下载到C:\\Program Files\MongoDB\Server\3.XX\bin

 3.配置①

  ①环境变量path    C:\Program Files\MongoDB\Server\3.0\bin;

  ②管理员cmd执行两条语句 cd \Program Files\MongoDB\Server\3.0\bin     mongod.exe --dbpath D:\WorkSpace\mongodb\data 

  ③浏览器中输入:http://localhost:27017/   ->  It looks like you are trying to access MongoDB over HTTP on the native driver port.

  打包几个文件bat,以后只需执行client.bat。或者UnInstall后,在依次执行installer server client。

-------------------mongodb_server_ Installer.txt---------------
start cmd /k "cd/d C:\Program Files\MongoDB\Server\3.0\bin&&mongod --dbpath D:\WorkSpace\mongodb\data"
--------------------mongodb_server.txt--------------------------
start cmd /k "cd/d C:\Program Files\MongoDB\Server\3.0\bin&&mongod --dbpath D:\WorkSpace\mongodb\data"
--------------------mongodb_client.txt--------------------------
start cmd /k "cd/d C:\Program Files\MongoDB\Server\3.0\bin&&mongo"
--------------------mongodb_server_ UnInstaller.txt-----------
net stop MongoDB
sc delete MongoDB
pause
server_Installer server client

 4.基本使用

  show dbs     use person    db.dropDatabase()    

  查 db.person.find({"name":"jack"})  //Json

   db.person.find({"age":{$lt:22}})  //$gt(>), $gte(>=), $lt(<), $lte(<=), $ne(!=)

       db.person.find({$or:[{"User.id":"12"},{"address.User.name":"xu"}]})   //"$or"(or), "$in"(in),"$nin"(notin),(and)

    插 db.person.insert({"name":"jack","age":20})   //Json

       var user=({"name":"jack","age":20})  db.person.insert(user);

  改 db.person.update({"name":"jack"},{"name":"jack","age":30})  //查找条件+更新的值

   db.person.update({"name":"jackson"},{$inc:{"age":10}},true)    //有则修改 无则添加

  删 db.person.remove({"name":"jack"})

 5.可视化工具MongoVUE

  连接

二、springboot使用

 1.pom.xml  application.yml 

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency> 
spring.data.mongodb.database=test
spring.data.mongodb.port=27017
spring.data.mongodb.host=127.0.0.1 

 2.修改原来的Person实体类

@Id
private Long id;    //再添加id的get和set方法
 public Person(Long id,String name, String sex) {
        this.id=id;
        this.name = name;
        this.sex = sex;
    } 

 3.使用jpa  public interface PersonRepository extends MongoRepository<Person,Long> {…}  

@RestController
public class PersonController {
    @Autowired
    private PersonRepository userRepository;

    @GetMapping("save")
    public String save() {
        Person userInfo = new Person(System.currentTimeMillis(),"李寻欢","男");
        userRepository.save(userInfo);
        return "success";
    }

    @GetMapping("getUserList")
    public List<Person> getUserList() {
        List<Person> userInfoList = userRepository.findAll();
        return userInfoList;
    }

    @GetMapping("delete")
    public String delete(Long id) {
        userRepository.delete(id);
        return "success";
    }

    @GetMapping("update")
    public String update(Long id, String username, String password) {
        Person userInfo = new Person(id, username, password);
        userRepository.save(userInfo);
        return "success";
    }
}
4.示例controller 

 运行http://localhost:8080/save后查看:

三、文件上传下载 

 首先 

<dependency>
     <groupId>net.sourceforge.nekohtml</groupId>
     <artifactId>nekohtml</artifactId>
     <version>1.9.15</version>
</dependency>   

 1.文件上传

@RequestMapping(value = "/upload")
 public String upload(@RequestParam("file") MultipartFile file) {
    String path="…", fileName = file.getOriginalFilename();   // 获取文件名
        if (!dest.getParentFile().exists())// 检测是否存在目录
        dest.getParentFile().mkdirs();// 新建文件夹
    file.transferTo(new File(path));// 执行文件写入本地path
}  

 2.多文件上传

@PostMapping("/batch")
public String handleFileUpload(HttpServletRequest request) {
    List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
      MultipartFile file = null;  BufferedOutputStream stream = null;
    for (int i = 0; i < files.size(); ++i) {
       file = files.get(i);     String filePath 
    if (!file.isEmpty()) {
      try {
          byte[] bytes = file.getBytes();
          stream = new BufferedOutputStream(new FileOutputStream(new File(filePath + file.getOriginalFilename())));//设置文件路径及名字
          stream.write(bytes);// 写入
          stream.close();
      }    

 3.文件下载

@GetMapping("/download")
public String downloadFile(HttpServletRequest request, HttpServletResponse response) { 
      File file = new File("E:\\storage/图片/test.jpg");    //设置文件路径                  
      response.setContentType("application/force-download");// 设置强制下载不打开
      response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
          byte[] buffer = new byte[1024];
          FileInputStream fis = null;   BufferedInputStream bis = null;
      try {
              fis = new FileInputStream(file);    bis = new BufferedInputStream(fis);     int i = bis.read(buffer);
         OutputStream os = response.getOutputStream(); 
              while (i != -1) { 
           os.write(buffer, 0, i); i = bis.read(buffer); 
         } 
       return "下载成功"; }
 }   

 4.html

<p>单文件上传</p>
<form action="upload" method="POST" enctype="multipart/form-data">
    文件:<input type="file" name="file"/>
    <input type="submit"/>
</form>
<p>文件下载</p>
<a href="download">下载文件</a>
<p>多文件上传</p>
<form method="POST" enctype="multipart/form-data" action="batch">
    <p>文件1:<input type="file" name="file"/></p>
    <p>文件2:<input type="file" name="file"/></p>
    <p><input type="submit" value="上传"/></p>
</form> 

转载资料 https://www.cnblogs.com/dalaoyang/p/8776807.html    https://www.cnblogs.com/jiekzou/p/9208706.html   https://www.cnblogs.com/jiekzou/p/6959638.html

 

2019-07-26  15:01:46 

posted @ 2019-07-26 15:03  shines87  阅读(332)  评论(0)    收藏  举报