SpringBoot -- 集成MongoDB

来源:互联网 发布:粉丝应援 知乎 编辑:程序博客网 时间:2024/06/14 02:44

前置工作

  • 熟悉本系列的工作环境
  • MongoDB已经搭建完成
  • MongoDB启用验证

引入&配置参数

引入 spring-boot-starter-data-MongoDB

build.gradle

compile ('org.springframework.boot:spring-boot-starter-data-mongodb:'+springBootVersion)
  • 1
  • 1

配置Mongodb参数

  • uri: mongodb://dev_xxx:dev_xxx@127.0.0.1:27017/kakme
  • dev_xxx:dev_xxx 为 username:password
  • kakme 为database
  • 如果用户名密码中带有URI特殊字符(”:”,”@”),请使用host方式

URI 方式:application.yml

spring:  data:    mongodb:      uri: mongodb://dev_xxx:dev_xxx@127.0.0.1:27017/kakme
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

OTHER: application.yml

spring:  data:    mongodb:      host: 127.0.0.1      port: 27017      username: dev_xxx      password: dev_xxxx      database: kakme
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

使用MongoRepository进行CRUI操作

创建Mongodb Bean,使用@Id标识 ObjectId

MessageInfo.Java

/** * @author cwenao * @version $Id MessageInfo.java, v 0.1 2017-01-30 12:29 cwenao Exp $$ */public class MessageInfo {    @Id    private String id;    private String title;    private String msgType;    private String msgInfo;    //getter setter    ... ...    @Override    public String toString() {        return "[ id ="+id+", title ="+title+", msgInfo="+msgInfo+" ]";    } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

自定义Repository进行额外的操作,集成 MongoRepository

MsgInfoRepository.java,此为 interface

/** * @author cwenao * @version $Id MsgInfoRepository.java, v 0.1 2017-01-30 12:33 cwenao Exp $$ */public interface MsgInfoRepository extends MongoRepository<MessageInfo,String> {    MessageInfo queryMsgInfoByTitle(String title);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

创建 controller,使用上几章的 UserInfoController

UserInfoController

/** * @author cwenao * @version $Id UserInfoController.java, v 0.1 2017-01-25 18:35 cwenao Exp $$ */@Controllerpublic class UserInfoController extends AbstractCacheSupport {    @Autowired    MsgInfoRepository msgInfoRepository;    @RequestMapping("/messageInfo")    public String messageInfo(String title, ModelMap modelMap) {        MessageInfo messageInfo = new MessageInfo();        messageInfo.setMsgInfo("hello world !");        messageInfo.setTitle(title);        messageInfo.setMsgType("1");        msgInfoRepository.save(messageInfo);          modelMap.addAttribute("test_mongodb",            msgInfoRepository.queryMsgInfoByTitle("cwenao").toString());        return "userinfo/accountInfo";    }}
  • 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
  • 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

测试

  • 依次启动 discovery、configserver、apigateway、dbserver

访问 http://localhost:10002/dbserver/messageInfo?title=cwenao

queryMsgInfoByTitle

mongodb库


代码

代码请移步 Github参考地址

如有疑问请加公众号(K171),如果觉得对您有帮助请 github start 

1 1