MongoDB+spring配置开发

本例子是利用MongoTemplate实现存储的

1、mongo.xml配置文件,主要是配置mongo一些连接的信息

<?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: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">

    <!--  host:mongo服务器的ip,port:端口号-->
    <mongo:mongo host="${mongo.host}" port="${mongo.port}">
        <mongo:options
                connections-per-host="${mongo.connectionsPerHost}"
                threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                connect-timeout="${mongo.connectTimeout}"
                max-wait-time="${mongo.maxWaitTime}"
                auto-connect-retry="${mongo.autoConnectRetry}"
                socket-keep-alive="${mongo.socketKeepAlive}"
                socket-timeout="${mongo.socketTimeout}"
                slave-ok="${mongo.slaveOk}"
                write-number="1"
                write-timeout="0"
                write-fsync="true" />
    </mongo:mongo>

    <!-- dbname:mongo的数据库名 -->
    <mongo:db-factory dbname="lsl" mongo-ref="mongo" />

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>

</beans>

2、mongo.properties,主要配置了mongo的信息

mongo.host=127.0.0.1
mongo.port=27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=1500
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=1500
mongo.slaveOk=true

3、把mongo.xml引入到spring.xml配置文件

<?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:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
      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.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
          http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
   <!-- 开启注解扫描 -->
   <context:component-scan base-package="com.lsl.generalevent" /> 
   
    
   <!-- 开启aop注解方式,此步骤不能少,这样java类中的aop注解才会生效 -->
   <!-- <aop:aspectj-autoproxy /> -->

   <!-- 加载配置文件 -->
   <!-- <context:property-placeholder location="classpath:application.properties" />-->
   <bean id="annotationPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
         <list>
            <value>classpath:mongo.properties</value>
            <value>classpath:application.properties</value>
         </list>
      </property>
   </bean>

   <!-- 数据库连接池 -->
     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      destroy-method="close">
      <property name="driverClassName" value="${spring.datasource.driverClassName}" />
      <property name="url" value="${spring.datasource.url}" />
      <property name="username" value="${spring.datasource.username}" />
      <property name="password" value="${spring.datasource.password}" />
      <property name="maxActive" value="10" />
      <property name="maxIdle" value="5" />
   </bean>

   <!-- 创建JdbcTemplate对象 -->
   <bean id="jdbcTemplete" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSource"></property>
   </bean>
   
      
   <!-- 配置切面 -->
   <bean id="serviceEventAdvice1" class="com.lsl.generalevent.support.ServiceEventAdvice"/>
   <!-- 配置事件存储 -->
   <bean id="saveEvent" class="com.lsl.generalevent.dao.saveevent.impl.SaveEventImpl"/>
   <bean id="lsl" class="com.lsl.generalevent.example.service.MyService"/>   
   
   <!-- 配置切入点及通知类型-->
    <aop:config>
        <aop:aspect id="serviceEventAspect1" ref="serviceEventAdvice1" order="100">
            <aop:pointcut id="servicePointcut1"
                          expression="${aop.piontcut.express1}"/>
            <aop:before method="before" pointcut-ref="servicePointcut1"/>
        </aop:aspect>
    </aop:config>

   <!-- 导入mongoxml-->
   <import resource="classpath:mongodb.xml"/>
   

</beans>

4、以上配置好后,就可以写java代码了

public class SaveEventImpl implements ISaveEvent {
   
 

   @Autowired
   MongoTemplate mongoTemplate;

  

   @Override
   public void mongoSave( Object object) {
      System.out.println("dfsfsfsdfsdf");
      //inser方法的第二个参数表示mongo中的集合(collection),也就是表明
      mongoTemplate.insert(object,"lsl");
   }

}

5、项目结构图如下:

6、数据存入mongo后,在客户端查询如下:

 

猜你喜欢

转载自blog.csdn.net/dhklsl/article/details/83578653
今日推荐