当前位置:   article > 正文

springboot整合MongoDB_starter-data-mongodb starter-data-jpa 同时加failed to

starter-data-mongodb starter-data-jpa 同时加failed to determine a suitable d

springboot整合MongoDB
docker启动MongoDB
# docker拉取镜像
[root@goblin~]# docker pull mongo:3.2.10
Trying to pull repository docker.io/library/mongo ... 
3.2.10: Pulling from docker.io/library/mongo
386a066cd84a: Pull complete 
524267bc200a: Pull complete 
476d61c7c43a: Pull complete 
0750d0e28b90: Pull complete 
4bedd83d0855: Pull complete 
b3b5d21a0eda: Pull complete 
7354b6c26240: Pull complete 
db792d386b51: Pull complete 
a867bd77950c: Pull complete 
Digest: sha256:532a19da83ee0e4e2a2ec6bc4212fc4af26357c040675d5c2629a4e4c4563cef
Status: Downloaded newer image for docker.io/mongo:3.2.10
# 查看镜像
[root@goblin~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis                latest              621ceef7494a        8 weeks ago         104 MB
docker.io/rabbitmq             3-management        30e33de9be86        11 months ago       184 MB
docker.io/tomcat               latest              5692d26ea179        13 months ago       507 MB
docker.io/mysql                5.7                 c4f186b9e038        13 months ago       435 MB
docker.io/rmohr/activemq       latest              7c58d2d8d6af        2 years ago         569 MB
docker.io/webcenter/activemq   latest              3af156432993        4 years ago         422 MB
docker.io/mongo                3.2.10              fe9198c04d62        4 years ago         342 MB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
# docker 启动镜像
[root@goblin ~]# docker run -di --name springCloudGoblinMongoDB -p 27017:27017 fe9198c04d62
9aae0b3a3f16acb5c8a35b85c2f104397216c85ba8c20ea2d4e474280951481d
# 查看运行的容器
[root@goblin ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
9aae0b3a3f16        fe9198c04d62        "/entrypoint.sh mo..."   14 seconds ago      Up 13 seconds       0.0.0.0:27017->27017/tcp            springCloudGoblinMongoDB
b37e28ee468e        621ceef7494a        "docker-entrypoint..."   26 hours ago        Up 2 hours          0.0.0.0:6379->6379/tcp              springCloudGoblinRedis
18931167fae2        c4f186b9e038        "docker-entrypoint..."   5 days ago          Up 2 hours          0.0.0.0:3306->3306/tcp, 33060/tcp   springcloudvue
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
打开mogo客户端
C:\Users\lenovo>mongo 192.168.1.105  #不是默认端口的需要将端口加上
MongoDB shell version: 3.2.10
connecting to: 192.168.1.105/test
Welcome to the MongoDB shell.
..........
# 选择和创建数据库
> use spit 
switched to db spit
# db.集合名称.insert(数据)
>db.spit.insert({"_id":"1","visits":NumberInt("0"),"share":NumberInt("0"),"thumbup":NumberInt("0"),"comment":NumberInt("0"),"state":"1"})
WriteResult({ "nInserted" : 1 }) # 说明成功插入一条数据
# 查询
> db.spit.find() 
{ "_id" : "1", "visits" : 0, "share" : 0, "thumbup" : 0, "comment" : 0, "state" : "1" }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

客户端(安装在了windows上)是使用的 mongodb-win32-x86_64-2008plus-ssl-3.2.10-signed.msi 软件;它既是服务端,也是客户端;示例中只用客户端,不用服务端,因为服务端用的是linux的docker启动的服务端;
mongodb-win32-x86_64-2008plus-ssl-3.2.10-signed.msi 安装完成之后,配置环境变量(直接在path中新建将该软件安装的路径加上)

pom.xml添加MongoDB依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- 引入MongoDB的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
application.yml配置
server: 
  port: 8090
spring: 
  application:  
    name: goblin_spit
  data:
    mongodb:
      host: 192.168.1.105 # mongodb主机ip;端口为默认27017可以省略不配
      database: spit#数据库名;
  redis:
    host: 192.168.1.105
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
Spit.java
package com.goblin.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "spit")
public class Spit implements Serializable {
    @Id
    private String id;
    private String content;
    private Date publishtime;
    private String userid;
    private String nickname;
    private Integer visits;
    private Integer thumbup;
    private Integer share;
    private Integer comment;
    private String state;
    private String parentid;
    // getter/setter方法省略
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
SpitApplication.java
package com.goblin;

import com.goblin.util.IdWorker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.Bean;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class })//忽略数据源自动配置(解决使用mongodb启动报错的问题(Failed to determine a suitable driver class))
public class SpitApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpitApplication.class, args);
    }

    @Bean // @Bean将IdWorker 注入到容器
    public IdWorker idWorker(){// ID生成工具;采用了雪花算法;源码网上有可自行下载
        return new IdWorker(1, 1);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
SpitDao.java
package com.goblin.dao;

import com.goblin.entity.Spit;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
// 持久层继承mongo仓库; MongoRepository<实体类, 主键的类型>
public interface SpitDao extends MongoRepository<Spit, String> {
    /**
     * 根据上级ID查询吐槽列表(分页)
     * @param parentId
     * @param pageable
     * @return
     */
    Page<Spit> findByParentid(String parentId, Pageable pageable);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
SpitService.java
package com.goblin.service;

import com.goblin.dao.SpitDao;
import com.goblin.entity.Spit;
import com.goblin.util.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SpitService {
    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 增加分享数
     * @param id
     */
    public void shareAdd(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("share", 1);
        mongoTemplate.updateFirst(query, update, "spit");
    }

    /**
     * 增加浏览量
     */
    public void visitsAdd(String id){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("visits", 1);
        mongoTemplate.updateFirst(query, update, "spit");
    }

    /**
     * 发布吐槽
     * @param spit
     */
    public void add(Spit spit) {
        spit.setId(idWorker.nextId() + "");
        spit.setThumbup(0);
        spit.setComment(0);
        spit.setContent("content");
        spit.setNickname("goblin");
        spit.setPublishtime(new Date());
        spit.setShare(0);
        spit.setState("1");
        spit.setUserid("1111");
        spit.setVisits(0);
        if (StringUtils.isNotBlank(spit.getParentid())){
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
            Update update = new Update();
            update.inc("comment", 1);
            mongoTemplate.updateFirst(query, update, "spit");
        }
        spitDao.save(spit);
    }

    /**
     * 点赞(方式2)
     * @param id
     */
    public void updateThumbup(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("thumbup", 1);
        mongoTemplate.updateFirst(query, update, "spit");
    }

    /**
     * 点赞(方式1)
     * @param id
     */
    /*public void updateThumbup(String id) {
        Spit spit = spitDao.findById(id).get();
        spit.setThumbup(spit.getThumbup()+1);
        spitDao.save(spit);
    }*/

    /**
     * 根据上级ID查询吐槽列表
     * @param parentId
     * @param page
     * @param size
     * @return
     */
    public Page<Spit> findByParentid(String parentId, int page, int size){
        PageRequest pageRequest = PageRequest.of(page -1, size);
        return spitDao.findByParentid(parentId, pageRequest);
    }

    /**
     * 查询所有
     * @return
     */
    public List<Spit> findAll(){
        return spitDao.findAll();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Spit findById(String id){
        return spitDao.findById(id).get();
    }

    /**
     * 保存
     * @param spit
     */
    public void save(Spit spit){
        spit.setId(idWorker.nextId() + "");
        spitDao.save(spit);
    }

    /**
     * 更新
     * @param spit
     */
    public void update(Spit spit){
        spitDao.save(spit);
    }

    /**
     * 根据id删除
     * @param id
     */
    public void deleteById(String id) {
        spitDao.deleteById(id);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
SpitController.java
package com.goblin.controller;

import com.goblin.entity.PageResult;
import com.goblin.entity.Result;
import com.goblin.entity.Spit;
import com.goblin.entity.StatusCode;
import com.goblin.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {
    @Autowired
    private SpitService spitService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分享数
     * @param id
     * @return
     */
    @GetMapping("/share/{id}")
    public Result share(@PathVariable String id){
        spitService.shareAdd(id);
        return new Result(true, StatusCode.OK, "分享成功");
    }

    /**
     * 访问量
     * @param id
     * @return
     */
    @GetMapping("/visits/{id}")
    public Result visits(@PathVariable String id) {
        spitService.visitsAdd(id);
        return new Result(true, StatusCode.OK, "访问量增加成功");
    }

    /**
     * 点赞(禁止重复点赞)
     * @param id
     */
    @GetMapping("/thumbup/{id}")
    public Result updateThumbup(@PathVariable String id) {
        String userId = "1";//模拟用户id
        if (redisTemplate.opsForValue().get("thumbup_" + userId + "_" + id) != null) {// 查询redis缓存是否有点赞记录
            return new Result(true, StatusCode.ERROR, "你已经点过赞了");
        }
        spitService.updateThumbup(id);
        redisTemplate.opsForValue().set("thumbup_" + userId + "_" + id, "1");// 点赞记录存入redis缓存中
        return new Result(true, StatusCode.OK, "点赞成功");
    }

    /**
     * 点赞
     * @param id
     */
    /*@PutMapping("/thumbup/{id}")
    public Result updateThumbup(@PathVariable String id) {
        spitService.updateThumbup(id);
        return new Result(true, StatusCode.OK, "点赞成功");
    }*/

    /**
     * 根据上级ID查询吐槽列表
     * @param parentId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/comment/{parentId}/{page}/{size}")
    public Result findByParentid(@PathVariable String parentId,
                                 @PathVariable int page,
                                 @PathVariable int size){
        Page<Spit> spits = spitService.findByParentid(parentId, page, size);
        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult<Spit>(spits.getTotalElements(),spits.getContent()));
    }

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Spit> spits = spitService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", spits);
    }

    /**
     * 根据id查询
     * @param Id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String Id) {
        return new Result(true, StatusCode.OK, "查询成功", spitService.findById(Id));
    }

    /**
     * 保存数据
     * @param spit
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Spit spit) {
        spitService.save(spit);
        return new Result(true, StatusCode.OK, "保存成功");
    }

    /**
     * 更新数据
     * @param spit
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Spit spit, @PathVariable String id) {
        spit.setId(id);
        spitService.update(spit);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        spitService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
表结构
字段名称字段含义字段类型
_idID文本
content吐槽内容文本
publishtime发布日期日期
userid发布人ID文本
nickname发布人昵称文本
visits浏览量整型
thumbup点赞数整型
share分享数整型
comment回复数整型
state是否可见文本
parentid上级ID文本
测试工具

项目启动成功,使用postman工具进行的测试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55949
推荐阅读
相关标签
  

闽ICP备14008679号