
[英]How can I do paging and sorting using spring data with Couchbase
[英]How can I add attachments (Files) to doccuments in couchbase using Spring data JPA in java?
这是我引用的链接, 网址为http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/attachment/index.html,但没有提及服务或道类。 沙发床的春季文件
这是我的道课
@Repository
public interface UserRepository extends CrudRepository<User, String> {
}
下面是我的用户模型
@JsonIgnoreProperties(ignoreUnknown=true)
@Document
public class User implements Serializable {
private static final long serialVersionUID = -6815079861643922076L;
@JsonProperty("docType")
private String docType = "users";
@Id
@JsonProperty("id")
private String id;
@JsonProperty("firstName")
private String firstName;
@JsonProperty("lastName")
private String lastName;
@JsonProperty("password")
private byte[] password;
public String getDocType() {
return docType;
}
public void setDocType(String docType) {
this.docType = docType;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public byte[] getPassword() {
return password;
}
public void setPassword(byte[] password) {
this.password = password;
}
}
这是我的服务实施
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired(required=true)
private UserRepository userRepository;
public User findById(String id) throws Exception {
User user = userRepository.findOne(id.toLowerCase());
return user;
}
public User create(User user MultipartFile file) throws Exception {
String fileName = null;
if (!file.isEmpty()) {
try {
fileName = file.getOriginalFilename();
byte[] bytes = file.getBytes();
}catch (Exception ex) {
}
}
//i want to attach this file as attachment to this user in couch db
User returnUser = userRepository.save(user);
return returnUser;
}
public User update(User user MultipartFile file) throws Exception {
String fileName = null;
if (!file.isEmpty()) {
try {
fileName = file.getOriginalFilename();
byte[] bytes = file.getBytes();
}catch (Exception ex) {
}
}
//i want to attach this file as attachment to this user in couch db
User returnUser = userRepository.save(user);
return returnUser;
}
}
这是我的dbconfig
@Configuration
@EnableCouchbaseRepositories(basePackages="com.repository")
public class DBConfig extends AbstractCouchbaseConfiguration {
@Autowired(required=true)
private PropertyFileReader propertyFileReader;
@Override
protected List<String> bootstrapHosts() {
List<String> couchbaseHostList = Arrays.asList(propertyFileReader.getCouchbaseHostList().split("\\s*,\\s*"));
return couchbaseHostList;
}
@Override
protected String getBucketName() {
String bucketName = propertyFileReader.getCouchbaseBucketName();
return bucketName;
}
@Override
protected String getBucketPassword() {
String bucketPassword = propertyFileReader.getCouchbaseBucketPassword();
logger.debug("bootstrapHosts() : bucketPassword={}", bucketPassword);
return bucketPassword;
}
public CustomConversions customConversions() {
return new CustomConversions(
Arrays.asList(StringToByteConverter.INSTANCE));
}
@ReadingConverter
public static enum StringToByteConverter implements Converter<String, byte[]> {
INSTANCE;
@Override
public byte[] convert(String source) {
return Base64.decodeBase64(source);
}
}
}
不幸的是,我认为您将Couchbase Server和Couchbase lite混为一谈,它们是两种不同的产品。
您正在使用的spring框架与不支持附件的Couchbase Server交互。 但是,您可以在Couchbase中保存二进制Blob。
Couchbase lite确实支持附件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.