Skip to content

Commit 0e44f5b

Browse files
committed
add mongodb
1 parent 800465e commit 0e44f5b

File tree

8 files changed

+279
-0
lines changed

8 files changed

+279
-0
lines changed

spring-boot-mongodb/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.neo</groupId>
7+
<artifactId>spring-boot-mongodb</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-mongodb</name>
12+
<description>Demo project for Spring Boot and mongodb</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-devtools</artifactId>
39+
<optional>true</optional>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
<configuration>
53+
<fork>true</fork>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
60+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.neo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
@SpringBootApplication
8+
public class Application {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.neo.dao;
2+
3+
import com.neo.entity.UserEntity;
4+
5+
/**
6+
* Created by summer on 2017/5/5.
7+
*/
8+
public interface UserDao {
9+
10+
public void saveUser(UserEntity user);
11+
12+
public UserEntity findUserByUserName(String userName);
13+
14+
public int updateUser(UserEntity user);
15+
16+
public void deleteUserById(Long id);
17+
18+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.neo.dao.impl;
2+
3+
import com.mongodb.WriteResult;
4+
import com.neo.dao.UserDao;
5+
import com.neo.entity.UserEntity;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.autoconfigure.security.SecurityProperties;
8+
import org.springframework.data.mongodb.core.MongoTemplate;
9+
import org.springframework.data.mongodb.core.query.Criteria;
10+
import org.springframework.data.mongodb.core.query.Query;
11+
import org.springframework.data.mongodb.core.query.Update;
12+
import org.springframework.stereotype.Component;
13+
14+
/**
15+
* Created by summer on 2017/5/5.
16+
*/
17+
@Component
18+
public class UserDaoImpl implements UserDao {
19+
20+
@Autowired
21+
private MongoTemplate mongoTemplate;
22+
23+
/**
24+
* 创建对象
25+
* @param user
26+
*/
27+
@Override
28+
public void saveUser(UserEntity user) {
29+
mongoTemplate.save(user);
30+
}
31+
32+
/**
33+
* 根据用户名查询对象
34+
* @param userName
35+
* @return
36+
*/
37+
@Override
38+
public UserEntity findUserByUserName(String userName) {
39+
Query query=new Query(Criteria.where("userName").is(userName));
40+
UserEntity user = mongoTemplate.findOne(query , UserEntity.class);
41+
return user;
42+
}
43+
44+
/**
45+
* 更新对象
46+
* @param user
47+
*/
48+
@Override
49+
public int updateUser(UserEntity user) {
50+
Query query=new Query(Criteria.where("id").is(user.getId()));
51+
Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
52+
//更新查询返回结果集的第一条
53+
WriteResult result =mongoTemplate.updateFirst(query,update,UserEntity.class);
54+
//更新查询返回结果集的所有
55+
// mongoTemplate.updateMulti(query,update,UserEntity.class);
56+
if(result!=null)
57+
return result.getN();
58+
else
59+
return 0;
60+
}
61+
62+
/**
63+
* 删除对象
64+
* @param id
65+
*/
66+
@Override
67+
public void deleteUserById(Long id) {
68+
Query query=new Query(Criteria.where("id").is(id));
69+
mongoTemplate.remove(query,UserEntity.class);
70+
}
71+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.neo.entity;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by summer on 2017/5/5.
7+
*/
8+
public class UserEntity implements Serializable {
9+
private static final long serialVersionUID = -3258839839160856613L;
10+
private Long id;
11+
private String userName;
12+
private String passWord;
13+
14+
public Long getId() {
15+
return id;
16+
}
17+
18+
public void setId(Long id) {
19+
this.id = id;
20+
}
21+
22+
public String getUserName() {
23+
return userName;
24+
}
25+
26+
public void setUserName(String userName) {
27+
this.userName = userName;
28+
}
29+
30+
public String getPassWord() {
31+
return passWord;
32+
}
33+
34+
public void setPassWord(String passWord) {
35+
this.passWord = passWord;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "UserEntity{" +
41+
"id=" + id +
42+
", userName='" + userName + '\'' +
43+
", passWord='" + passWord + '\'' +
44+
'}';
45+
}
46+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring.application.name=spirng-boot-mongodb
2+
3+
spring.data.mongodb.uri=mongodb://192.168.9.61:20000/test
4+
5+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.neo;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class ApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
System.out.println("hello world");
15+
}
16+
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.neo.dao;
2+
3+
import com.neo.entity.UserEntity;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
/**
11+
* Created by summer on 2017/5/5.
12+
*/
13+
@RunWith(SpringRunner.class)
14+
@SpringBootTest
15+
public class UserDaoTest {
16+
17+
@Autowired
18+
private UserDao userDao;
19+
20+
@Test
21+
public void testSaveUser() throws Exception {
22+
UserEntity user=new UserEntity();
23+
user.setId(2l);
24+
user.setUserName("小明");
25+
user.setPassWord("fffooo123");
26+
userDao.saveUser(user);
27+
}
28+
29+
@Test
30+
public void findUserByUserName(){
31+
UserEntity user= userDao.findUserByUserName("小明");
32+
System.out.println("user is "+user);
33+
}
34+
35+
@Test
36+
public void updateUser(){
37+
UserEntity user=new UserEntity();
38+
user.setId(2l);
39+
user.setUserName("天空");
40+
user.setPassWord("fffxxxx");
41+
userDao.updateUser(user);
42+
}
43+
44+
@Test
45+
public void deleteUserById(){
46+
userDao.deleteUserById(1l);
47+
}
48+
49+
}

0 commit comments

Comments
 (0)