From 4e62262bc30b6d1b10a3fcb3881b9a07b309bbb3 Mon Sep 17 00:00:00 2001 From: nelson Date: Sun, 18 Aug 2019 17:19:02 +0800 Subject: [PATCH] crud --- .../sbmongo/controller/UserController.java | 45 ++++++++++++++++ .../java/com/example/sbmongo/model/User.java | 54 +++++++++++++++++++ .../sbmongo/repository/UserRepository.java | 12 +++++ .../example/sbmongo/service/UserService.java | 17 ++++++ .../sbmongo/service/impl/UserServiceImpl.java | 38 +++++++++++++ src/main/resources/application.yml | 11 +++- 6 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/sbmongo/controller/UserController.java create mode 100644 src/main/java/com/example/sbmongo/model/User.java create mode 100644 src/main/java/com/example/sbmongo/repository/UserRepository.java create mode 100644 src/main/java/com/example/sbmongo/service/UserService.java create mode 100644 src/main/java/com/example/sbmongo/service/impl/UserServiceImpl.java diff --git a/src/main/java/com/example/sbmongo/controller/UserController.java b/src/main/java/com/example/sbmongo/controller/UserController.java new file mode 100644 index 0000000..17e9381 --- /dev/null +++ b/src/main/java/com/example/sbmongo/controller/UserController.java @@ -0,0 +1,45 @@ +package com.example.sbmongo.controller; + +import com.example.sbmongo.model.User; +import com.example.sbmongo.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.UUID; + +@RestController +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping("save") + public User save() { + User user = new User(); + user.setAge(12); + user.setUsername(UUID.randomUUID().toString()); + return userService.save(user); + } + + + @GetMapping("list") + public Object list() { + Pageable pageable1 = PageRequest.of(1, 3); + //获取分页数据,每页3条数,取第1页的数据, +// Page teacherPage = userService.findByUsername("nelson", pageable1); + List list = userService.finAll(); + return list; + } + + + @GetMapping("user") + public User username() { + User list = userService.findByUsername("nelson"); + return list; + } +} diff --git a/src/main/java/com/example/sbmongo/model/User.java b/src/main/java/com/example/sbmongo/model/User.java new file mode 100644 index 0000000..010eb6d --- /dev/null +++ b/src/main/java/com/example/sbmongo/model/User.java @@ -0,0 +1,54 @@ +package com.example.sbmongo.model; + +import org.bson.types.ObjectId; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.Field; + +import java.io.Serializable; + +@Document(collection = "zgm_user") +public class User implements Serializable { + + @Id +// private Long id; + private ObjectId id; + + @Field("username") + private String username; + + private Integer age; + +// public ObjectId getId() { +// return id; +// } +// +// public void setId(ObjectId id) { +// this.id = id; +// } + + + public ObjectId getId() { + return id; + } + + public void setId(ObjectId 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; + } +} diff --git a/src/main/java/com/example/sbmongo/repository/UserRepository.java b/src/main/java/com/example/sbmongo/repository/UserRepository.java new file mode 100644 index 0000000..084bfcb --- /dev/null +++ b/src/main/java/com/example/sbmongo/repository/UserRepository.java @@ -0,0 +1,12 @@ +package com.example.sbmongo.repository; + +import com.example.sbmongo.model.User; +import org.bson.types.ObjectId; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.mongodb.repository.MongoRepository; + +public interface UserRepository extends MongoRepository { + Page findByUsername(String username, Pageable pageable); + User findByUsername(String username); +} diff --git a/src/main/java/com/example/sbmongo/service/UserService.java b/src/main/java/com/example/sbmongo/service/UserService.java new file mode 100644 index 0000000..f15c009 --- /dev/null +++ b/src/main/java/com/example/sbmongo/service/UserService.java @@ -0,0 +1,17 @@ +package com.example.sbmongo.service; + +import com.example.sbmongo.model.User; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +import java.util.List; + +public interface UserService { + User save(User user); + + Page findByUsername(String username, Pageable pageable1); + + List finAll(); + + User findByUsername(String nelson); +} diff --git a/src/main/java/com/example/sbmongo/service/impl/UserServiceImpl.java b/src/main/java/com/example/sbmongo/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..016aff4 --- /dev/null +++ b/src/main/java/com/example/sbmongo/service/impl/UserServiceImpl.java @@ -0,0 +1,38 @@ +package com.example.sbmongo.service.impl; + +import com.example.sbmongo.model.User; +import com.example.sbmongo.repository.UserRepository; +import com.example.sbmongo.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserServiceImpl implements UserService { + + @Autowired + private UserRepository userRepository; + + @Override + public User save(User user) { + return userRepository.save(user); + } + + @Override + public Page findByUsername(String username, Pageable pageable) { + return userRepository.findAll(pageable); + } + + @Override + public List finAll() { + return userRepository.findAll(); + } + + @Override + public User findByUsername(String username) { + return userRepository.findByUsername(username); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 47fbb02..0142016 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,2 +1,11 @@ server: - port: 8080 \ No newline at end of file + port: 8080 +spring: + data: + mongodb: +# host: 106.12.3.97 +# port: 27017 +# database: twelvecrm +# username: root +# password: yasaka.00 + uri: mongodb://root:yasaka.00@106.12.3.97:27017/admin?authSource=admin&authMechanism=SCRAM-SHA-1 \ No newline at end of file