In the tutorial, JavaSampleApproach will show you how to create a SpringBoot Kotlin project that uses SpringJPA with @Lob
annotation to save Files/Images to MySQL database.
Contents
I. Technologies
– Kotlin 1.2.20
– Apache Maven 3.5.2
– Spring Tool Suite – Version 3.9.0.RELEASE
– MySQL
– Spring Boot – 1.5.10.RELEASE
II. Goal
– We create a SpringBoot project to save files/images to MySQL database as below structure:
Create MySQL table:
-> Run and check results:
III. Practice
Step to do:
– Create SpringBoot Kotlin project
– Create data model
– Create JPA Repository
– Implement Client to save/retrieve files/images
– Configure JPA connection & MySQL script
1. Create SpringBoot Kotlin project
Using SpringToolSuite to create a SpringBoot project with dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> |
2. Create data model
package com.javasampleapproach.kotlin.springboot.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") data class ImageModel( @Id @Column(name = "id") val id: Long, @Column(name = "name") val name: String, @Column(name = "type") val type: String, @Lob @Column(name="pic") val pic: ByteArray ){ private constructor(): this(-1, "", "", byteArrayOf()) } |
3. Create JPA Repository
package com.javasampleapproach.kotlin.springboot.saveimage2mysql.jpa import org.springframework.data.jpa.repository.JpaRepository; import com.javasampleapproach.kotlin.springboot.saveimage2mysql.model.ImageModel interface ImageRepository: JpaRepository<ImageModel, Long> |
4. Implement Client to save/retrieve files/images
package com.javasampleapproach.kotlin.springboot.saveimage2mysql import java.nio.file.Files import java.nio.file.Paths import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.core.io.ClassPathResource import org.springframework.context.annotation.Bean import org.springframework.boot.CommandLineRunner import com.javasampleapproach.kotlin.springboot.saveimage2mysql.jpa.ImageRepository import com.javasampleapproach.kotlin.springboot.saveimage2mysql.model.ImageModel @SpringBootApplication class KotlinSpringBootSaveImage2MySqlApplication{ @Bean fun imageProcess(imageRepository: ImageRepository) = CommandLineRunner { // image 1 val backImgFile = ClassPathResource("image/jsa_about_img_black_background.png") var arrayPic = ByteArray(backImgFile.contentLength().toInt()) backImgFile.getInputStream().read(arrayPic); val blackImage = ImageModel(1, "JSA-ABOUT-IMAGE-BLACK-BACKGROUND", "png", arrayPic) // image 2 val blueImgFile = ClassPathResource("image/jsa_about_img_blue_background.png") arrayPic = ByteArray(blueImgFile.contentLength().toInt()) blueImgFile.getInputStream().read(arrayPic) val blueImage = 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 imageRepository.findAll().forEach{ imageModel -> Files.write(Paths.get("retrieve-dir/" + imageModel.name + "." + imageModel.type), imageModel.pic); } } } fun main(args: Array<String>) { SpringApplication.run(KotlinSpringBootSaveImage2MySqlApplication::class.java, *args) } |
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
KotlinSpringBootSaveImage2MySQL
Last updated on September 18, 2018.