SpringBoot对常用的数据库支持外,对NoSQL 数据库也进行了封装自动化。
这一篇主要讲springboot与mongo多数据源相关的配置
1、mongo的介绍
MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种。它在许多场景下可用于替代传统的关系型数据库或键/值存储方式,这里不再多做说明。详细介绍参考
mongo入门
2、项目构建
添加pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
3、在application.properties中添加配置
##start mongodb for basic
#----------------------------------------------
basic.mongodb.host= 127.0.0.1
basic.mongodb.port= 27017
basic.mongodb.database= basic
#----------------------------------------------
##end mongodb for spirit
##start mongodb for basic
#----------------------------------------------
order.mongodb.host= 127.0.0.2
order.mongodb.port= 27017
order.mongodb.database= order
#----------------------------------------------
##end mongodb for spirit
4、配置相应的数据源
这里我们采用mongoTemplate这种模式去做mongo的相关操作
先写一个基础的抽象类
public abstract class AbstractMongoConfigure {
private String host, database;
private int port;
/*
* Method that creates MongoDbFactory
* Common to both of the MongoDb connections
*/
public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(host, port), database);
}
/*
* Factory method to create the MongoTemplate
*/
abstract public MongoTemplate getMongoTemplate() throws Exception;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
3.1)接下来第一个库的封装
@Configuration
@EnableMongoRepositories(basePackages = {"com.sunliangliang.service.basic"}, mongoTemplateRef = "basicMongoTemplate")
@ConfigurationProperties(prefix = "basic.mongodb")
public class BasicMongoConfigure extends AbstractMongoConfigure {
@Override
@Bean(name = "basicMongoTemplate")
public MongoTemplate getMongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
其中 basePackages 的值是该库应用于相应的基础包,prefix即为相应的applications中的配置值
3.2)接下来第两个库的封装
@Configuration
@EnableMongoRepositories(basePackages = {"com.sunliangliang.service.order"}, mongoTemplateRef = "orderMongoTemplate")
@ConfigurationProperties(prefix = "order.mongodb")
public class OrderMongoConfigure extends AbstractMongoConfigure{
@Primary
@Override
@Bean(name = "orderMongoTemplate")
public MongoTemplate getMongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
封装完成后接下来进行相应实体的配置
4、创建数据实体
public class UserInfo implements Serializable{
private long id;
private String username;
private String password;
private String nickname;
private int age;
private String name;
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;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4、添加对实体的操作
在这里我写了一个公用的接口用于基础的增删改查
public interface BaseService<T> {
Boolean save(T entity);
Boolean update(T entity);
Boolean delete(Long id);
T getById(Long id);
List<T> listAll();
List<T> listAll(T entity);
}
接下来接口的实现
@Service
public class UserInfoService implements BaseService<UserInfo>{
@Autowired
@Qualifier("basicMongoTemplate")
private MongoTemplate mongoTemplate;
@Override
public Boolean save(UserInfo entity) {
mongoTemplate.save(entity);
return true;
}
@Override
public Boolean update(UserInfo entity) {
return null;
}
@Override
public Boolean delete(Long id) {
return null;
}
@Override
public UserInfo getById(Long id) {
return null;
}
@Override
public List<UserInfo> listAll() {
return mongoTemplate.findAll(UserInfo.class);
}
@Override
public List<UserInfo> listAll(UserInfo entity) {
return null;
}
}
注意此处@Qualifier("basicMongoTemplate")中即为相应的具体实现类。
这里只写了添加和查询列表的实现,其他的查询相应的api即可
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserInfoServiceTest {
@Autowired
private UserInfoService userInfoService;
@Test
public void save() throws Exception {
UserInfo userInfo = new UserInfo();
userInfo.setAge(23);
userInfo.setId(1000l);
userInfo.setName("管理员");
userInfo.setNickname("hehe");
userInfo.setPassword("123456");
userInfo.setUsername("admin");
userInfoService.save(userInfo);
}
@Test
public void listAll() throws Exception {
userInfoService.listAll();
}
}
若库不存在,会自动生成相应的库和文档
如图:
另一个order相关库的配置就不再这里写了,相应的请参考代码
github
中lessons-5