背景

旧项目中依然在使用springmvc的xml传统配置,最近须要使用MongoDB,须要在spring引入MongoDB的链接配置。html

解决

配置mongodb数据库

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mongo="http://www.springframework.org/schema/data/mongo"

xsi:schemaLocation=

"http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

配置mongodb数据工厂

配置mongodb的Template

配置mongodb的dao文件夹位置

mongodb的dao实现

package com.xxx.dao;

import com.xxx.model.Form;

import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface FormRepository extends MongoRepository

{

public List

findByCreator(String creator);

}

mongodb的service实现

package com.xxx.service;

import com.xxx.dao.FormRepository;

import com.xxx.model.Form;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "formService")

public class FormService {

@Autowired

FormRepository formRepository;

/**

* 根据建立者,获取表单

* @param creator 建立者

* @return 表单列表

*/

public List

findByCreator(String creator){

return formRepository.findByCreator(creator);

}

}

感觉

若是使用spring boot的配置文件配置的这个方式,是最好的。能不用xml配置就不用,毕竟官方首先教咱们都是去xml化配置的。java

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