When you call the Save method of Mongotemplate, Spring-data-mongodb TypeConverter automatically adds a _class attribute to the document, and the value is the name of the class you saved. There's no harm in this design. Spring-data-mongodb is designed to convert a document to a Java object when it is converted to a specific subclass. But sometimes we don't want this field to appear, mainly because it looks more "annoying". You can solve this problem by setting the Mappingmongoconverter mongotypemapper.
The first argument to the constructor of the Defaultmongotypemapper class is the type name in MongoDB. When set to NULL, the _class property is not automatically added when saving. So I need to overwrite it.
Spring's configuration file method:
<mongo:mongo host= "localhost" port= "27017"/> <mongo:db-factory dbname= "database"/> <bean id= " Mappingcontext "class=" Org.springframework.data.mongodb.core.mapping.MongoMappingContext "/> <bean i D= "Defaultmongotypemapper" class= "Org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper" > <constructor-arg name= "Typekey" ><null/></constructor-arg> </bean> <bean id= "Mappin Gmongoconverter "class=" Org.springframework.data.mongodb.core.convert.MappingMongoConverter "> <constru Ctor-arg name= "Mongodbfactory" ref= "mongodbfactory"/> <constructor-arg name= "Mappingcontext" ref= "MappingCon Text "/> <property name=" typemapper "ref=" Defaultmongotypemapper "/> </bean> <bean I D= "Mongotemplate" class= "org.springframework.data.mongodb.core.MongoTemplate" > <constructor-arg name= " Mongodbfactory "ref=" MongodbfacTory "/> <constructor-arg name=" Mongoconverter "ref=" Mappingmongoconverter "/> </bean>
Annotation method:
@EnableMongoRepositories (basepackages = {"COM.DHP"}, Repositoryfactorybeanclass = Dhbmongorepositoryfac Torybean.class) @PropertySource ("Classpath:mongo.properties") @EnableMongoAuditing public class Mongoconfig extends A bstractmongoconfiguration {@Value ("${mongo.connectionstring}") Private String connectionString; @Value ("${mongo.dbname}") Private String dbName; @Autowired private ApplicationContext AppContext; @Override protected String GetDatabaseName () {return dbName; } @Override @Bean public Mongo Mongo () throws Exception {Mongoclienturi Mongoclienturi = new Mo Ngoclienturi (connectionString); return new Mongoclient (Mongoclienturi); } @Override @Bean public mongotemplate mongotemplate () throws Exception {Mongodbfactory Factory = Mongodbfactory (); Mongomappingcontext mongomappingcontext = new Mongomappingcontext (); Mongomappingcontext.setapplicationcontext (AppContext); Mappingmongoconverter converter = new Mappingmongoconverter (new Defaultdbrefresolver (Factory), Mongomappingcontext); Converter.settypemapper (new Defaultmongotypemapper (null)); return new Mongotemplate (factory, Converter); } @Bean public static Propertysourcesplaceholderconfigurer Propertyconfigindev () {return new Prope Rtysourcesplaceholderconfigurer (); } }
When using servlet 3 initialization to ensure that the application context is added to MONGO, the exception is reported if you do not add these two sentences: org.springframework.expression.spel.SpelEvaluationException
@Autowired
Private ApplicationContext AppContext;
Mongomappingcontext.setapplicationcontext (AppContext);
Remove _class columns from spring MongoDB