Created
January 3, 2017 13:22
-
-
Save mp911de/efe10bcd7858ae4a06dea0088affff56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example; | |
import lombok.Data; | |
import java.math.BigDecimal; | |
import java.util.Arrays; | |
import javax.annotation.PostConstruct; | |
import org.bson.types.Decimal128; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.core.convert.converter.Converter; | |
import org.springframework.data.mongodb.core.MongoTemplate; | |
import org.springframework.data.mongodb.core.convert.CustomConversions; | |
import org.springframework.data.mongodb.core.mapping.Document; | |
import org.springframework.data.mongodb.core.query.Query; | |
import org.springframework.data.mongodb.core.query.Update; | |
import com.mongodb.DBObject; | |
/** | |
* Shows Mongo 3.4 {@link Decimal128} use with Spring Data MongoDB | |
* | |
* @author Mark Paluch | |
*/ | |
@SpringBootApplication | |
public class Decimal128WithSpringDataMongoDB { | |
@Bean | |
public CustomConversions customConversions() { | |
return new CustomConversions( | |
Arrays.asList(BigDecimalToDecimal128Converter.INSTANCE, Decimal128ToBigDecimalConverter.INSTANCE)) { | |
// Required for converter use (BigDecimal <-> Decimal128) | |
@Override | |
public Class<?> getCustomWriteTarget(Class<?> sourceType) { | |
if (sourceType == BigDecimal.class) { | |
return Decimal128.class; | |
} | |
return super.getCustomWriteTarget(sourceType); | |
} | |
// Required for native Decimal128 use | |
@Override | |
public boolean isSimpleType(Class<?> type) { | |
return type == Decimal128.class || super.isSimpleType(type); | |
} | |
}; | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(Decimal128WithSpringDataMongoDB.class, args); | |
} | |
@Autowired MongoTemplate mongoTemplate; | |
@PostConstruct | |
private void postConstruct() { | |
mongoTemplate.dropCollection("person"); | |
Person person = new Person(); | |
person.setDecimal(new BigDecimal("123.456")); | |
person.setDecimal128(new Decimal128(new BigDecimal("123.456"))); | |
mongoTemplate.save(person); | |
mongoTemplate.updateFirst(new Query(), new Update().inc("decimal", 5), Person.class); | |
Person loaded = mongoTemplate.findById(person.getId(), Person.class); | |
DBObject dbObject = mongoTemplate.findById(person.getId(), DBObject.class, "person"); | |
System.out.println(loaded.getDecimal()); | |
System.out.println(loaded.getDecimal128()); | |
System.out.println((Decimal128) dbObject.get("decimal")); | |
System.out.println((Decimal128) dbObject.get("decimal128")); | |
} | |
@Document | |
@Data | |
static class Person { | |
String id; | |
BigDecimal decimal; | |
Decimal128 decimal128; | |
} | |
private static enum BigDecimalToDecimal128Converter implements Converter<BigDecimal, Decimal128> { | |
INSTANCE; | |
@Override | |
public Decimal128 convert(BigDecimal bigDecimal) { | |
return new Decimal128(bigDecimal); | |
} | |
} | |
private static enum Decimal128ToBigDecimalConverter implements Converter<Decimal128, BigDecimal> { | |
INSTANCE; | |
@Override | |
public BigDecimal convert(Decimal128 decimal128) { | |
return decimal128.bigDecimalValue(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment