<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
<info
organisation=""
module="adagent"
status="integration">
</info>
<configurations>
<conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime,master"/>
<conf name="master" visibility="public" description="contains only the artifact published by this module itself, with no transitive dependencies"/>
<conf name="compile" visibility="public" description="this is the default scope, used if none is specified. Compile dependencies are available in all classpaths."/>
<conf name="provided" visibility="public" description="this is much like compile, but indicates you expect the JDK or a container to provide it. It is only available on the compilation classpath, and is not transitive."/>
<conf name="runtime" visibility="public" description="this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath." extends="compile"/>
<conf name="test" visibility="private" description="this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases." extends="runtime"/>
<conf name="system" visibility="public" description="this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository."/>
<conf name="sources" visibility="public" description="this configuration contains the source artifact of this module, if any."/>
<conf name="javadoc" visibility="public" description="this configuration contains the javadoc artifact of this module, if any."/>
<conf name="optional" visibility="public" description="contains all optional dependencies"/>
</configurations>
<dependencies>
<!--spring-->
<dependency org="org.springframework" name="spring-core" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-beans" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-web" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-context" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-context-support" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-aop" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-expression" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-tx" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-jms" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-jdbc" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework" name="spring-webmvc" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.slf4j" name="slf4j-api" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.slf4j" name="jcl-over-slf4j" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.slf4j" name="jul-to-slf4j" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.slf4j" name="log4j-over-slf4j" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="ch.qos.logback" name="logback-classic" rev="1.1.2" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="ch.qos.logback" name="logback-core" rev="1.1.2" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<!--json序列化-->
<dependency org="com.fasterxml.jackson.core" name="jackson-core" rev="2.3.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="com.fasterxml.jackson.core" name="jackson-annotations" rev="2.3.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.3.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<!--HTTP-->
<dependency org="org.aopalliance" name="com.springsource.org.aopalliance" rev="1.0.0" />
<dependency org="org.springframework.data" name="spring-data-commons" rev="1.10.0.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.apache.httpcomponents" name="httpclient" rev="4.3.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.apache.httpcomponents" name="httpcore" rev="4.3.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.apache.httpcomponents" name="httpmime" rev="4.3.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.apache.ws.xmlschema" name="xmlschema-core" rev="2.1.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<!--cxf-->
<dependency org="org.apache.cxf" name="cxf-bundle" rev="2.7.11" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.apache.neethi" name="neethi" rev="3.0.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.apache.velocity" name="velocity" rev="1.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.apache.ws.xmlschema" name="xmlschema-core" rev="2.1.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="javax.xml.bind" name="jaxb-api" rev="2.2.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="javax.xml.soap" name="saaj-api" rev="1.3.5" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="com.sun.xml.bind" name="jaxb-xjc" rev="2.2.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="com.sun.xml.bind" name="jaxb-core" rev="2.2.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="com.sun.xml.messaging.saaj" name="saaj-impl" rev="1.3.23" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.codehaus.woodstox" name="woodstox-core-asl" rev="4.3.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.codehaus.woodstox" name="stax2-api" rev="3.1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.jvnet.staxex" name="stax-ex" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.codehaus.jettison" name="jettison" rev="1.3.5" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="wsdl4j" name="wsdl4j" rev="1.6.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <!-- axis相关内容 -->
<dependency org="axis" name="axis" rev="1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" />
<dependency org="axis" name="axis-jaxrpc" rev="1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" />
<dependency org="axis" name="axis-saaj" rev="1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" />
<dependency org="axis" name="axis-wsdl4j" rev="1.5.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" />
<dependency org="commons-discovery" name="commons-discovery" rev="0.2" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" /> <dependency org="com.google.code.gson" name="gson" rev="2.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="dom4j" name="dom4j" rev="1.6.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<!--
<dependency org="org.apache.axis" name="axis" rev="1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
-->
<dependency org="jaxen" name="jaxen" rev="1.1-beta-9" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="javax.validation" name="validation-api" rev="1.1.0.Final" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" />
<dependency org="javax.ws.rs" name="javax.ws.rs-api" rev="2.0-m10" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.springframework.data" name="spring-data-mongodb" rev="1.7.0.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.mongodb" name="mongo-java-driver" rev="3.0.0-rc1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="org.apache.solr" name="solr-solrj" rev="5.1.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.commons" name="commons-vfs2" rev="2.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="commons-discovery" name="commons-discovery" rev="0.5" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="commons-logging" name="commons-logging" rev="1.2" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
<dependency org="javax.xml.rpc" name="javax.xml.rpc-api" rev="1.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
</dependencies>
</ivy-module>

