SpringBoot-Mongodb 淩亂°似流年 2022-05-18 06:28 184阅读 0赞 目录结构: ![70][] pom.xml: <?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> <groupId>com.neo</groupId> <artifactId>spring-boot-mongodb</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>spring-boot-mongodb</name> <description>Demo project for Spring Boot and mongodb</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project> UserEntity: package com.neo.entity; import java.io.Serializable; /** * Created by summer on 2017/5/5. */ public class UserEntity implements Serializable { private static final long serialVersionUID = -3258839839160856613L; private Long id; private String userName; private String passWord; 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; } @Override public String toString() { return "UserEntity{" + "id=" + id + ", userName='" + userName + '\'' + ", passWord='" + passWord + '\'' + '}'; } } UserDao: package com.neo.dao; import com.neo.entity.UserEntity; /** * Created by summer on 2017/5/5. */ public interface UserDao { public void saveUser(UserEntity user); public UserEntity findUserByUserName(String userName); public int updateUser(UserEntity user); public void deleteUserById(Long id); } UserDaoImpl: package com.neo.dao.impl; import com.mongodb.WriteResult; import com.neo.dao.UserDao; import com.neo.entity.UserEntity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Component; /** * Created by summer on 2017/5/5. */ @Component public class UserDaoImpl implements UserDao { @Autowired private MongoTemplate mongoTemplate; /** * 创建对象 * @param user */ @Override public void saveUser(UserEntity user) { mongoTemplate.save(user); } /** * 根据用户名查询对象 * @param userName * @return */ @Override public UserEntity findUserByUserName(String userName) { Query query=new Query(Criteria.where("userName").is(userName)); UserEntity user = mongoTemplate.findOne(query , UserEntity.class); return user; } /** * 更新对象 * @param user */ @Override public int updateUser(UserEntity user) { Query query=new Query(Criteria.where("id").is(user.getId())); Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord()); //更新查询返回结果集的第一条 WriteResult result =mongoTemplate.updateFirst(query,update,UserEntity.class); //更新查询返回结果集的所有 // mongoTemplate.updateMulti(query,update,UserEntity.class); if(result!=null) return result.getN(); else return 0; } /** * 删除对象 * @param id */ @Override public void deleteUserById(Long id) { Query query=new Query(Criteria.where("id").is(id)); mongoTemplate.remove(query,UserEntity.class); } } Application: package com.neo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } application.properties: spring.application.name=spirng-boot-mongodb spring.data.mongodb.uri=mongodb://127.0.0.1:27017/test 上面的mongodb自行百度安装,然后安装一个Robo 3T 1.2.1可视化工具。 这里查看上面的uri链接地址: ![70 1][] 最后添加两个测试类: ApplicationTests: package com.neo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Test public void contextLoads() { System.out.println("hello world"); } } 测试结果: ![70 2][] UserDaoTest: package com.neo.dao; import com.neo.entity.UserEntity; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * Created by summer on 2017/5/5. */ @RunWith(SpringRunner.class) @SpringBootTest public class UserDaoTest { @Autowired private UserDao userDao; @Test public void testSaveUser() throws Exception { UserEntity user=new UserEntity(); user.setId(2l); user.setUserName("小明"); user.setPassWord("fffooo123"); userDao.saveUser(user); } @Test public void findUserByUserName(){ UserEntity user= userDao.findUserByUserName("小明"); System.out.println("user is "+user); } @Test public void updateUser(){ UserEntity user=new UserEntity(); user.setId(2l); user.setUserName("天空"); user.setPassWord("fffxxxx"); userDao.updateUser(user); } @Test public void deleteUserById(){ userDao.deleteUserById(2l); } } 测试保存结果: ![70 3][] ![70 4][] 查找测试: ![70 5][] 修改测试: ![70 6][] ![70 7][] 删除测试: ![70 8][] ![70 9][] [70]: /images/20220518/08ba5df2d88f40868935ca90bc27ca50.png [70 1]: /images/20220518/7193ac4cee6f4160ad475f346340f57a.png [70 2]: /images/20220518/1634cf0372c6430d86c5fcad80da603b.png [70 3]: /images/20220518/d8df901be43648578a4b8b9b745dbbeb.png [70 4]: /images/20220518/7e0aeff890674abb85dc3daf76f74bd5.png [70 5]: /images/20220518/467f0f91552043d4b762be66f6c6c431.png [70 6]: /images/20220518/50a5eb8c3f6644da8bd85ed744a302e0.png [70 7]: /images/20220518/37106cca2da54d19a2c3ab918f6914ff.png [70 8]: /images/20220518/f5827392909a42908d727dbf2428011c.png [70 9]: /images/20220518/bfebf6b2c8cf44119e858d8dfe440dfd.png
还没有评论,来说两句吧...