특정 문자열로 시작하는 값 조회

방법 CONCAT을 이용해야 된다. SELECT * FROM TABLE_NAME WHERE COLUMN_NAME LIKE CONCAT(‘%’, #{searchKeyword}) 참고 자료 https://dongram.tistory.com/12

2024-09-15 · 1 min · 16 words

DynamicSQL

목표 MyBatis의 dynamic sql 사용법을 이해한다. if 조건을 만족했을 때만 쿼리 추가 <select id="findActiveBlogWithTitleLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> </select> choose, when, otherwise Java의 switch 문과 같이 조건을 만족하는 하나의 구문만 추가 모두 만족하지 않을 경우 otherwise 구문 추가 <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test="title != null"> AND title like #{title} </when> <when test="author !...

2024-09-15 · 2 min · 399 words

batch update

목표 한꺼번에 다수의 row를 업데이트 해야될 때, 하나씩 update 쿼리를 보내면 부하가 많이 발생할 수 있다. mybatis에서 batch update 방법을 알아본다. 방법 SqlSessionFactory를 통해 SqlSession을 생성하고 flush와 commit을 한다. sqlSessionFactory.openSession(ExecutorType.BATCH).use { sqlSession -> val xxxMapper = sqlSession.getMapper(XXXMaper::class.java) xxxs.forEach { xxxMapper.update(it) } sqlSession.flushStatements() sqlSession.commit() } SqlSession 트랜잭션을 commit 또는 rollback하고, Mapper 인스턴스를 획득할 수 있다. auto-commit이 꺼져있다면, commit 메서드를 호출하기 전까지 데이터베이스에 커밋되지 않는다. commit 메서드를 호출하지 않고 SqlSession이 닫힌다면 롤백된다. sqlSessionFactory....

2024-09-15 · 1 min · 82 words