Mapper Layer Interface
MongoPlus provides a persistence layer interface, which can be used in two ways: via annotation scanning or manual customization/extension.
Annotation Scanning
By annotating the main application class with MongoMapperScan
and providing one or more package paths, MongoPlus will automatically register these interfaces as Beans. For Solon
, you only need to add the @Mongo
annotation to the interface and do not need MongoMapperScan
.
// Solon does not require this annotation
@MongoMapperScan("com.mongoplus.mapper")
public class MongoPlusApplication {
public static void main(String[] args) {
// Start the application
}
}
// Mapper interfaces need to extend the `MongoMapper` interface and specify the entity generic type
// For Solon, you need to annotate it with @Mongo here
public interface UserMongoMapper extends MongoMapper<User> {
}
This approach registers the mapper automatically without creating an implementation class, suitable for using only the basic features of MongoPlus.
Manual Registration
If you need custom or advanced operations beyond what the interface provides, you can manually extend the MongoMapper
interface and create an implementation class. This method does not depend on MongoMapperScan
.
// Mapper interfaces need to extend the `MongoMapper` interface and specify the entity generic type
public interface UserMongoMapper extends MongoMapper<User> {
}
// Implementation class needs to be registered as a Bean
@Repository
public class UserMongoMapperImpl extends MongoMapperImpl<User> implements UserMongoMapper {
}
Using either of these two methods, you can create the mapper layer and shift database interactions to it, eliminating the dependency on IService
.