ivy.xml

1、先引入以上的jar包:

2、使用MongoTemplate:

package com.d.service;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; //@RestController
//@RequestMapping("/test")
public class TestService { private static final Logger logger = LoggerFactory.getLogger(TestService.class); @Autowired
MongoTemplate mongoTemplate; @RequestMapping(value = "/check", method = RequestMethod.GET)
@ResponseBody
public JsonResult<String> check(HttpServletRequest request) {
try {
logger.info("Address:{}", request.getLocalAddr());
logger.info("Name:{}", request.getLocalName());
} catch (Throwable e) {
logger.error(e.getMessage());
logger.debug(e.getMessage(), e);
}
return new JsonResult<String>("服务运行正常...").success();
} @RequestMapping(value = "/add", method = RequestMethod.GET)
@ResponseBody
public JsonResult<String> add(HttpServletRequest request) {
try {
TestMo mo = new TestMo();
mo.setId("1111");
mo.setMessage("2222");
mo.setName("3333");
mongoTemplate.insert(mo); } catch (Throwable e) {
logger.error(e.getMessage());
logger.debug(e.getMessage(), e);
return new JsonResult<String>(e.getMessage()).fail(0);
}
return new JsonResult<String>("服务运行正常...").success();
}
}

3、使用Reosititory:

package com.repository;

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

import com.model.MaterialInfo;

public interface MaterialRepository extends MongoRepository<MaterialInfo, String>{

}

Api:

package com.api;

import javax.validation.Valid;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam; import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query; import com.model.MaterialInfo;
import com.utils.QueryResult; public interface MaterialApi { @POST
@Path("insert")
MaterialInfo insert(@Valid MaterialInfo materialInfo); @DELETE
@Path("delete")
void delete(@QueryParam("id") String id); @POST
@Path("update")
void update(@QueryParam("id") String id); @POST
@Path("save")
MaterialInfo save(@Valid MaterialInfo materialInfo); @GET
@Path("get")
MaterialInfo getBroadCastList(String id); @POST
@Path("query/with/paging")
public QueryResult<MaterialInfo> queryWithPaging(Query query, Sort sort, //
int start, int limit);
}

Impl:

package com.api.impl;

