Last active
March 19, 2016 21:43
-
-
Save arthurportas/f164ac4a8a1bf9622e70 to your computer and use it in GitHub Desktop.
Sample spring-data mongodb project setup with querydsl
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 com.arthurportas.domain.mongo.entities.User; | |
public interface UserRepository extends MongoRepository<User, Long> { | |
User findBySid(UUID userSid); | |
} |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.arthurportas</groupId> | |
<artifactId>com.arthurportas.data.mongo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>SPRING_DATA_MONGO_QUERY_DSL</name> | |
<description></description> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<java.version>1.8</java.version> | |
<querydsl.version>4.0.7</querydsl.version> | |
<querydsl.apt-maven-plugin.version>1.1.3</querydsl.apt-maven-plugin.version> | |
<com.mysema.querydsl.version>3.7.1</com.mysema.querydsl.version> | |
</properties> | |
<dependencies> | |
<!-- Querydsl --> | |
<dependency> | |
<groupId>com.mysema.querydsl</groupId> | |
<artifactId>querydsl-apt</artifactId> | |
<version>${com.mysema.querydsl.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.mysema.querydsl</groupId> | |
<artifactId>querydsl-mongodb</artifactId> | |
<version>${com.mysema.querydsl.version}</version> | |
<exclusions> | |
<exclusion> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-mongodb</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<!-- Generates the MongoDB static meta model --> | |
<plugin> | |
<groupId>com.mysema.maven</groupId> | |
<artifactId>apt-maven-plugin</artifactId> | |
<version>${querydsl.apt-maven-plugin.version}</version> | |
<executions> | |
<execution> | |
<id>process-common-model</id> | |
<goals> | |
<goal>process</goal> | |
</goals> | |
<configuration> | |
<!--target folder with generated metamodels Qxxxx.java--> | |
<outputDirectory>target/generated-sources/apt</outputDirectory> | |
<processors> | |
<processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor> | |
</processors> | |
</configuration> | |
</execution> | |
</executions> | |
<dependencies> | |
<dependency> | |
<groupId>com.mysema.querydsl</groupId> | |
<artifactId>querydsl-apt</artifactId> | |
<version>${com.mysema.querydsl.version}</version> | |
</dependency> | |
</dependencies> | |
</plugin> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
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 com.mongodb.Mongo; | |
import com.mongodb.MongoClient; | |
import com.mongodb.MongoCredential; | |
import com.mongodb.ServerAddress; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.mongodb.config.AbstractMongoConfiguration; | |
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; | |
import java.util.Arrays; | |
@Configuration | |
@EnableMongoRepositories(basePackages = "com.arthurportas.repo.mongo") | |
@ComponentScan(basePackages = "com.arthurportas") | |
public class SpringMongoConfig extends AbstractMongoConfiguration { | |
@Value("${mongo.host}")//read from config | |
private String host; | |
@Value("${mongo.port}")//read from config | |
private int port; | |
@Value("${mongo.db}")//read from config | |
private String databaseName; | |
@Value("${mongo.username}")//read from config | |
private String userName; | |
@Value("${mongo.password}")//read from config | |
private String password; | |
@Override | |
public String getDatabaseName() { | |
return databaseName; | |
} | |
@Override | |
@Bean | |
public Mongo mongo() throws Exception { | |
return new MongoClient(serverAddress(), Arrays.asList(mongoCredential())); | |
} | |
@Override | |
protected String getMappingBasePackage() { | |
return "com.arthurportas.domain.mongo.entities"; | |
} | |
@Bean | |
public ServerAddress serverAddress() throws Exception { | |
return new ServerAddress();//TODO: read host+port from config | |
} | |
@Bean | |
public MongoCredential mongoCredential() { | |
return MongoCredential.createCredential( | |
this.userName, | |
getDatabaseName(), | |
this.password.toCharArray()); | |
} | |
} |
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 java.math.BigInteger; | |
import java.time.Instant; | |
import java.util.Map; | |
import org.springframework.data.annotation.Id; | |
import org.springframework.data.annotation.Version; | |
import org.springframework.data.mongodb.core.index.Indexed; | |
import org.springframework.data.mongodb.core.mapping.Document; | |
@Document(collection = User.USERS_COLLECTION_NAME) | |
public class User { | |
public static final String USERS_COLLECTION_NAME = "users_docs"; | |
@Id | |
private BigInteger id; | |
@Indexed(unique = true) | |
UUID sid; | |
String name; | |
String email; | |
Map<String, String> attributes; | |
Instant createdAt; | |
Instant updatedAt; | |
// … getters and setters omitted | |
} |
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.BeanUtils; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import com.arthurportas.repo.mongo.UserRepository; | |
@Service | |
public class UserService { | |
@Autowired | |
private UserRepository userRepository; | |
public User saveUser(User user) throws Exception { | |
return userRepository.save(user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment