-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Spring Boot 3.0 MongoDB axamples
- Loading branch information
Showing
21 changed files
with
597 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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-mongodb</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-mongodb</name> | ||
<description>Demo project for Spring Boot and mongodb</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.0.0</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>17</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<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> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/MongoDBApplication.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.neo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class MongoDBApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MongoDBApplication.class, args); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/model/User.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.neo.model; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Created by summer on 2017/5/5. | ||
*/ | ||
public class User implements Serializable { | ||
private static final long serialVersionUID = -3258839839160856613L; | ||
private Long id; | ||
private String userName; | ||
private String passWord; | ||
|
||
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; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "UserEntity{" + | ||
"id=" + id + | ||
", userName='" + userName + '\'' + | ||
", passWord='" + passWord + '\'' + | ||
'}'; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/repository/UserRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.neo.repository; | ||
|
||
import com.neo.model.User; | ||
|
||
/** | ||
* Created by summer on 2017/5/5. | ||
*/ | ||
public interface UserRepository { | ||
|
||
public void saveUser(User user); | ||
|
||
public User findUserByUserName(String userName); | ||
|
||
public long updateUser(User user); | ||
|
||
public void deleteUserById(Long id); | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...mongodb/spring-boot-mongodb/src/main/java/com/neo/repository/impl/UserRepositoryImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.neo.repository.impl; | ||
|
||
import com.mongodb.client.result.UpdateResult; | ||
import com.neo.repository.UserRepository; | ||
import com.neo.model.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.data.mongodb.core.query.Update; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Created by summer on 2017/5/5. | ||
*/ | ||
@Component | ||
public class UserRepositoryImpl implements UserRepository { | ||
|
||
@Autowired | ||
private MongoTemplate mongoTemplate; | ||
|
||
/** | ||
* 创建对象 | ||
* @param user | ||
*/ | ||
@Override | ||
public void saveUser(User user) { | ||
mongoTemplate.save(user); | ||
} | ||
|
||
/** | ||
* 根据用户名查询对象 | ||
* @param userName | ||
* @return | ||
*/ | ||
@Override | ||
public User findUserByUserName(String userName) { | ||
Query query=new Query(Criteria.where("userName").is(userName)); | ||
User user = mongoTemplate.findOne(query , User.class); | ||
return user; | ||
} | ||
|
||
/** | ||
* 更新对象 | ||
* @param user | ||
*/ | ||
@Override | ||
public long updateUser(User user) { | ||
Query query=new Query(Criteria.where("id").is(user.getId())); | ||
Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord()); | ||
//更新查询返回结果集的第一条 | ||
UpdateResult result =mongoTemplate.updateFirst(query,update,User.class); | ||
//更新查询返回结果集的所有 | ||
// mongoTemplate.updateMulti(query,update,UserEntity.class); | ||
if(result!=null) | ||
return result.getMatchedCount(); | ||
else | ||
return 0; | ||
} | ||
|
||
/** | ||
* 删除对象 | ||
* @param id | ||
*/ | ||
@Override | ||
public void deleteUserById(Long id) { | ||
Query query=new Query(Criteria.where("id").is(id)); | ||
mongoTemplate.remove(query,User.class); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
spring-boot-mongodb/spring-boot-mongodb/src/main/resources/application.properties
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
spring.application.name=spring-boot-mongodb | ||
|
||
spring.data.mongodb.uri=mongodb://119.0.0.6:27017/test | ||
|
||
|
17 changes: 17 additions & 0 deletions
17
spring-boot-mongodb/spring-boot-mongodb/src/test/java/com/neo/MongoDBApplicationTests.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class MongoDBApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
System.out.println("hello world"); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...boot-mongodb/spring-boot-mongodb/src/test/java/com/neo/repository/UserRepositoryTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.neo.repository; | ||
|
||
import com.neo.model.User; | ||
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; | ||
|
||
/** | ||
* Created by summer on 2017/5/5. | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class UserRepositoryTest { | ||
|
||
@Autowired | ||
private UserRepository userDao; | ||
|
||
@Test | ||
public void testSaveUser() throws Exception { | ||
User user=new User(); | ||
user.setId(2l); | ||
user.setUserName("小明"); | ||
user.setPassWord("fffooo123"); | ||
userDao.saveUser(user); | ||
} | ||
|
||
@Test | ||
public void findUserByUserName(){ | ||
User user= userDao.findUserByUserName("小明"); | ||
System.out.println("user is "+user); | ||
} | ||
|
||
@Test | ||
public void updateUser(){ | ||
User user=new User(); | ||
user.setId(2l); | ||
user.setUserName("天空"); | ||
user.setPassWord("fffxxxx"); | ||
userDao.updateUser(user); | ||
} | ||
|
||
@Test | ||
public void deleteUserById(){ | ||
userDao.deleteUserById(1l); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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-multi-mongodb</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-multi-mongodb</name> | ||
<description>Demo project for Spring Boot and multi mongodb</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.0.0</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>17</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<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> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
...boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/MultiMongodbApplication.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.neo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class MultiMongodbApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MultiMongodbApplication.class, args); | ||
} | ||
} |
Oops, something went wrong.