diff --git a/springboot-mongodb/.gitignore b/springboot-mongodb/.gitignore new file mode 100644 index 0000000..9d8e7cc --- /dev/null +++ b/springboot-mongodb/.gitignore @@ -0,0 +1,10 @@ +.idea/ +target/ +*.iml +*.ipr +*.iws +*.log +.svn/ +.project +rebel.xml +.rebel-remote.xml.* diff --git a/springboot-mongodb/LICENSE b/springboot-mongodb/LICENSE new file mode 100644 index 0000000..83cd47d --- /dev/null +++ b/springboot-mongodb/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2018 Xiong Neng + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/springboot-mongodb/README.md b/springboot-mongodb/README.md new file mode 100644 index 0000000..e00789a --- /dev/null +++ b/springboot-mongodb/README.md @@ -0,0 +1,24 @@ +## 集成MongoDB + +SpringBoot集成MongoDB访问NoSQL数据库 + +## 安装MongoDB数据库 + +数据库的安装教程网上非常多,参考 [官网安装](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/) + +配置数据库的账号和密码。 + +## 修改application.yml + +修改配置文件,主要是MongoDB的连接信息 + +## 运行测试用例 + +执行对用户表增/删/改/查的测试用例:`com.xncoding.pos.ApplicationTests.java` + + +## 许可证 + +Copyright (c) 2018 Xiong Neng + +基于 MIT 协议发布: diff --git a/springboot-mongodb/pom.xml b/springboot-mongodb/pom.xml new file mode 100644 index 0000000..07686dd --- /dev/null +++ b/springboot-mongodb/pom.xml @@ -0,0 +1,87 @@ + + + 4.0.0 + + com.xncoding + springboot-mongodb + 1.0.0-SNAPSHOT + jar + + springboot-mongodb + 集成MongoDB + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + org.springframework.boot + spring-boot-starter-test + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.20 + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + + src/main/resources + + + src/main/java + + **/*.xml + + + + + + \ No newline at end of file diff --git a/springboot-mongodb/run.sh b/springboot-mongodb/run.sh new file mode 100644 index 0000000..955efb1 --- /dev/null +++ b/springboot-mongodb/run.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# 项目自动更新脚本 +# 先clone相应的分支下来: +# git clone ssh://git@120.24.173.142:7999/xxx.git +# 远程调试启动: +# 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 & + +function start { + profile="$1" + echo "启动环境profile=${profile}" + jarfile=$(ls target/*.jar) + if [[ "$?" == "0" ]]; then + stop $profile $jarfile + fi + branch=$(git branch |awk '{print $2}') + git pull origin ${branch} + echo "更新完代码开始重新打包" + mvn clean && mvn clean && mvn package -DskipTests=true + if [[ "$?" != "0" ]]; then + echo "编译出错,退出!" + exit 1 + fi + echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &" + nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 & + echo "启动应用中,请查看日志文件..." +} + +function stop { + profile="$1" + jarfile="$2" + ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null + if [[ "$?" == "0" ]]; then + echo "该应用还在跑,我先停了它" + pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}') + if [[ "$pid" != "" ]]; then + kill -9 $pid + fi + echo "停止应用成功..." + fi +} + +if [[ "$1" == "start" ]]; then + if [[ "$#" < 2 ]]; then + echo "请输入正确参数:./epay.sh start {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "参数错误,请输入正确的profile参数,使用方法:" + echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production" + exit 1 + fi + start "${profile}" +elif [[ "$1" == "stop" ]]; then + if [[ "$#" < 2 ]]; then + echo "请输入正确参数:./epay.sh stop {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "参数错误,请输入正确的profile参数,使用方法:" + echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production" + exit 1 + fi + jarfile=$(ls target/*.jar) + stop $profile $jarfile +else + echo "参数错误,使用方法:{}参数是必填的,[]参数可选" + echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production" + echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production" + exit 1 +fi diff --git a/springboot-mongodb/src/main/java/com/xncoding/pos/Application.java b/springboot-mongodb/src/main/java/com/xncoding/pos/Application.java new file mode 100644 index 0000000..adc4c48 --- /dev/null +++ b/springboot-mongodb/src/main/java/com/xncoding/pos/Application.java @@ -0,0 +1,12 @@ +package com.xncoding.pos; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/springboot-mongodb/src/main/java/com/xncoding/pos/dao/entity/Customer.java b/springboot-mongodb/src/main/java/com/xncoding/pos/dao/entity/Customer.java new file mode 100644 index 0000000..f09cc50 --- /dev/null +++ b/springboot-mongodb/src/main/java/com/xncoding/pos/dao/entity/Customer.java @@ -0,0 +1,58 @@ +package com.xncoding.pos.dao.entity; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +/** + * Customer + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/3 + */ +@Document(collection = "customer") +public class Customer { + @Id + private String id; + private String firstName; + private String lastName; + + public Customer() { + } + + public Customer(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @Override + public String toString() { + return String.format( + "Customer[id=%s, firstName='%s', lastName='%s']", + id, firstName, lastName); + } +} diff --git a/springboot-mongodb/src/main/java/com/xncoding/pos/dao/repository/CustomerRepository.java b/springboot-mongodb/src/main/java/com/xncoding/pos/dao/repository/CustomerRepository.java new file mode 100644 index 0000000..54fe4f7 --- /dev/null +++ b/springboot-mongodb/src/main/java/com/xncoding/pos/dao/repository/CustomerRepository.java @@ -0,0 +1,21 @@ +package com.xncoding.pos.dao.repository; + +import com.xncoding.pos.dao.entity.Customer; +import org.springframework.data.mongodb.repository.MongoRepository; + +import java.util.List; + +/** + * 客户数据访问服务 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/3 + */ +public interface CustomerRepository extends MongoRepository { + + Customer findByFirstName(String firstName); + + List findByLastName(String lastName); + +} diff --git a/springboot-mongodb/src/main/java/com/xncoding/pos/service/CustomerService.java b/springboot-mongodb/src/main/java/com/xncoding/pos/service/CustomerService.java new file mode 100644 index 0000000..befcb34 --- /dev/null +++ b/springboot-mongodb/src/main/java/com/xncoding/pos/service/CustomerService.java @@ -0,0 +1,63 @@ +package com.xncoding.pos.service; + +import com.xncoding.pos.dao.entity.Customer; +import com.xncoding.pos.dao.repository.CustomerRepository; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * CustomerService + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/3 + */ +@Service +public class CustomerService { + @Resource + private CustomerRepository repository; + + /** + * 删除所有的客户 + */ + public void deleteAll() { + repository.deleteAll(); + } + + /** + * 保存客户 + * @param customer 客户 + */ + public void save(Customer customer) { + repository.save(customer); + } + + /** + * 查询所有客户列表 + * @return 客户列表 + */ + public Iterable findAll() { + return repository.findAll(); + } + + /** + * 通过名查找某个客户 + * @param firstName + * @return + */ + public Customer findByFirstName(String firstName) { + return repository.findByFirstName(firstName); + } + + /** + * 通过姓查找客户列表 + * @param lastName + * @return + */ + public List findByLastName(String lastName) { + return repository.findByLastName(lastName); + } + +} diff --git a/springboot-mongodb/src/main/resources/application.yml b/springboot-mongodb/src/main/resources/application.yml new file mode 100644 index 0000000..f135207 --- /dev/null +++ b/springboot-mongodb/src/main/resources/application.yml @@ -0,0 +1,45 @@ +########################################################## +################## 所有profile共有的配置 ################# +########################################################## + +################### spring配置 ################### +spring: + profiles: + active: dev + +--- + +##################################################################### +######################## 开发环境profile ########################## +##################################################################### +spring: + profiles: dev + data: + mongodb: + uri: mongodb://xiongneng:123456@localhost:27017/test + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: E:/logs/app.log + +--- + +##################################################################### +######################## 测试环境profile ########################## +##################################################################### + +spring: + profiles: test + data: + mongodb: + uri: mongodb://xiongneng:123456@localhost:27017/test + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: /var/logs/app.log diff --git a/springboot-mongodb/src/main/resources/banner.txt b/springboot-mongodb/src/main/resources/banner.txt new file mode 100644 index 0000000..859b78f --- /dev/null +++ b/springboot-mongodb/src/main/resources/banner.txt @@ -0,0 +1,23 @@ + + _____ _______ _____ _____ + /\ \ /::\ \ /\ \ /\ \ + /::\____\ /::::\ \ /::\____\ /::\ \ + /:::/ / /::::::\ \ /:::/ / /::::\ \ + /:::/ / /::::::::\ \ /:::/ / /::::::\ \ + /:::/ / /:::/~~\:::\ \ /:::/ / /:::/\:::\ \ + /:::/ / /:::/ \:::\ \ /:::/____/ /:::/__\:::\ \ + /:::/ / /:::/ / \:::\ \ |::| | /::::\ \:::\ \ + /:::/ / /:::/____/ \:::\____\ |::| | _____ /::::::\ \:::\ \ + /:::/ / |:::| | |:::| | |::| | /\ \ /:::/\:::\ \:::\ \ +/:::/____/ |:::|____| |:::| | |::| | /::\____\/:::/__\:::\ \:::\____\ +\:::\ \ \:::\ \ /:::/ / |::| | /:::/ /\:::\ \:::\ \::/ / + \:::\ \ \:::\ \ /:::/ / |::| | /:::/ / \:::\ \:::\ \/____/ + \:::\ \ \:::\ /:::/ / |::|____|/:::/ / \:::\ \:::\ \ + \:::\ \ \:::\__/:::/ / |:::::::::::/ / \:::\ \:::\____\ + \:::\ \ \::::::::/ / \::::::::::/____/ \:::\ \::/ / + \:::\ \ \::::::/ / ~~~~~~~~~~ \:::\ \/____/ + \:::\ \ \::::/ / \:::\ \ + \:::\____\ \::/____/ \:::\____\ + \::/ / ~~ \::/ / + \/____/ \/____/ + diff --git a/springboot-mongodb/src/main/resources/sql/schema.sql b/springboot-mongodb/src/main/resources/sql/schema.sql new file mode 100644 index 0000000..d05f38b --- /dev/null +++ b/springboot-mongodb/src/main/resources/sql/schema.sql @@ -0,0 +1,18 @@ +-- Dumping database structure for concretepage +CREATE DATABASE IF NOT EXISTS `pos` default charset utf8 COLLATE utf8_general_ci; +SET FOREIGN_KEY_CHECKS=0; +USE `pos`; +-- Dumping structure for table concretepage.articles +CREATE TABLE IF NOT EXISTS `articles` ( + `article_id` int(5) NOT NULL AUTO_INCREMENT, + `title` varchar(200) NOT NULL, + `category` varchar(100) NOT NULL, + PRIMARY KEY (`article_id`) +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='文章表'; + +-- Dumping data for table concretepage.articles: ~3 rows (approximately) +INSERT INTO `articles` (`article_id`, `title`, `category`) VALUES + (1, 'Java Concurrency', 'Java'), + (2, 'Hibernate HQL ', 'Hibernate'), + (3, 'Spring MVC with Hibernate', 'Spring'); + diff --git a/springboot-mongodb/src/test/java/com/xncoding/pos/ApplicationTests.java b/springboot-mongodb/src/test/java/com/xncoding/pos/ApplicationTests.java new file mode 100644 index 0000000..dea1638 --- /dev/null +++ b/springboot-mongodb/src/test/java/com/xncoding/pos/ApplicationTests.java @@ -0,0 +1,68 @@ +package com.xncoding.pos; + +import com.xncoding.pos.dao.entity.Customer; +import com.xncoding.pos.service.CustomerService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.stereotype.Service; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +/** + * 测试 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApplicationTests { + private static final Logger log = LoggerFactory.getLogger(ApplicationTests.class); + + @Resource + private CustomerService service; + + /** + * 测试增删改查 + */ + @Test + public void test() { + + service.deleteAll(); + + // save a couple of customers + service.save(new Customer("Alice", "Smith")); + service.save(new Customer("Bob", "Smith")); + + // fetch all customers + System.out.println("Customers found with findAll():"); + System.out.println("-------------------------------"); + int count = 0; + for (Customer customer : service.findAll()) { + System.out.println(customer); + count++; + } + assertThat(count, is(2)); + + // fetch an individual customer + System.out.println("Customer found with findByFirstName('Alice'):"); + System.out.println("--------------------------------"); + Customer c = service.findByFirstName("Alice"); + assertThat(c, notNullValue()); + assertThat(c.getFirstName(), is("Alice")); + + System.out.println("Customers found with findByLastName('Smith'):"); + System.out.println("--------------------------------"); + + List list = service.findByLastName("Smith"); + assertThat(list, notNullValue()); + assertThat(list.size(), greaterThan(1)); + assertThat(list.size(), is(2)); + } +}