mybatis를 이용해서 BLOB 저장
2024. 11. 7. 22:56ㆍJava/mybatis
이미지를 byte[]로 변환
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public byte[] readImage(String filePath) throws IOException {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
byte[] imageBytes = new byte[(int) file.length()];
fis.read(imageBytes);
fis.close();
return imageBytes;
}
mybatis 사용하지 않고, BLOB 저장
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public void saveImageToDatabase(String filePath) throws SQLException, IOException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// Get the image as a byte array
byte[] imageBytes = readImage(filePath);
// Get the connection
conn = getConnection();
// SQL query with a BLOB column
String sql = "INSERT INTO your_table (id, image_column) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
// Set the parameters
pstmt.setInt(1, 1); // Set the ID (or another field)
pstmt.setBytes(2, imageBytes); // Set the BLOB column with byte array
// Execute the insert
pstmt.executeUpdate();
System.out.println("Image saved to database successfully.");
} finally {
// Close resources
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
}
Mapper Interface
public interface ImageMapper {
void insertImage(@Param("id") int id, @Param("image") byte[] image);
}
XML
<mapper namespace="com.example.mapper.ImageMapper">
<insert id="insertImage">
INSERT INTO your_table (id, image)
VALUES (#{id}, #{image, jdbcType=BLOB})
</insert>
</mapper>
Java Code
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class ImageService {
private SqlSessionFactory sqlSessionFactory;
public ImageService(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
// Method to read an image as byte array
public byte[] readImage(String filePath) throws IOException {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
byte[] imageBytes = new byte[(int) file.length()];
fis.read(imageBytes);
fis.close();
return imageBytes;
}
// Method to save image into database
public void saveImage(int id, String imagePath) {
try (SqlSession session = sqlSessionFactory.openSession()) {
ImageMapper mapper = session.getMapper(ImageMapper.class);
byte[] imageBytes = readImage(imagePath);
mapper.insertImage(id, imageBytes);
session.commit();
System.out.println("Image inserted successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Main
public class Main {
public static void main(String[] args) {
// Initialize SqlSessionFactory (usually from MyBatis config)
SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
// Create an instance of the service class
ImageService imageService = new ImageService(sqlSessionFactory);
// Save the image (replace with the actual image path and id)
imageService.saveImage(1, "path_to_image.jpg");
}
}
Mybatis Configuration
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml"; // MyBatis configuration XML file
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
'Java > mybatis' 카테고리의 다른 글
Mybatis insert후 키값 반환하기 (0) | 2024.12.27 |
---|---|
Mybatis if ~ else 적용하기 (0) | 2024.11.27 |
mybatis ORA-00979: GROUP BY 표현식이 아닙니다 (0) | 2024.11.26 |
IN() 절 처리 (0) | 2024.11.07 |
Spring boot mybatis 환경파일 설정 (0) | 2024.08.14 |