目录
一、Mongodb安装
Mongodb我安装在本机上,安装教程可以参考这篇博客 《Mongodb安装教程》
本机安装和可视化工具都有详细教程
二、整合SpringBoot
1.搭建环境
1.2 pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
1.3 yml文件
spring:
data:
mongodb:
uri: mongodb://localhost/goods
关于uri的写法区分为连接有密码或无密码
- - - -
在Mongodb控制台设置密码
use test
db.createUser(
{
user:"root",
pwd:"123456",
roles:[{role:"root",db:"admin"}]
}
)
有密码写法:
spring:
data:
mongodb:
uri: mongodb://root:123456@localhost:27017/goods
三、代码编写
实体类(pojo)
@Document("good") //绑定映射为mongodb中的哪个Collection
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
@Id
@Field(value = "_id")
private ObjectId id;
@Field(value = "name")
private String name;
@Field(value = "price")
private String price;
@Field(value = "remark")
private String remark;
}
服务层(service)
接口
public interface GoodsService {
List<Goods> findAll();
void addGoods(Goods goods);
Goods findGoodsByName(String name);
void dltGoodById(ObjectId id);
void updGood(Goods goods);
}
实现类
@Service
public class GoodsServiceImpl implements GoodsService {
@Autowired
private GoodsDao goodsDao;
@Override
public List<Goods> findAll() {
return goodsDao.findAll();
}
@Override
public void addGoods(Goods goods) {
goodsDao.addGood(goods);
}
@Override
public Goods findGoodsByName(String name) {
return goodsDao.findGoodByName(name);
}
@Override
public void dltGoodById(ObjectId id) {
goodsDao.dltGoodById(id);
}
@Override
public void updGood(Goods goods) {
goodsDao.updGood(goods);
}
}
持久层(dao)
接口
public interface GoodsDao {
List<Goods> findAll();
void addGood(Goods goods);
Goods findGoodByName(String name);
void dltGoodById(ObjectId id);
void updGood(Goods goods);
}
实现类
@Repository
public class GoodsDaoImpl implements GoodsDao {
@Resource
private MongoTemplate mongoTemplate;
@Override
public List<Goods> findAll() {
return mongoTemplate.findAll(Goods.class);
}
@Override
public void addGood(Goods goods) {
mongoTemplate.insert(goods);
}
@Override
public Goods findGoodByName(String name) {
Criteria c=new Criteria();
c.and("name").is(name);
Query query=new Query(c);
return mongoTemplate.findOne(query,Goods.class);
}
@Override
public void dltGoodById(ObjectId id) {
Criteria c=new Criteria();
c.and("id").is(id);
Query query=new Query(c);
DeleteResult result=mongoTemplate.remove(query,Goods.class);
long deleteCount=result.getDeletedCount();
if (deleteCount==1){
System.out.println("删除成功!!");
}else {
System.out.println("删除失败!!");
}
}
@Override
public void updGood(Goods goods) {
Query query=new Query(Criteria.where("id").is(goods.getId()));
Update update=new Update();
update.set("name",goods.getName());
update.set("price",goods.getPrice());
update.set("remark",goods.getRemark());
mongoTemplate.upsert(query,update,Goods.class);
}
}
简单测试
@SpringBootTest
class MongoDbApplicationTests {
@Autowired
private GoodsService goodsService;
/**
* 查询所有商品
*/
@Test
public void findAll(){
List<Goods>list=goodsService.findAll();
list.forEach(System.out::println);
}
/**
* 新增商品
* @return
*/
@Test
public void addGood(){
Goods goods=new Goods();
goods.setName("小八嘎");
goods.setPrice("18.8");
goods.setRemark("货到付款");
goodsService.addGoods(goods);
}
/**
* 根据名称/id删除
*/
@Test
public void dltGoodById(){
//先根据名称查询id
String name="小八嘎";
Goods goods=goodsService.findGoodsByName(name);
goodsService.dltGoodById(goods.getId());
}
/**
* 根据名称修改商品
*/
@Test
public void updGood(){
//先根据名称查询实体类
String name="小八嘎";
Goods goods=goodsService.findGoodsByName(name);
goods.setPrice("99");
goods.setRemark("八嘎!");
goodsService.updGood(goods);
}
}