添加代码示例springboot-mongodb

This commit is contained in:
yidao620
2018-03-03 12:14:18 +08:00
parent befc5d96dd
commit d57263cc62
13 changed files with 521 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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, String> {
Customer findByFirstName(String firstName);
List<Customer> findByLastName(String lastName);
}

View File

@ -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<Customer> findAll() {
return repository.findAll();
}
/**
* 通过名查找某个客户
* @param firstName
* @return
*/
public Customer findByFirstName(String firstName) {
return repository.findByFirstName(firstName);
}
/**
* 通过姓查找客户列表
* @param lastName
* @return
*/
public List<Customer> findByLastName(String lastName) {
return repository.findByLastName(lastName);
}
}

View File

@ -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

View File

@ -0,0 +1,23 @@
_____ _______ _____ _____
/\ \ /::\ \ /\ \ /\ \
/::\____\ /::::\ \ /::\____\ /::\ \
/:::/ / /::::::\ \ /:::/ / /::::\ \
/:::/ / /::::::::\ \ /:::/ / /::::::\ \
/:::/ / /:::/~~\:::\ \ /:::/ / /:::/\:::\ \
/:::/ / /:::/ \:::\ \ /:::/____/ /:::/__\:::\ \
/:::/ / /:::/ / \:::\ \ |::| | /::::\ \:::\ \
/:::/ / /:::/____/ \:::\____\ |::| | _____ /::::::\ \:::\ \
/:::/ / |:::| | |:::| | |::| | /\ \ /:::/\:::\ \:::\ \
/:::/____/ |:::|____| |:::| | |::| | /::\____\/:::/__\:::\ \:::\____\
\:::\ \ \:::\ \ /:::/ / |::| | /:::/ /\:::\ \:::\ \::/ /
\:::\ \ \:::\ \ /:::/ / |::| | /:::/ / \:::\ \:::\ \/____/
\:::\ \ \:::\ /:::/ / |::|____|/:::/ / \:::\ \:::\ \
\:::\ \ \:::\__/:::/ / |:::::::::::/ / \:::\ \:::\____\
\:::\ \ \::::::::/ / \::::::::::/____/ \:::\ \::/ /
\:::\ \ \::::::/ / ~~~~~~~~~~ \:::\ \/____/
\:::\ \ \::::/ / \:::\ \
\:::\____\ \::/____/ \:::\____\
\::/ / ~~ \::/ /
\/____/ \/____/

View File

@ -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');

View File

@ -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<Customer> list = service.findByLastName("Smith");
assertThat(list, notNullValue());
assertThat(list.size(), greaterThan(1));
assertThat(list.size(), is(2));
}
}