Springboot之整合MongoDB
一、在虚拟机中安装MongoDB
执行脚本
#!/bin/bash
echo "**************************************"
echo "***** *****"
echo "*** Mongodb 安装 ***"
echo "***** *****"
echo "**************************************"
echo "=====>开始下载Mongodb:"
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.3.tgz
if [ $? -eq 0 ]; then
echo "=====>下载成功!"
echo "=====>开始解压!"
tar -zxvf mongodb-linux-x86_64-3.6.3.tgz
echo "=====>解压成功!"
mkdir /usr/local/mongodb && mv mongodb-linux-x86_64-3.6.3/* /usr/local/mongodb/ && cd /usr/local/mongodb && mkdir data && touch logs && cd bin
echo "=====>开始安装!"
./mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork
if [ $? -eq 0 ]; then
echo "=====>安装成功";
touch mongodb.conf
echo "=====>配置mongodb"
echo "dbpath=/usr/local/mongodb/data" >> mongodb.conf
echo "logpath=/usr/local/mongodb/logs" >> mongodb.conf
echo "logappend=true" >> mongodb.conf
echo "port=27017" >> mongodb.conf
echo "fork=true" >> mongodb.conf
echo "bind_ip=0.0.0.0" >> mongodb.conf
echo "=====>设置Mongodb启动项"
touch /lib/systemd/system/mongodb.service
echo "[Unit]" >> /lib/systemd/system/mongodb.service
echo "Description=mongodb" >> /lib/systemd/system/mongodb.service
echo "After=network.target remote-fs.target nss-lookup.target" >> /lib/systemd/system/mongodb.service
echo "[Service]" >> /lib/systemd/system/mongodb.service
echo "Type=forking" >> /lib/systemd/system/mongodb.service
echo "ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf" >>/lib/systemd/system/mongodb.service
echo "ExecReload=/bin/kill -s HUP $MAINPID" >> /lib/systemd/system/mongodb.service
echo "ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/bin/mongodb.conf" >> /lib/systemd/system/mongodb.service
echo "PrivateTmp=true" >> /lib/systemd/system/mongodb.service
echo "[Install]" >> /lib/systemd/system/mongodb.service
echo "WantedBy=multi-user.target" >> /lib/systemd/system/mongodb.service
echo "=====> Mongodb配置完毕!";
systemctl daemon-reload
systemctl enable mongodb.service
sed -i '/^PATH/ i\MONGODB_HOME\=\/usr\/local\/mongodb' /etc/profile
sed -i '/^PATH/ s/$/\:\$MONGODB_HOME\/bin/' /etc/profile
sed -i '/export/ s/$/\ MONGODB_HOME/' /etc/profile
source /etc/profile
else
echo "=====>安装失败!";
exit;
fi
else
echo "=====>下载失败,结束安装!"
exit
fi
echo "===>开启mongodb<===";
mongo
刷新环境变量
source /etc/profile
关闭防火墙
systemctl stop firewalld
查看MongoDB的配置文件
# MongoDB的数据存储目录
dbpath=/usr/local/mongodb/data
# 日志保存文件
logpath=/usr/local/mongodb/logs
logappend=true
port=27017
fork=true
# 允许远程访问
bind_ip=0.0.0.0
Mongodb基本操作
systemctl stop mongodb.service
systemctl start mongodb.service
二、启动Springboot项目
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mongodb.date</groupId>
<artifactId>mongodb-data</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mongodb-data</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml文件
spring:
data:
mongodb:
uri: mongodb://192.168.1.10/test
创建实体类
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -1L;
private Long id;
private String username;
private Integer age;
public User() {
}
public User(Long id, String username, Integer age) {
this.id = id;
this.username = username;
this.age = age;
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", age=" + age +
'}';
}
}
Mapper层
import com.mongodb.date.entity.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserRepository extends MongoRepository<User, Long> {
User findUserById(Long id);
List<User> findByAge(Integer age);
}
Service层
import com.mongodb.date.entity.User;
import java.util.List;
public interface UserService {
User add(User user);
List<User> findByAge(Integer age);
User findById(Long id);
}
Service实现类
import com.mongodb.date.entity.User;
import com.mongodb.date.mapper.UserRepository;
import com.mongodb.date.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private MongoTemplate mongoTemplate;
@Override
public User add(User user) {
return userRepository.save(user);
}
@Override
public List<User> findByAge(Integer age) {
return userRepository.findByAge(age);
}
@Override
public User findById(Long id) {
return userRepository.findUserById(id);
}
}
控制器类
import com.mongodb.date.entity.User;
import com.mongodb.date.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api")
public class DemoController {
@Autowired
private UserService userService;
@GetMapping("/list/{age}")
public List<User> getList(@PathVariable("age") Integer age) {
return userService.findByAge(age);
}
@PostMapping("/add")
public User add(User user) {
return userService.add(user);
}
}
三、测试
接口:http://localhost:8080/api/add
接口: http://localhost:8080/api/list/12