Last active
June 5, 2019 22:09
-
-
Save LenarBad/40863adb131613004535c4db476e5367 to your computer and use it in GitHub Desktop.
Heroku - mLab MongoDB - Spring Boot - How to start example
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
# MONGODB | |
spring.data.mongodb.uri=${MONGODB_URI} |
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
import org.springframework.data.annotation.Id; | |
import org.springframework.data.mongodb.core.mapping.Document; | |
@Document(collection = "stats") | |
public class StatEntry { | |
@Id | |
public String id; | |
private long time; | |
private String referrer; | |
private String site; | |
... | |
} |
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
... | |
<dependencies> | |
... | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-mongodb</artifactId> | |
</dependency> | |
... | |
</dependencies> | |
... |
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
import org.springframework.data.mongodb.repository.MongoRepository; | |
import java.util.List; | |
public interface StatEntryRepo extends MongoRepository<StatEntry, String> { | |
StatEntry findOneById(String id); | |
List<StatEntry> findAll(); | |
void delete(StatEntry entry); | |
void deleteAll(); | |
} |
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
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
import java.time.Instant; | |
import java.util.UUID; | |
@Component | |
public class StatService { | |
@Autowired | |
public StatEntryRepo statsRepo; | |
public void addStatEntry(String site, String referrer) { | |
StatEntry entry = new StatEntry(); | |
entry.setSite(site); | |
entry.setReferrer(referrer); | |
entry.setTime(Instant.now().toEpochMilli()); | |
entry.setId(UUID.randomUUID().toString()); | |
statsRepo.save(entry); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment