本文来源于:
来源:CSDN
原文:https://blog.csdn.net/ClementAD/article/details/50849680
去除class字段的本质为设置
DefaultMongoTypeMapper
typeKey为空
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/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.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 让Spring通过自动扫描来查询和管理Bean -->
<context:component-scan base-package="SpringMogo.mongo" />
<!-- 使Spring关注Annotation -->
<context:annotation-config/>
<!-- Default bean name is 'mongo' -->
<mongo:mongo host="localhost" port="27017" id="mongo" />
<mongo:db-factory dbname="chaoba" mongo-ref="mongo" />
<bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"></bean>
<bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<property name="typeMapper" ref="defaultMongoTypeMapper"></property>
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mongoMappingContext" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter"/>
</bean>
<mongo:repositories base-package="SpringMogo.mongo"></mongo:repositories>
</beans>
代码配置 @Configuration public class MongoConfigLog extends AbstractMongoConfiguration{ @Value("${mongo.ip}") private String MONGO_IP; @Value("${mongo.port}") private int MONGO_PORT; @Override protected String getDatabaseName() { return "dbName"; } @Override public Mongo mongo() throws Exception { return new MongoClient(MONGO_IP, MONGO_PORT); } //都只是为了去除“_class”字段 @Bean @Override public MappingMongoConverter mappingMongoConverter() throws Exception { MappingMongoConverter mmc = super.mappingMongoConverter(); mmc.setTypeMapper(new DefaultMongoTypeMapper(null)); return mmc; } }