-
Notifications
You must be signed in to change notification settings - Fork 0
Home
nimamoosavi edited this page Aug 25, 2021
·
8 revisions
this project use for connect to mongodb service and implement the repository layer in crud project
The library works with Java 8+, ladder Core 1.0.1+ and implemented Crud Project
MongoTemplate to provide below feature
- Implementation of GeneralRepository for CRUD Project
- Sequence service
- Modify fieldName dynamically before save and after fetch even in runtime
- relocate nested field of a map object dynamically even in runtime which means before save and after fetch
- Change field name by content of desired variable
@Component
public class CostHeaderGeneralRepository extends MongoRepositoryServiceImpl<CostHeader, ObjectId> {
}
@Repository
public interface CostHeaderRepository extends MongoRepository<CostHeader, ObjectId> {
}
@Component
public class CostHeaderService {
@Autowire
CostHeaderGeneralRepository costHeaderGeneralRepository;
public void updateFields(ObjectId id, CostHeader costHeader) {
costHeaderGeneralRepository.updateField(id, costHeader);
}
}
if MongoFieldMapper exists, SimpleMongoRepository will call it before and after CRUD in MongoDB, otherwise continues to its business
for providing Sequence ,Move and Rename features, you must generate below bean, This bean allows you to rename and relocate and change fields dynamically
@Component
public class CostHeaderMapper extends MongoFieldMapper<CostHeader> {
}
public class MyService{
@Autowire
CostHeaderMapper costHeaderMapper;
public void customFunc(){
costHeaderMapper.getVariableGeneratorFields().put("dynamicFields.name".split(com.nicico.mongoclient.mapper.MongoFieldMapper.MONGO_FIELD_NAME_SEPARATOR)
,
new ValueModifier<Object>() {
@Override
public void before(Object o, org.bson.Document doc) {
}
@Override
public void after(org.bson.Document doc) {
}
}
);
}
}
@Document
public class CostHeader {
@Sequence(name = "Cost_Header_Number")
public Long number;
public void setNumber(Long number) {
this.number = number;
}
}
{
"number": null
}
{
"number": 80
}
public class SequenceUtil {
@AutoWired
SequenceGeneratorService sequenceGeneratorService;
public Long getNextSequence(String seqName) {
return sequenceGeneratorService.generateSequence();
}
}
public class CostRow {
private String type;
@FieldName(name = "type")
private Map<String, Object> dynamicFields;
}
{
"type": "train",
"dynamicFields": {
"name": "Green Train"
}
}
{
"type": "train",
"train": {
"name": "Green Train"
}
}
if you need to relocate nested fields of a map to everywhere in a document, you can use RelocateField annotation
public class CostRow {
private String type;
@RelocateField(source = {"staticFields", "dynamicFields.name"}, target = {"stat", "name"})
private Map<String, Object> dynamicFields;
}
{
"type": "train",
"staticFields": {
"price": 1000
},
"dynamicFields": {
"number": 80,
"name": "Green Train"
}
}
{
"type": "train",
"stat": {
"price": 1000
},
"name": "Green Train",
"dynamicFields": {
"number": 80
}
}