nelson
5 years ago
6 changed files with 176 additions and 1 deletions
@ -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<User> teacherPage = userService.findByUsername("nelson", pageable1);
|
||||
|
List<User> list = userService.finAll(); |
||||
|
return list; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@GetMapping("user") |
||||
|
public User username() { |
||||
|
User list = userService.findByUsername("nelson"); |
||||
|
return list; |
||||
|
} |
||||
|
} |
@ -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; |
||||
|
} |
||||
|
} |
@ -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<User, ObjectId> { |
||||
|
Page<User> findByUsername(String username, Pageable pageable); |
||||
|
User findByUsername(String username); |
||||
|
} |
@ -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<User> findByUsername(String username, Pageable pageable1); |
||||
|
|
||||
|
List<User> finAll(); |
||||
|
|
||||
|
User findByUsername(String nelson); |
||||
|
} |
@ -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<User> findByUsername(String username, Pageable pageable) { |
||||
|
return userRepository.findAll(pageable); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<User> finAll() { |
||||
|
return userRepository.findAll(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public User findByUsername(String username) { |
||||
|
return userRepository.findByUsername(username); |
||||
|
} |
||||
|
} |
@ -1,2 +1,11 @@ |
|||||
server: |
server: |
||||
port: 8080 |
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 |
Loading…
Reference in new issue