spring集成mongodb操作以及配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p" 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.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <context:property-placeholder location="classpath:mongo.properties" /><context:component-scan base-package="com.mongo.dao" /><context:component-scan base-package="com.mongo.service" /><!-- Default bean name is 'mongo' --><mongo:mongo host="localhost" port="27017"><mongo:options connections-per-host="8"threads-allowed-to-block-for-connection-multiplier="4"connect-timeout="1000" max-wait-time="1500" auto-connect-retry="true"socket-keep-alive="true" socket-timeout="1500" slave-ok="true"write-number="1" write-timeout="0" write-fsync="true" /></mongo:mongo><!-- 注册MongoDbFactory类 --><mongo:db-factory id="mongoDbFactory" host="localhost"port="27017" dbname="bylw" username="xiaozhi" password="123" /><!-- 注册MongoTemplate类 --><bean id="mongoTemplate" /><!-- <constructor-arg name="databaseName" value="xiaozhi" /> --></bean></beans>
?domain类:Student
package com.mongo.domain;import org.springframework.data.annotation.Id;import org.springframework.data.annotation.PersistenceConstructor;import org.springframework.data.mongodb.core.mapping.Document;/* * 学生信息集合类 */@Documentpublic class Student {@Idprivate String sno; // 学号private String sname; // 姓名private String ssex; // 性别private int sage; // 年龄private String sdept; // 学院private String smajor; // 专业private String s_entrance; // 入学时间private String s_class; // 班级private String subject_id = "";// 选择的题目 get{} set{}...}
?Dao类
package com.mongo.dao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.stereotype.Repository;import com.mongo.domain.Student;@Repositorypublic class GraduateProjectDao {@Autowiredprivate MongoTemplate mongoTemplate;//添加学生public void addStudent(Student student){mongoTemplate.insert(student);}}
?
测试:
package com.mongo.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.data.mongodb.core.MongoTemplate;import com.mongo.dao.GraduateProjectDao;import com.mongo.domain.Student;public class TestMain {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");System.out.println(ctx.getBean("mongoTemplate",MongoTemplate.class));Student student = new Student("2010021443", "王智", "男", 21, "管理工程学院", "信管", "2010", "102");GraduateProjectDao gpd = ctx.getBean("graduateProjectDao",GraduateProjectDao.class);gpd.addStudent(student);}}
?