In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Add dependencies
Org.springframework.boot spring-boot-starter-data-mongodb
2. Configure application.properties
# # mongodb start # # spring.data.mongodb.host=localhostspring.data.mongodb.port=27017spring.data.mongodb.database=db_test##mongodb end # # #
Third, create entity classes
Package com.example.demo.pojo;import java.util.Date;/** * user Information * * @ Author: I love Dajin * @ Description: user Information * @ Date: Create in 14:09 2017-7-5 * / public class User {private int id; private String name; private Date createTime; public int getId () {return id;} public void setId (int id) {this.id = id } public String getName () {return name;} public void setName (String name) {this.name = name;} public Date getCreateTime () {return createTime;} public void setCreateTime (Date createTime) {this.createTime = createTime } @ Override public String toString () {return "User {" + "id=" + id + ", name='" + name +'\'+ ", createTime=" + createTime +'}';}}
Fourth, use MongoTemplate to implement
MongodbComponent.java
Package com.example.demo.utils.component;import com.example.demo.pojo.User;import org.springframework.beans.factory.annotation.Autowired;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 / * * Mongodb tool class * * @ Author: I love Dajin * @ Description: Mongodb tool class * @ Date: Create in 13:48 2017-7-5 * / @ Componentpublic class MongodbComponent {@ Autowired private MongoTemplate mongoTemplate; public void insert (User user) {mongoTemplate.insert (user);} public void deleteById (int id) {Criteria criteria = Criteria.where ("id") .in (id) Query query = new Query (criteria); mongoTemplate.remove (query, User.class);} public void updateById (User user) {Criteria criteria = Criteria.where ("id") .in (user.getId ()); Query query = new Query (criteria); Update update = new Update (); update.set ("name", user.getName ()); update.set ("createTime", user.getCreateTime ()) MongoTemplate.updateMulti (query, update, User.class);} public User selectById (int id) {Criteria criteria = Criteria.where ("id") .in (id); Query query = new Query (criteria); return mongoTemplate.findOne (query, User.class);}}
MongoTemplateTest.java (Test)
Package com.example.demo;import com.example.demo.pojo.User;import com.example.demo.utils.component.MongodbComponent;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import java.util.Date;/** * MongoTemplate Test * * @ Author: I love Dajin * @ Description: MongoTemplate Test * @ Date: Create in 13:54 2017-7-5 * / public class MongoTemplateTest extends ApplicationTests {@ Autowired private MongodbComponent mongodbComponent @ Test public void insert () {User user = new User (); user.setId (1); user.setName ("Zhang San"); user.setCreateTime (new Date ()); mongodbComponent.insert (user);} @ Test public void select () {System.out.println (mongodbComponent.selectById (1)) } @ Test public void update () {User user = new User (); user.setId (1); user.setName (Li Si); user.setCreateTime (new Date ()); mongodbComponent.updateById (user); System.out.println (mongodbComponent.selectById (1));} @ Test public void delete () {mongodbComponent.deleteById (1);}}
Use the interface to inherit MongoRepository
UserDao.java
Package com.example.demo.dao;import com.example.demo.pojo.User;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.mongodb.repository.MongoRepository;import java.util.List / * * user dao * @ Author: I love Dajin * @ Description: user dao * @ Date: user dao * @ Date: 14:09 2017-7-5 * / public interface UserDao extends MongoRepository {/ * search by name * @ Author: I love Dajin * @ Description: search by name * @ Date: 14:12 2017-7-5 * @ param name name * @ return * / List findByName (String name) / * * Paging query by name * @ Author: I love Dajin * @ Description: paging query * @ Date: 14:15 2017-7-5 * @ param name name * @ param pageable paging parameter * @ return * / Page findByName (String name, Pageable pageable);}
Test:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.