1. function
Use spring to link mongodb, and make additional deletions and checks
2. Class structure diagram
3. Implement
Code structure
Entity #userentity
Packagecom.jihite.entity; Public classuserentity {Private intID; PrivateString name; PrivateString passwd; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getpasswd () {returnpasswd; } Public voidsetpasswd (String passwd) { This. passwd =passwd; } @Override PublicString toString () {returnString.Format ("id:%s, name:%s, passwd:%s", ID, name, passwd); }}
View Code
Interface #userdao
Package Com.jihite.dao; Import com.jihite.entity.UserEntity; Public Interface Userdao { void saveuser (userentity Userdao); Userentity getuserbyname (String name); Long updateuserentity (userentity userentity); void Deleteuserbyid (int ID);}
Implement #userdaoimpl
PackageCom.jihite.dao.impl;ImportCom.jihite.dao.UserDao;Importcom.jihite.entity.UserEntity;ImportCom.mongodb.client.result.UpdateResult;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.mongodb.core.MongoTemplate;ImportOrg.springframework.data.mongodb.core.query.Criteria;ImportOrg.springframework.data.mongodb.core.query.Query;Importorg.springframework.data.mongodb.core.query.Update;Importorg.springframework.stereotype.Component; @Component Public classUserdaoimplImplementsuserdao{@AutowiredPrivatemongotemplate mongotemplate; @Override Public voidSaveuser (userentity Userdao) {mongotemplate.save (Userdao); } @Override Publicuserentity getuserbyname (String name) {query Query=NewQuery (Criteria.where ("name"). is (name)); returnMongotemplate.findone (query, userentity.class); } @Override Public Longupdateuserentity (userentity userentity) {query Query=NewQuery (Criteria.where ("id"). Is (Userentity.getid ())); Update Update=NewUpdate (). Set ("Name", Userentity.getname ()). Set ("passwd", USERENTITY.GETPASSWD ()); Updateresult Writeresult= Mongotemplate.updatefirst (query, update, userentity.class); returnWriteresult.getmatchedcount (); } @Override Public voidDeleteuserbyid (intID) {query query=NewQuery (Criteria.where ("id"). is (ID)); Mongotemplate.remove (query, userentity.class); }}
#测试
PackageCom.jihite.dao;Importcom.jihite.entity.UserEntity;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.boot.test.context.SpringBootTest;ImportOrg.springframework.test.context.junit4.SpringRunner; the @RunWith (Springrunner.class) @SpringBootTest Public classuserdaotest {@AutowiredPrivateUserdao Userdao; @Test Public voidTestsaveuser () {userentity user=Newuserentity (); User.setid (1); User.setname ("Jihite"); USER.SETPASSWD ("ABC"); Userdao.saveuser (user); } @Test Public voidTestgetuserbyname () {userentity user= Userdao.getuserbyname ("Jihite"); SYSTEM.OUT.PRINTLN (user); } @Test Public voidtestupdateuserentity () {userentity user=Newuserentity (); User.setid (1); User.setname ("Jihite2"); USER.SETPASSWD ("EFG"); userdao.updateuserentity (user); User= Userdao.getuserbyname ("Jihite2"); SYSTEM.OUT.PRINTLN (user); } @Test Public voidTestdeleteuserbyid () {Userdao.deleteuserbyid (1); Userentity User=Newuserentity (); System.out.println (Userdao.getuserbyname ("Jihite")); System.out.println (Userdao.getuserbyname ("Jihite2")); }}
4. Notes
4.1 Spring Boot Parent Dependency
<parent> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-parent</artifactid> <version>1.5.3.RELEASE</version> < Relativepath/> <!--lookup parent from repository--></parent>
This configuration is spring boot parent dependency, with this, the current project is the Spring Boot project. Spring-boot-starter-parent is a special starterthat is used to provide the relevant Maven default dependency, and after using it, the usual package dependencies can be omitted from the version tag.
4.2 Start Dependent spring-boot-starter-***
Start-ups are like brand-name machines (compared to assembly machines) that automatically encapsulate the dependencies of the functionality you want to implement, and reduce the effort involved in resolving dependencies.
Spring boot provides many initial dependencies to reduce the complexity of project dependencies.
5. Code
Link
Spring-boot-mongodb