2015-10-16 55 views
1

以下是我提到http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/attachment/index.html的链接,但没有提及服务或dao类中的用法。 spring doc for couchbase如何使用Java中的Spring数据JPA将附件(文件)添加到couchbase中的文档?

这是我的DAO类

@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); 
     } 
    } 

} 

回答

0

不幸的是我认为你得到的Couchbase服务器和Couchbase lite混合起来,他们是两种不同的产品。

您正在使用的spring框架与不支持附件的Couchbase Server交互。但是,您可以将二进制Blob保存在Couchbase中。

Couchbase lite支持附件。

+0

谢谢@Paddy它真的帮助。有没有用于沙发基础lite的弹簧数据jpa?如果itz会有弹簧jpa支持附件吗? – Arjun

相关问题