본문 바로가기
코딩도전기/Spring Boot

Spring Boot - 쿼리문 로고 찍기 / 동적쿼리

by 코도꼬마 2023. 5. 25.

쿼리문 로고 찍는 방법

 

 

Maven Central

Maven Central

central.sonatype.com

<!-- SQL Logger -->
<dependency>
    <groupId>org.bgee.log4jdbc-log4j2</groupId>
    <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
    <version>1.16</version>
</dependency>

 

  • application.properties에서 아래 내용 추가 및 변경
# jdbc
# name 변경
spring.datasource.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
# log4jdbc 추가
spring.datasource.url=jdbc:log4jdbc:mariadb://localhost:1512/mydb
spring.datasource.username=web_user
spring.datasource.password=pass

 

  • 아래 파일을 src/main/resources 폴더에 추가(application.properties과 같은 위치)

log4jdbc.log4j2.properties
0.00MB

  • 로그를 선택해서 찍히게 함

logback-spring.xml
0.00MB

 

  • application.properties에서 아래 내용 추가(debug 사라짐)
logging.level.root=info

 

 

 

동적쿼리

  • 파라메터로 들어오는 값에 따라 쿼리문을 변경해주고 싶을 경우 사용(쿼리문 여러개 작성하지 않아도 됨)
    • ex) 특정한 값이 null 로 들어왔을 경우 다른 값으로 대처
    • ex) 특정한 조건에 따라 조건 절이 변경
    • ex) 조건에 따라 수정하는 column 변경
  • 동적쿼리 문장에는 주로 if문 사용(trim 이나 for each 등의 문이 있음)

 

  • 경우에 따라서 쿼리문의 일부가 변화해야 할 때 if OR choose 사용
<insert id="join" parameterType="member">
    INSERT INTO member(id,pw,name,email) 
        <choose>
            <when test="!email.equals('')">
                VALUES(#{id},#{pw},#{name},#{email})
            </when>
            <otherwise>
                VALUES(#{id},#{pw},#{name},'이메일 없음')
            </otherwise>
        </choose>

        <!-- <if test="!email.equals('')">
            VALUES(#{id},#{pw},#{name},#{email})
        </if>
        <if test="email.equals('')">
            VALUES(#{id},#{pw},#{name},'이메일 없음')
        </if> -->
</insert>

 

  • 변수와 문자열을 합치기 위해 쓰는 방법
ORACLE : '%' || #{keyword} || '%'
MS-SQL : '%'+#{keyword}+'%'
MYSQL : CONCAT('%',#{keyword},'%')

 

  • WHERE 태그를 사용하면 WHERE를 생략할 수 있음
<select id="list" parameterType="hashmap" resultType="member">
    SELECT id,pw,name,email FROM member 
    <where>	
    <if test="opt =='id' and !keyword.equals('') and keyword != null">
        id LIKE CONCAT('%',#{keyword},'%')
    </if>
    <if test="opt =='name' and !keyword.equals('') and keyword != null">
        name LIKE CONCAT('%',#{keyword},'%')
    </if>
    <if test="opt == 'email' and !keyword.equals('') and keyword != null">
        email LIKE CONCAT('%',#{keyword},'%')
    </if>
    </where>
</select>

 

  • 수정 시 여러개의 컬럼이 올 경우 set 태그를 사용하여 ','로 인한 문법오류 해결
<update id="update" parameterType="hashmap">
    UPDATE member 
        <set>
            <if test="!pw.equals('')">pw = #{pw},</if>
            <if test="!name.equals('')">name = #{name},</if>
            <if test="!email.equals('')">email = #{email}</if>
        </set>
</update>

 

  • IN or OR 사용 시 foreach 사용
<!-- <select id="multi" parameterType="list" resultType="member">
    SELECT * FROM member
        <where>
            <foreach collection="list" item="item" separator="OR">
                name = #{item}									
            </foreach>
        </where>
</select> -->

<!-- SELECT * FROM member WHERE name IN ('','','') -->
<select id="multi" parameterType="list" resultType="member">
    SELECT * FROM member
        <where>
            name IN
                <foreach collection="list" item="item" separator="," open="(" close=")">
                    #{item}									
                </foreach>
        </where>
</select>

'코딩도전기 > Spring Boot' 카테고리의 다른 글

Spring Boot - Properties / AOP(Interceptor)  (0) 2023.06.02
Spring Boot - REST API  (0) 2023.05.31
String Boot - Restful Service  (0) 2023.05.30
Spring Boot - Transaction / Connection Pool / CSS 일괄적용  (0) 2023.05.26
Spring Boot  (0) 2023.05.24