工程目录结构:
applicationContext-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:context="http://www.springframework.org/schema/context"
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.0.xsd
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-2.0.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">
<!-- 连接属性文件,配置服务器IP端口、数据库和连接参数 -->
<context:property-placeholder location="classpath:config/mongodb.properties"
ignore-unresolvable="true" />
<!-- 创建MongoClient -->
<mongo:mongo-client id="mongo" replica-set="${mongo.hostport}" >
<mongo:client-options connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier=
"${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}" />
</mongo:mongo-client>
<!-- MongoDbFactory -->
<mongo:db-factory id="mgFactory" dbname="${mongo.dbname}"
mongo-ref="mongo" />
<!-- MongoTemplate -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mgFactory" />
</bean>
</beans>
mongodb.properties配置:
mongo.hostport=127.0.0.1:27017
mongo.dbname=spitdb
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=1000
mongo.maxWaitTime=1500
mongo.socketKeepAlive=true
mongo.socketTimeout=1500
applicationContext.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" 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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 组件扫描 -->
<context:component-scan base-package="hncj.edu.service" />
<!--使用注解管理bean -->
<context:annotation-config />
</beans>
Tbperson类:
package hncj.edu.service.pojo;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.io.Serializable;
import java.util.Date;
@Document(collection = "employee")
public class Tbperson implements Serializable {
@Id
@Field(value = "_id")
private String id;
@Field(value = "name")
private String name;
@Field
private Integer age;
@Field
private String createTime = new Date().toLocaleString();
public Tbperson() {
}
public Tbperson(String name, Integer age) {
this.name = name;
this.age = 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "Tbperson{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", createTime='" + createTime + '\'' +
'}';
}
}
TbpersonDaoImpl配置:
package hncj.edu.service.Dao;
import hncj.edu.service.pojo.Tbperson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class TbpersonDaoImpl {
@Autowired
private MongoTemplate mongoTemplate;
public List<Tbperson> findAll() {
System.out.println(mongoTemplate.getCollectionNames());
Query query = new Query();
return mongoTemplate.findAll(Tbperson.class,"employee");
}
public void insertOneTbperson(Tbperson Tbperson) {
mongoTemplate.insert(Tbperson);
}
public void deleteOneTbpersonByName(String name) {
Criteria c = new Criteria();
c.and("name").is(name);
Query query = new Query(c);
mongoTemplate.remove(query, Tbperson.class);
}
public void deleteOneTbperson(String id) {
Criteria c = new Criteria();
c.and("_id").is(id);
Query query = new Query(c);
mongoTemplate.remove(query, Tbperson.class);
}
public Tbperson findByName(String name) {
Criteria c = new Criteria();
c.and("name").is(name);
Query query = new Query(c);
return mongoTemplate.findOne(query, Tbperson.class);
}
public Tbperson find(String id) {
return mongoTemplate.findById(id, Tbperson.class);
}
}
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
源码:链接:https://pan.baidu.com/s/1FrNia7-YXdbHm64Um44fjw
提取码:jzfo
复制这段内容后打开百度网盘手机App,操作更方便哦