import javax.ws.rs.Path;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component; import com.api.MaterialApi;
import com.model.MaterialInfo;
import com.repository.MaterialRepository;
import com.utils.QueryResult; @Path("/material")
@Component("materialImpl")
public class MaterialApiImpl implements MaterialApi { @Autowired
private MaterialRepository _materialRepository; @Autowired
@Qualifier("mongoTemplate")
MongoTemplate mongoTemplate; @Override
public MaterialInfo insert(MaterialInfo materialInfo) {
// TODO Auto-generated method stub
return _materialRepository.insert(materialInfo);
} @Override
public void delete(String id) {
// TODO Auto-generated method stub } @Override
public MaterialInfo save(MaterialInfo materialInfo) {
// TODO Auto-generated method stub
return null;
} @Override
public MaterialInfo getBroadCastList(String id) {
// TODO Auto-generated method stub
return null;
} @Override
public QueryResult<MaterialInfo> queryWithPaging(Query query, Sort sort, int start, int limit) {
// TODO Auto-generated method stub
return null;
} @Override
public void update(String id) {
// TODO Auto-generated method stub } }

Service:

package com.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.api.MaterialApi;
import com.model.MaterialInfo; @Component("materialService")
public class MaterialService { public static Logger logger = LoggerFactory.getLogger(MaterialService.class); @Autowired
MaterialApi _materialApi; public String save(MaterialInfo materialInfo){
try {
_materialApi.save(materialInfo);
} catch (Throwable e) {
return e.getMessage();
}
return "success";
}
}

以上!

缺少关键的beans.xml文件 补上去:注意其中的<mongo:repositories />很重要;

<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
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-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.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
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 自动扫描 -->
<context:annotation-config /> <mvc:annotation-driven /> <context:component-scan base-package="com.d" /> <import resource="classpath:com/d/beans.xml"/> <mongo:repositories base-package="com.d.repository"></mongo:repositories> <context:property-placeholder location="classpath:com/d/mongodb.properties" />
<!--
<mongo:mongo id="mongo" host="${mongodb.host}" port="${mongodb.port}" />
-->
<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" />
</bean> </beans>
以上文件加载只能加载classpath下的文件,加载外部文件需修改配置如下:
<context:property-placeholder location="file:${app.home}/config/mongodb.properties" />
重点在添加file标识 mongodb.properties:
#mongodb
mongodb.database=adbase
mongodb.replicaSet=127.0.0.1:27017
#mongodb.host=127.0.0.1
#mongodb.port=27017

SpringMVC连接MongoDB操作数据库的更多相关文章

  1. 【node】node连接mongodb操作数据库

    1.下载第三方模块mongodb cnpm install mongodb --save 2.检测是否连接成功 1.引入第三方模块mongodb并创建一个客户端 const MongoClient = ...

  2. 【node】------node连接mongodb操作数据库------【巷子】

    1.下载第三方模块mongodb cnpm install mongodb --save 2.检测是否连接成功 1.引入第三方模块mongodb并创建一个客户端 const MongoClient = ...

  3. Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库

    概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...

  4. 使用python简单连接并操作数据库

    python中连接并操作数据库 图示操作流程 一.使用的完整流程 # 1. 导入模块 from pymysql import connect # 2. 创建和数据库服务器的连接,自行设置 服务器地址, ...

  5. PHP连接MYSQL操作数据库

    PHP连接MYSQL操作数据库 <?php $con = mysql_connect("localhost","root",""); ...

  6. MongoDB聚合查询及Python连接MongoDB操作

    今日内容概要 聚合查询 Python操作MongoDB 第三方可视化视图工具 今日内容详细 聚合查询 Python操作MongoDB 数据准备 from pymongo import MongoCli ...

  7. MongoDB操作数据库的几个命令(自己用)

    本文以配置百度的Clouda为源头:http://cloudajs.org/docs 继而安装node.js:http://nodejs.org/download/(直接安装,简单) 和MongoDB ...

  8. java方式连接数据操作数据库

    package com.bdqn.dao.impl; import java.io.IOException;import java.io.InputStream;import java.io.Seri ...

  9. Python连接MongoDB操作

    1.安装PyMongo 注意:请勿安装“bson”软件包. PyMongo配有自己的bson包; 执行“pip install bson”或“easy_install bson”则会安装与PyMong ...

随机推荐

  1. gradle发布jar包

    在.gradle文件中添加以下代码 apply plugin: 'application' mainClassName = 'com.excel.ExcelApplication' jar { doF ...

  2. IEEE1588协议简介

    IEEE1588协议,又称PTP(precise time protocol,精确时间协议),可以达到亚微秒级别时间同步精度,于2002年发布version1,2008年发布version2. IEE ...

  3. iOS APP跳转设置界面以及设置中的其他界面

    1.跳转设置总页面(iOS10+以及之前的都可以用:ios10+ 是跳转到了应用到设置界面) [[UIApplication sharedApplication]openURL:[NSURL URLW ...

  4. Android Looper原理分析

    实际业务使用场景: 某业务场景需要将本地数据传递到服务端,服务端再返回传递成功或者失败的信息. 1. 失败时: 重传5次 2.设置客户端请求的最小时间间隔,这个间隔内最多请求1次 具体逻辑如下:(这里 ...

  5. shell if相关参数

    [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真. [ ...

  6. JavaScript的DOM编程--10--删除节点

    1). removeChild(): 从一个给定元素里删除一个子节点 var reference = element.removeChild(node); 返回值是一个指向已被删除的子节点的引用指针. ...

  7. Fiddler的hosts配置使用

    前提:使用fiddler的hosts配置,可以方便的配置自己想要测试环境,不需要每次配置hosts都到windows目录下去修改hosts文件 1.点击Tool->HOSTS,打开hosts的配 ...

  8. rsyslogd以及日志轮替logrotate的梳理

    rsyslog 1)日志类型 auth :(authpriv) 主要与认证有关的机制,例如 login, ssh, su 等需要帐号/密码的咚咚: cron: 就是例行性工作排程 cron/at 等产 ...

  9. [代码审计]青云客Cms前台有条件注入至getshell,后台xss至getshell、至弹你一脸计算器

    之前写了一篇关于青云客cms的文章,发在了t00ls,就不copy过来了. 给个链接,好记录一下. https://www.t00ls.net/thread-43093-1-1.html

  10. Android开发——使用高级的RecyclerView实现侧滑菜单删除功能(SwipeRecyclerView)

    使用之前,先简单介绍一下这个SwipeRecyclerView,这是严大(严振杰)基于RecyclerView的进行修改和封装的高级RecyclerView,其可以实现像QQ聊天界面的侧滑删除菜单,和 ...