Movie node entity 內容如下:
package neo4j.springdata.vo; import org.neo4j.ogm.annotation.GeneratedValue; import org.neo4j.ogm.annotation.Id; import org.neo4j.ogm.annotation.Index; import org.neo4j.ogm.annotation.NodeEntity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; import lombok.RequiredArgsConstructor; @NodeEntity @Data @NoArgsConstructor @RequiredArgsConstructor @AllArgsConstructor public class Movie { @Id @GeneratedValue private Long id; @Index(unique = true) @NonNull private String name; }
Repository interface / class 內容如下:
package neo4j.springdata.repository; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import neo4j.springdata.repository.custom.MovieRepositoryCustom; import neo4j.springdata.vo.Movie; @Repository public interface MovieRepository extends CrudRepository<Movie, Long>, MovieRepositoryCustom { }
package neo4j.springdata.repository.custom; import java.util.List; import neo4j.springdata.vo.Movie; public interface MovieRepositoryCustom { List<Movie> findSimilarName(String name); }
package neo4j.springdata.repository.impl; import java.util.HashMap; import java.util.List; import java.util.Map; import org.neo4j.ogm.session.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.google.common.collect.ImmutableList; import neo4j.springdata.repository.custom.MovieRepositoryCustom; import neo4j.springdata.vo.Movie; @Component public class MovieRepositoryImpl implements MovieRepositoryCustom { @Autowired private Session session; @Override public List<Movie> findSimilarName(String name) { String cypher = "match (m:Movie) where m.name =~ {name} return m"; Map<String, Object> params = new HashMap<>(); params.put("name", ".*" + name + ".*"); Iterable<Movie> result = session.query(Movie.class, cypher, params); return ImmutableList.copyOf(result); } }
Service class 內容如下:
package neo4j.springdata.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import neo4j.springdata.repository.MovieRepository; import neo4j.springdata.vo.Movie; @Service public class MovieService { @Autowired private MovieRepository movieRepo; @Transactional public void createMovies(List<Movie> movies) { movieRepo.saveAll(movies); } public List<Movie> findSimilarName(String name) { return movieRepo.findSimilarName(name); } }
Client class 內容如下:
package neo4j.springdata; import java.util.List; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.Bean; import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; import lombok.extern.slf4j.Slf4j; import neo4j.springdata.service.MovieService; import neo4j.springdata.vo.Movie; @SpringBootApplication @EnableNeo4jRepositories("neo4j.springdata.repository") @EntityScan(basePackages = "neo4j.springdata.vo") @Slf4j public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Bean CommandLineRunner createCustomQuery(MovieService movieService) { return args -> { List<Movie> movies = movieService.findSimilarName("Fa"); log.info("movies = " + movies.toString()); }; } }
pom.xml 內容如下:
<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>neo4j</groupId> <artifactId>springdata</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springdata</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.7.Final</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-libs-release</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
No comments:
Post a Comment