SSM整合Mongodb(已解决版本冲突问题)
前言
本实例使用在SSM框架
spring版本4.3.7.RELEASE
实现
pom.xml引入依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.23.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
创建spring-mongodb.xml
<?xml version="1.0" encoding="UTF-8"?>
<?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: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-1.10.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- mongodb配置 -->
<!-- <context:property-placeholder location="classpath:config.properties"/> -->
<!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
<mongo:mongo-client id="mongoClient" replica-set="hostpost" credentials="username:passwork@datebase">
<!-- 一些连接属性的设置 -->
<mongo:client-options connections-per-host=""
threads-allowed-to-block-for-connection-multiplier=""
connect-timeout=""
max-wait-time=""
socket-keep-alive=""
socket-timeout=""/>
</mongo:mongo-client>
<!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<mongo:db-factory id="mongoDbFactory" dbname="" mongo-ref="mongoClient" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
</bean>
<!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->
<mongo:mapping-converter base-package="com.mongodb.entity" />
<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<!-- 配置mongodb映射类型 -->
<bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>
<!-- 默认Mongodb类型映射 -->
<bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey">
<null /><!-- 这里设置为空,可以把 spring data mongodb 多余保存的_class字段去掉 -->
</constructor-arg>
</bean>
<!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
<mongo:repositories base-package="com.mongodb.dao" />
</beans>
spring-mvc.xml引入
<!-- 引入配置文件-->
<import resource="classpath:spring-mongodb.xml"></import>
<!-- 扫描包-->
<context:component-scan base-package="com.mongodb.*" />
创建映射bean
@Data
@Document(collection = "LocBean")
public class Loc implements Serializable {
@Id
private String dateTime;
private String name;
private String Longitude;
private String Latitude;
private String Addr;
}
创建dao
public interface LocDao {
/**
* 查询
* @param name
* @return
*/
List<Loc> getByTime(String name);
}
创建实现
@Slf4j
@Component(value = "LocDao ")
public class LocDaoImpl implements LocDao {
@Resource
private MongoTemplate mongoTemplate;
@Override
public List<Loc> getByTime(String name) {
try {
Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
return mongoTemplate.find(query,Loc.class);
}catch (Exception e1){
log.info("[MongoDB-FIND] ERROR MSG={},Data=",e1.getMessage());
return null;
}
}
}
结构