mirror of
https://github.com/ityouknow/spring-boot-examples.git
synced 2026-06-01 02:19:17 +08:00
m
This commit is contained in:
@@ -19,7 +19,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准
|
||||
- [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx) :Docker Compose + Spring Boot + Nginx + Mysql 示例
|
||||
- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner) :Spring Boot 使用 commandLineRunner 实现项目启动时资源初始化示例
|
||||
- [spring-boot-web-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web-thymeleaf) :Spring Boot 使用 thymeleaf 实现布局、验参、增删改查示例
|
||||
- [spring-boot-elasticsearch](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-elasticsearch) :Spring Boot 使用 Elasticsearch 增删改查示例
|
||||
|
||||
|
||||
**参考文章**
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ Spring Boot examples, using the simplest and the most useful scene demos.
|
||||
- [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx) :Docker Compose + Spring Boot + Nginx + Mysql example
|
||||
- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner) :Example of resource initialization at project startup using Spring Boot and commandLineRunner
|
||||
- [spring-boot-web-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web-thymeleaf) :Spring Boot uses thymeleaf to implement layout, check parameters and CURD
|
||||
- [spring-boot-elasticsearch](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-elasticsearch) :Spring Boot uses Elasticsearch CURD demo
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
<?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.neo</groupId>
|
||||
<artifactId>spring-boot-atomikos</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-boot-atomikos</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -1,15 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.neo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class JtaAtomikosApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(JtaAtomikosApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.neo.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class DataSourceConfig {
|
||||
|
||||
@Bean(name = "primaryDataSource")
|
||||
@Qualifier("primaryDataSource")
|
||||
@ConfigurationProperties(prefix="spring.primary.datasource")
|
||||
public DataSource primaryDataSource() {
|
||||
return new AtomikosDataSourceBean();
|
||||
}
|
||||
|
||||
@Bean(name = "secondaryDataSource")
|
||||
@Qualifier("secondaryDataSource")
|
||||
@Primary
|
||||
@ConfigurationProperties(prefix="spring.secondary.datasource")
|
||||
public DataSource secondaryDataSource() {
|
||||
return new AtomikosDataSourceBean();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.neo.config;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(
|
||||
entityManagerFactoryRef="entityManagerFactoryPrimary",
|
||||
transactionManagerRef="transactionManagerPrimary",
|
||||
basePackages= { "com.neo.repository.test1" })//设置dao(repo)所在位置
|
||||
public class PrimaryConfig {
|
||||
|
||||
@Autowired
|
||||
private JpaProperties jpaProperties;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("primaryDataSource")
|
||||
private DataSource primaryDataSource;
|
||||
|
||||
@Bean(name = "entityManagerPrimary")
|
||||
@Primary
|
||||
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
|
||||
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
|
||||
}
|
||||
|
||||
@Bean(name = "entityManagerFactoryPrimary")
|
||||
@Primary
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
|
||||
return builder
|
||||
.dataSource(primaryDataSource)
|
||||
.properties(getVendorProperties(primaryDataSource))
|
||||
.packages("com.neo.domain") //设置实体类所在位置
|
||||
.persistenceUnit("primaryPersistenceUnit")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Map<String, String> getVendorProperties(DataSource dataSource) {
|
||||
return jpaProperties.getHibernateProperties(dataSource);
|
||||
}
|
||||
|
||||
@Bean(name = "transactionManagerPrimary")
|
||||
@Primary
|
||||
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
|
||||
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.neo.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(
|
||||
entityManagerFactoryRef="entityManagerFactorySecondary",
|
||||
transactionManagerRef="transactionManagerSecondary",
|
||||
basePackages= { "com.neo.repository.test2" })
|
||||
public class SecondaryConfig {
|
||||
@Autowired
|
||||
private JpaProperties jpaProperties;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("secondaryDataSource")
|
||||
private DataSource secondaryDataSource;
|
||||
|
||||
@Bean(name = "entityManagerSecondary")
|
||||
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
|
||||
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
|
||||
}
|
||||
|
||||
@Bean(name = "entityManagerFactorySecondary")
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
|
||||
return builder
|
||||
.dataSource(secondaryDataSource)
|
||||
.properties(getVendorProperties(secondaryDataSource))
|
||||
.packages("com.neo.domain")
|
||||
.persistenceUnit("secondaryPersistenceUnit")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Map<String, String> getVendorProperties(DataSource dataSource) {
|
||||
return jpaProperties.getHibernateProperties(dataSource);
|
||||
}
|
||||
|
||||
@Bean(name = "transactionManagerSecondary")
|
||||
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
|
||||
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.neo.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class HelloController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String index() {
|
||||
return "Hello Spring Boot 2.0!";
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package com.neo.domain;
|
||||
|
||||
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Entity
|
||||
public class User implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
@Column(nullable = false, unique = true)
|
||||
private String userName;
|
||||
@Column(nullable = false)
|
||||
private String passWord;
|
||||
@Column(nullable = false, unique = true)
|
||||
private String email;
|
||||
@Column(nullable = true, unique = true)
|
||||
private String nickName;
|
||||
@Column(nullable = false)
|
||||
private String regTime;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String userName, String passWord, String email, String nickName, String regTime) {
|
||||
this.userName = userName;
|
||||
this.passWord = passWord;
|
||||
this.email = email;
|
||||
this.nickName = nickName;
|
||||
this.regTime = regTime;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getPassWord() {
|
||||
return passWord;
|
||||
}
|
||||
|
||||
public void setPassWord(String passWord) {
|
||||
this.passWord = passWord;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getNickName() {
|
||||
return nickName;
|
||||
}
|
||||
|
||||
public void setNickName(String nickName) {
|
||||
this.nickName = nickName;
|
||||
}
|
||||
|
||||
public String getRegTime() {
|
||||
return regTime;
|
||||
}
|
||||
|
||||
public void setRegTime(String regTime) {
|
||||
this.regTime = regTime;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.neo.repository.test1;
|
||||
|
||||
import com.neo.domain.User;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
public interface UserTest1Repository extends JpaRepository<User, Long> {
|
||||
User findByUserName(String userName);
|
||||
User findByUserNameOrEmail(String username, String email);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.neo.repository.test2;
|
||||
|
||||
import com.neo.domain.User;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public interface UserTest2Repository extends JpaRepository<User, Long> {
|
||||
User findByUserName(String userName);
|
||||
User findByUserNameOrEmail(String username, String email);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
#primary
|
||||
spring.primary.datasource.url=jdbc:mysql://localhost:3306/test1
|
||||
spring.primary.datasource.username=root
|
||||
spring.primary.datasource.password=root
|
||||
spring.primary.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
#secondary
|
||||
spring.secondary.datasource.url=jdbc:mysql://localhost:3306/test2
|
||||
spring.secondary.datasource.username=root
|
||||
spring.secondary.datasource.password=root
|
||||
spring.secondary.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
#multiple Setting
|
||||
|
||||
spring.jpa.properties.hibernate.hbm2ddl.auto=update
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
|
||||
spring.jpa.show-sql= true
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.neo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class HelloApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
System.out.println("Hello Spring Boot 2.0!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.neo.repository;
|
||||
|
||||
import com.neo.domain.User;
|
||||
import com.neo.repository.test1.UserTest1Repository;
|
||||
import com.neo.repository.test2.UserTest2Repository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.DateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class UserRepositoryTests {
|
||||
@Resource
|
||||
private UserTest1Repository userTest1Repository;
|
||||
@Resource
|
||||
private UserTest2Repository userTest2Repository;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void testSave() throws Exception {
|
||||
Date date = new Date();
|
||||
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
|
||||
String formattedDate = dateFormat.format(date);
|
||||
|
||||
userTest1Repository.save(new User("aa", "aa123456","aa@126.com", "aa", formattedDate));
|
||||
userTest1Repository.save(new User("bb", "bb123456","bb@126.com", "bb", formattedDate));
|
||||
userTest2Repository.save(new User("cc", "cc123456","cc@126.com", "cc", formattedDate));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBaseQuery() {
|
||||
Date date = new Date();
|
||||
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
|
||||
String formattedDate = dateFormat.format(date);
|
||||
User user=new User("ff", "ff123456","ff@126.com", "ff", formattedDate);
|
||||
userTest1Repository.findAll();
|
||||
userTest2Repository.findOne(3l);
|
||||
userTest2Repository.save(user);
|
||||
user.setId(2l);
|
||||
userTest1Repository.delete(user);
|
||||
userTest1Repository.count();
|
||||
userTest2Repository.exists(3l);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
#primary
|
||||
spring.primary.datasource.url=jdbc:mysql://localhost:3306/test1
|
||||
spring.primary.datasource.username=root
|
||||
spring.primary.datasource.password=root
|
||||
spring.primary.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
#secondary
|
||||
spring.secondary.datasource.url=jdbc:mysql://localhost:3306/test2
|
||||
spring.secondary.datasource.username=root
|
||||
spring.secondary.datasource.password=root
|
||||
spring.secondary.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
#multiple Setting
|
||||
|
||||
spring.jpa.properties.hibernate.hbm2ddl.auto=update
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
|
||||
spring.jpa.show-sql= true
|
||||
@@ -1,8 +0,0 @@
|
||||
/neo/hello/spring-boot-atomikos/src/main/java/com/neo/config/SecondaryConfig.java
|
||||
/neo/hello/spring-boot-atomikos/src/main/java/com/neo/JtaAtomikosApplication.java
|
||||
/neo/hello/spring-boot-atomikos/src/main/java/com/neo/model/User.java
|
||||
/neo/hello/spring-boot-atomikos/src/main/java/com/neo/repository/test1/UserTest1Repository.java
|
||||
/neo/hello/spring-boot-atomikos/src/main/java/com/neo/config/PrimaryConfig.java
|
||||
/neo/hello/spring-boot-atomikos/src/main/java/com/neo/controller/HelloController.java
|
||||
/neo/hello/spring-boot-atomikos/src/main/java/com/neo/config/DataSourceConfig.java
|
||||
/neo/hello/spring-boot-atomikos/src/main/java/com/neo/repository/test2/UserTest2Repository.java
|
||||
@@ -1,51 +0,0 @@
|
||||
<?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.neo</groupId>
|
||||
<artifactId>spring-boot-elasticsearch</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-boot-elasticsearch</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.neo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ElasticsearchApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ElasticsearchApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
|
||||
package com.neo.model;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
|
||||
@Document(indexName = "customer", type = "customer", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String userName;
|
||||
|
||||
private String address;
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
public Customer(String userName, String address) {
|
||||
this.userName = userName;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Customer[id=%s, userName='%s', address='%s']", this.id,
|
||||
this.userName, this.address);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
|
||||
package com.neo.repository;
|
||||
|
||||
import com.neo.model.Customer;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {
|
||||
|
||||
public Customer findByUserName(String userName);
|
||||
|
||||
public int deleteByUserName(String userName);
|
||||
|
||||
public List<Customer> findByAddress(String address);
|
||||
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
spring.data.elasticsearch.cluster-nodes=localhost:9300
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.neo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ElasticsearchApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
System.out.println("Spring Boot Test");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.neo.repository;
|
||||
|
||||
|
||||
import com.neo.model.Customer;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class CustomerRepositoryTest {
|
||||
@Autowired
|
||||
private CustomerRepository repository;
|
||||
|
||||
@Test
|
||||
public void saveCustomers() {
|
||||
repository.save(new Customer("Alice", "北京"));
|
||||
repository.save(new Customer("Bob", "北京"));
|
||||
repository.save(new Customer("neo", "西安"));
|
||||
repository.save(new Customer("summer", "烟台"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteCustomers() {
|
||||
// repository.deleteAll();
|
||||
repository.deleteByUserName("neo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateCustomers() {
|
||||
Customer customer= repository.findByUserName("summer");
|
||||
System.out.println(customer);
|
||||
customer.setAddress("北京市海淀区西直门");
|
||||
repository.save(customer);
|
||||
Customer xcustomer=repository.findByUserName("summer");
|
||||
System.out.println(xcustomer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchAllCustomers() {
|
||||
System.out.println("Customers found with findAll():");
|
||||
System.out.println("-------------------------------");
|
||||
for (Customer customer : repository.findAll()) {
|
||||
System.out.println(customer);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchIndividualCustomers() {
|
||||
System.out.println("Customer found with findByUserName('summer'):");
|
||||
System.out.println("--------------------------------");
|
||||
System.out.println(repository.findByUserName("summer"));
|
||||
|
||||
System.out.println("Customers found with findByAddress(\"北京\"):");
|
||||
System.out.println("--------------------------------");
|
||||
for (Customer customer : repository.findByAddress("北京")) {
|
||||
System.out.println(customer);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user