SpringJPA – save/retrieve Files/Images to MySQL database with @Lob annotation

SpringJPA – save_retrieve Files_Images to MySQL database with @Lob annotation

In the tutorial, JavaSampleApproach will show you how to create a SpringBoot project that uses SpringJpa with @Lob annotation to save Files/Images to MySQL database.

I. Technologies

– Java 1.8
– Maven 3.6.1
– Spring Tool Suite – Version 3.9.0.RELEASE
– MySQL
– Spring Boot – 1.5.9.RELEASE

II. Goal

– We create a SpringBoot project to save files/images to MySQL database as below structure:

Spring JPA saves Files-Images to MySQL server - project structure

Create MySQL table:

Spring JPA saves Files-Images to MySQL server - mysql structure

-> Run and check results:

Spring JPA saves Files-Images to MySQL server - mysql results

Spring JPA saves Files-Images to MySQL server - retrieve files results

III. Practice

Step to do:
– Create SpringBoot project
– Create data model
– Create JPA Repository
– Implement Client to save/retrieve files/images
– Configure JPA connection & MySQL script

1. Create SpringBoot project

Using SpringToolSuite to create a SpringBoot project with dependencies:


	org.springframework.boot
	spring-boot-starter-data-jpa



	mysql
	mysql-connector-java
	runtime

2. Create data model
package com.javasampleapproach.saveimage2mysql.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name="image_model")
public class ImageModel {
	@Id
    @Column(name = "id")
    private Long id;
	
    @Column(name = "name")
	private String name;
    
    @Column(name = "type")
	private String type;
	
	@Lob
    @Column(name="pic")
    private byte[] pic;
	
	public ImageModel(){}
	
	public ImageModel(long id, String name, String type, byte[] pic){
		this.id = id;
		this.name = name;
		this.type = type;
		this.pic = pic;
	}
	
	public Long getId(){
		return this.id;
	}
	
	public void setId(Long id){
		this.id = id;
	}
	
	public String getName(){
		return this.name;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getType(){
		return this.type;
	}
	
	public void setType(String type){
		this.type = type;
	}
	
	public byte[] getPic(){
		return this.pic;
	}
	
	public void setPic(byte[] pic){
		this.pic = pic;
	}
}

3. Create JPA Repository
package com.javasampleapproach.saveimage2mysql.jpa;

import org.springframework.data.jpa.repository.JpaRepository;

import com.javasampleapproach.saveimage2mysql.model.ImageModel;

public interface ImageRepository extends JpaRepository{
}

4. Implement Client to save/retrieve files/images

Implement code to save/retrieve files/images from MySQL database via SpringJPA repository:

package com.javasampleapproach.saveimage2mysql;

import java.nio.file.Files;
import java.nio.file.Paths;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;

import com.javasampleapproach.saveimage2mysql.jpa.ImageRepository;
import com.javasampleapproach.saveimage2mysql.model.ImageModel;

@SpringBootApplication
public class SpringJpaSaveImage2MySqlApplication implements CommandLineRunner{

	@Autowired
	ImageRepository imageRepository;
	
	public static void main(String[] args) {
		SpringApplication.run(SpringJpaSaveImage2MySqlApplication.class, args);
	}

	@Override
	public void run(String... arg0) throws Exception {
		// image 1
		ClassPathResource backImgFile = new ClassPathResource("image/jsa_about_img_black_background.png");
		byte[] arrayPic = new byte[(int) backImgFile.contentLength()];
		backImgFile.getInputStream().read(arrayPic);
		ImageModel blackImage = new ImageModel(1, "JSA-ABOUT-IMAGE-BLACK-BACKGROUND", "png", arrayPic);
		
		// image 2
		ClassPathResource blueImgFile = new ClassPathResource("image/jsa_about_img_blue_background.png");
		arrayPic = new byte[(int) blueImgFile.contentLength()];
		blueImgFile.getInputStream().read(arrayPic);
		ImageModel blueImage = new ImageModel(2, "JSA-ABOUT-IMAGE-BLUE-BACKGROUND", "png", arrayPic);
		
		// store image to MySQL via SpringJPA
		imageRepository.save(blackImage);
		imageRepository.save(blueImage);
		
		// retrieve image from MySQL via SpringJPA
		for(ImageModel imageModel : imageRepository.findAll()){
			Files.write(Paths.get("retrieve-dir/" + imageModel.getName() + "." + imageModel.getType()), imageModel.getPic());
		}
	}
}

5. Configure JPA connection & MySQL script
5.1 Configure connection info

– Open application.properties file, add connection info:

spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=12345
5.2 MySQL script

– Implement MySQL creating table script:

CREATE TABLE image_model(
   id BIGINT NOT NULL,
   name VARCHAR(100) NOT NULL,
   type VARCHAR(10) NOT NULL,
   pic BLOB NOT NULL,
   PRIMARY KEY (id)
);

IV. Sourcecode

SpringJpaSaveImage2MySql



By grokonez | December 3, 2017.

Last updated on May 18, 2019.



Related Posts


2 thoughts on “SpringJPA – save/retrieve Files/Images to MySQL database with @Lob annotation”

  1. Hello
    I would like to build a SpringBoot and angular6 application to record images in a remote database.
    my difficulty is i do not know how to load an image with angular6 and then save this image or this file with springBoot
    thank you for giving me an example illustrating this approach
    thanks again

Got Something To Say:

Your email address will not be published. Required fields are marked *

*