Spring MVC + Mongodb
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring MVC + Mongodb,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8993字,纯文字阅读大概需要13分钟。
内容图文
![Spring MVC + Mongodb](/upload/InfoBanner/zyjiaocheng/484/3440d55734d24c749e4ac60cc9b5c162.jpg)
在applicationContext.xml增加mongodb.properties:
- <?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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache-4.2.xsd ">
- <context:component-scan base-package="com.zjf.*" />
- <!-- 属性文件位置 -->
- <bean id="annotationPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:config/properties/mongodb.properties</value>
- <value>classpath:config/properties/redis.properties</value>
- </list>
- </property>
- </bean>
- </beans>
mongodb.properties:
- mongo.hostport=192.168.1.101:27017
- mongo.connectionsPerHost=8
- mongo.threadsAllowedToBlockForConnectionMultiplier=4
- #\u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4
- mongo.connectTimeout=1000
- #\u7B49\u5F85\u65F6\u95F4
- mongo.maxWaitTime=1500
- mongo.autoConnectRetry=true
- mongo.socketKeepAlive=true
- #Socket\u8D85\u65F6\u65F6\u95F4
- mongo.socketTimeout=1500
- mongo.slaveOk=true
增加mongodb.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:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
- xsi:schemaLocation="http://www.springframework.org/schema/data/mongo
- http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
- <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
- <mongo:mongo id="mongo" replica-set="${mongo.hostport}">
- <!-- 一些连接属性的设置 -->
- <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:db-factory id="myMongoDbFactory" dbname="mydb" mongo-ref="mongo" />
- <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
- <constructor-arg name="mongoDbFactory" ref="myMongoDbFactory" />
- </bean>
- </beans>
配置已经完成,我们来看一下java代码的目录结构:
主要看model和daoimpl的代码。贴出如下:
- package com.zjf.spring.mongodb.model;
- import org.springframework.data.mongodb.core.mapping.Document;
- //将student映射为Mongodb数据库中的一个名字叫student的collection
- @Document(collection="student")
- public class Student {
- private String id;
- private String name;
- private int age;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
- }
- }
- package com.zjf.spring.mongodb.dao.impl;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.mongodb.core.MongoOperations;
- import org.springframework.data.mongodb.core.query.Criteria;
- import org.springframework.data.mongodb.core.query.Query;
- import org.springframework.data.mongodb.core.query.Update;
- import org.springframework.stereotype.Repository;
- import com.mongodb.WriteResult;
- import com.zjf.spring.mongodb.dao.StudentDao;
- import com.zjf.spring.mongodb.model.Student;
- //持久层注解
- @Repository
- public class StudentDaoImpl implements StudentDao {
- //引入mongo.xml中配置的mongoTemplate
- @Autowired
- private MongoOperations mongoTemplate;
- public void add(Student student)
- {
- this.mongoTemplate.insert(student);
- }
- public Student getById(String id)
- {
- return this.mongoTemplate.findById(id, Student.class);
- }
- public void modify(Student student)
- {
- this.mongoTemplate.save(student);
- }
- public void delete(Student student)
- {
- this.mongoTemplate.remove(student);
- }
- public void deleteById(String id)
- {
- this.mongoTemplate.remove(new Query(Criteria.where("_id").is(id)), Student.class);
- }
- //一个根据名字查询结果的方法
- @Override
- public List<Student> getByName(String name) {
- List<Student> students = new ArrayList<Student>();
- Query query = new Query();
- query.addCriteria(new Criteria("name").is(name));
- students = this.mongoTemplate.find(query, Student.class);
- return students;
- }
- ////一个根据名字更新数据方法
- @Override
- public int updateByName(String name) {
- int n = 0;
- List<Student> students = new ArrayList<Student>();
- Query query = new Query();
- query.addCriteria(new Criteria("name").is(name));
- Update update = new Update();
- update.set("name", "xhj");
- WriteResult result = this.mongoTemplate.updateMulti(query, update, Student.class);
- n = result.getN();
- return n;
- }
- }
Spring MVC + Mongodb
标签:blog cto 文件 sync keep figure sock 技术 dbf
本文系统来源:http://www.cnblogs.com/xiaolang8762400/p/6943107.html
内容总结
以上是互联网集市为您收集整理的Spring MVC + Mongodb全部内容,希望文章能够帮你解决Spring MVC + Mongodb所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。