Skip to content

Commit d57263c

Browse files
committed
添加代码示例springboot-mongodb
1 parent befc5d9 commit d57263c

File tree

13 files changed

+521
-0
lines changed

13 files changed

+521
-0
lines changed

springboot-mongodb/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.idea/
2+
target/
3+
*.iml
4+
*.ipr
5+
*.iws
6+
*.log
7+
.svn/
8+
.project
9+
rebel.xml
10+
.rebel-remote.xml.*

springboot-mongodb/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Xiong Neng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

springboot-mongodb/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## 集成MongoDB
2+
3+
SpringBoot集成MongoDB访问NoSQL数据库
4+
5+
## 安装MongoDB数据库
6+
7+
数据库的安装教程网上非常多,参考 [官网安装](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/)
8+
9+
配置数据库的账号和密码。
10+
11+
## 修改application.yml
12+
13+
修改配置文件,主要是MongoDB的连接信息
14+
15+
## 运行测试用例
16+
17+
执行对用户表增/删/改/查的测试用例:`com.xncoding.pos.ApplicationTests.java`
18+
19+
20+
## 许可证
21+
22+
Copyright (c) 2018 Xiong Neng
23+
24+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>

springboot-mongodb/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xncoding</groupId>
8+
<artifactId>springboot-mongodb</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-mongodb</name>
13+
<description>集成MongoDB</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>1.5.9.RELEASE</version>
19+
<relativePath/>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.hamcrest</groupId>
40+
<artifactId>hamcrest-all</artifactId>
41+
<version>1.3</version>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>3.6.1</version>
52+
<configuration>
53+
<!--<proc>none</proc>-->
54+
<source>1.8</source>
55+
<target>1.8</target>
56+
</configuration>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-surefire-plugin</artifactId>
61+
<version>2.20</version>
62+
<configuration>
63+
<skip>true</skip>
64+
</configuration>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
<executions>
70+
</executions>
71+
</plugin>
72+
</plugins>
73+
74+
<resources>
75+
<resource>
76+
<directory>src/main/resources</directory>
77+
</resource>
78+
<resource>
79+
<directory>src/main/java</directory>
80+
<includes>
81+
<include>**/*.xml</include>
82+
</includes>
83+
</resource>
84+
</resources>
85+
</build>
86+
87+
</project>

springboot-mongodb/run.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# 项目自动更新脚本
3+
# 先clone相应的分支下来:
4+
# git clone ssh://[email protected]:7999/xxx.git
5+
# 远程调试启动:
6+
# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
7+
8+
function start {
9+
profile="$1"
10+
echo "启动环境profile=${profile}"
11+
jarfile=$(ls target/*.jar)
12+
if [[ "$?" == "0" ]]; then
13+
stop $profile $jarfile
14+
fi
15+
branch=$(git branch |awk '{print $2}')
16+
git pull origin ${branch}
17+
echo "更新完代码开始重新打包"
18+
mvn clean && mvn clean && mvn package -DskipTests=true
19+
if [[ "$?" != "0" ]]; then
20+
echo "编译出错,退出!"
21+
exit 1
22+
fi
23+
echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &"
24+
nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
25+
echo "启动应用中,请查看日志文件..."
26+
}
27+
28+
function stop {
29+
profile="$1"
30+
jarfile="$2"
31+
ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null
32+
if [[ "$?" == "0" ]]; then
33+
echo "该应用还在跑,我先停了它"
34+
pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}')
35+
if [[ "$pid" != "" ]]; then
36+
kill -9 $pid
37+
fi
38+
echo "停止应用成功..."
39+
fi
40+
}
41+
42+
if [[ "$1" == "start" ]]; then
43+
if [[ "$#" < 2 ]]; then
44+
echo "请输入正确参数:./epay.sh start {profile}"
45+
exit 1
46+
fi
47+
profile="$2"
48+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
49+
echo "参数错误,请输入正确的profile参数,使用方法:"
50+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
51+
exit 1
52+
fi
53+
start "${profile}"
54+
elif [[ "$1" == "stop" ]]; then
55+
if [[ "$#" < 2 ]]; then
56+
echo "请输入正确参数:./epay.sh stop {profile}"
57+
exit 1
58+
fi
59+
profile="$2"
60+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
61+
echo "参数错误,请输入正确的profile参数,使用方法:"
62+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
63+
exit 1
64+
fi
65+
jarfile=$(ls target/*.jar)
66+
stop $profile $jarfile
67+
else
68+
echo "参数错误,使用方法:{}参数是必填的,[]参数可选"
69+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
70+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
71+
exit 1
72+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xncoding.pos;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
12+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.xncoding.pos.dao.entity;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.mapping.Document;
5+
6+
/**
7+
* Customer
8+
*
9+
* @author XiongNeng
10+
* @version 1.0
11+
* @since 2018/3/3
12+
*/
13+
@Document(collection = "customer")
14+
public class Customer {
15+
@Id
16+
private String id;
17+
private String firstName;
18+
private String lastName;
19+
20+
public Customer() {
21+
}
22+
23+
public Customer(String firstName, String lastName) {
24+
this.firstName = firstName;
25+
this.lastName = lastName;
26+
}
27+
28+
public String getId() {
29+
return id;
30+
}
31+
32+
public void setId(String id) {
33+
this.id = id;
34+
}
35+
36+
public String getFirstName() {
37+
return firstName;
38+
}
39+
40+
public void setFirstName(String firstName) {
41+
this.firstName = firstName;
42+
}
43+
44+
public String getLastName() {
45+
return lastName;
46+
}
47+
48+
public void setLastName(String lastName) {
49+
this.lastName = lastName;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return String.format(
55+
"Customer[id=%s, firstName='%s', lastName='%s']",
56+
id, firstName, lastName);
57+
}
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.xncoding.pos.dao.repository;
2+
3+
import com.xncoding.pos.dao.entity.Customer;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
6+
import java.util.List;
7+
8+
/**
9+
* 客户数据访问服务
10+
*
11+
* @author XiongNeng
12+
* @version 1.0
13+
* @since 2018/3/3
14+
*/
15+
public interface CustomerRepository extends MongoRepository<Customer, String> {
16+
17+
Customer findByFirstName(String firstName);
18+
19+
List<Customer> findByLastName(String lastName);
20+
21+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.xncoding.pos.service;
2+
3+
import com.xncoding.pos.dao.entity.Customer;
4+
import com.xncoding.pos.dao.repository.CustomerRepository;
5+
import org.springframework.stereotype.Service;
6+
7+
import javax.annotation.Resource;
8+
import java.util.List;
9+
10+
/**
11+
* CustomerService
12+
*
13+
* @author XiongNeng
14+
* @version 1.0
15+
* @since 2018/3/3
16+
*/
17+
@Service
18+
public class CustomerService {
19+
@Resource
20+
private CustomerRepository repository;
21+
22+
/**
23+
* 删除所有的客户
24+
*/
25+
public void deleteAll() {
26+
repository.deleteAll();
27+
}
28+
29+
/**
30+
* 保存客户
31+
* @param customer 客户
32+
*/
33+
public void save(Customer customer) {
34+
repository.save(customer);
35+
}
36+
37+
/**
38+
* 查询所有客户列表
39+
* @return 客户列表
40+
*/
41+
public Iterable<Customer> findAll() {
42+
return repository.findAll();
43+
}
44+
45+
/**
46+
* 通过名查找某个客户
47+
* @param firstName
48+
* @return
49+
*/
50+
public Customer findByFirstName(String firstName) {
51+
return repository.findByFirstName(firstName);
52+
}
53+
54+
/**
55+
* 通过姓查找客户列表
56+
* @param lastName
57+
* @return
58+
*/
59+
public List<Customer> findByLastName(String lastName) {
60+
return repository.findByLastName(lastName);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)