简体   繁体   中英

What is the best way to store base64 string in MYSQL database?

I'm working on a small web application where I have to store image of a user into Mysql database. I'm new to the hibernate framework and I'm struck here. I have converted the image into Base64 string. Can anyone suggest me how to store this string into database using hibernate.

Unless you can define a max size to your Base64 String , the best way to store your image content is not to store it as Base64 String but rather as an array of bytes (since it could be big) by simply defining your field with the JPA annotations @Lob (stands for Large Object) and with the JPA annotation @Basic(fetch = FetchType.LAZY) in case you want to fetch it lazily, as next:

@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] image;

I recommend the below

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "file64", columnDefinition = "LONGBLOB")
private byte[] file64;

The LONGBLOB allows to save larger content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM