๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

MyBatis ๋ณธ๋ฌธ

Back_End/Spring

MyBatis

๊ธฐ๋Œ 2024. 3. 12. 18:34
728x90

 

๐Ÿ”ธ MyBatis 

  • ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ SQL๋ฌธ ํ˜น์€ ์ €์žฅํ”„๋กœ์‹œ์ € ๊ฒฐ๊ณผ ๊ฐ’์„ ์ž๋ฐ” ์˜ค๋ธŒ์ ํŠธ์— ์ž๋™๋งคํ•‘ํ•˜๋Š” ์„œ๋น„์Šค
  • ์ˆ˜๋™์ ์ธ JDBC ๋ฐฉ์‹์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ž‘์—… ์ฝ”๋“œ์™€๋Š” ๋‹ฌ๋ฆฌ ์ฟผ๋ฆฌ๊ฒฐ๊ณผ์™€ ์˜ค๋ธŒ์ ํŠธ ๊ฐ„์— ์ž๋™๋งคํ•‘์„ ์ง€์›
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™์„ ์œ„ํ•œ ์ž๋ฐ” ORM ํ”„๋ ˆ์ž„์›Œํฌ (Object-Relational Mapping)

๐Ÿ’ป ๊ตฌ์„ฑ์š”์†Œ

1. MapperConfig XML File : MyBatis ๋™์ž‘์„ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ์„ค์ •์„ ๊ณตํ†ต์œผ๋กœ ์ •์˜

2. Mapper XML File : ์‹คํ–‰ํ•  SQL๋ฌธ ๋ฐ ๋งคํ•‘์ •๋ณด๋ฅผ XML ๋ฐฉ์‹์œผ๋กœ ์ •์˜

3. Mapper Annotations : ์ž๋ฐ” ์ฝ”๋“œ ๋‚ด์—์„œ ์‹คํ–‰ํ•  SQL๋ฌธ ๋ฐ ๋งคํ•‘ ์ •๋ณด๋ฅผ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•ด ์ •์˜

4. Parameter Object :  SQL ๋ฌธ์˜ ์กฐ๊ฑด์ ˆ์—์„œ ๊ฐ’์„ ๋น„๊ตํ•˜๊ฑฐ๋‚˜ , INSERT , UPDATE ๋“ฑ์— ํ•„์š”ํ•œ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•œ ์˜ค๋ธŒ์ ํŠธ

5. Result Object :  ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์•„ ๋ฆฌํ„ดํ•˜๊ธฐ ์œ„ํ•œ ์˜ค๋ธŒ์ ํŠธ


๐Ÿงฉ ์ฃผ์š”์†์„ฑ

โ‘  id

โ‘ก parameterType

  •  SQL ๊ตฌ๋ฌธ์‹คํ–‰์— ํ•„์š”ํ•œ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด์˜ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ , ํด๋ž˜์Šค๋ช… ํ˜น์€ ๋ณ„์นญ
  • parameterType ์œผ๋กœ ๊ฐ€๋Šฅํ•œ ๊ฐ’
    • ๊ธฐ๋ณธ์ž๋ฃŒํ˜•๊ณผ ๋ฌธ์ž์—ด     SQL ๊ตฌ๋ฌธ์— ์น˜ํ™˜ํ•  ๊ฐ’์ด ์˜ค์ง ํ•˜๋‚˜๋ฐ–์— ์—†๋Š”๊ฒฝ์šฐ
    • ์ž๋ฐ”๋นˆ์ฆˆ , Map    SQL ๊ตฌ๋ฌธ์— ์น˜ํ™˜ํ•  ๊ฐ’์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ 
                                     Map ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ parameterType="map"์œผ๋กœ ์„ค์ •ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค

โ‘ข resultType

  • ์กฐํšŒ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด์˜ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ ๋ฐ ํด๋ž˜์Šค๋ช… ํ˜น์€ ๋ณ„์นญ
  • ์กฐํšŒ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ–‰์ด์–ด๋„ ํ•œ ํ–‰์— ๋Œ€ํ•ด์„œ๋งŒ ๊ณ ๋ คํ•˜๋ฉด ๋œ๋‹ค
    • resultType ์œผ๋กœ ๊ฐ€๋Šฅํ•œ ๊ฐ’
    • ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•๊ณผ ๋ฌธ์ž์—ด → ์ปฌ๋Ÿผ์˜ ๊ฐฏ์ˆ˜๊ฐ€ 1๊ฐœ ์ผ๋•Œ , int , long , double , Date , string
    • ์ž๋ฐ”๋นˆ์ฆˆ → ์ปฌ๋Ÿผ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ผ ๋•Œ  , ์กฐํšŒ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ€๋ณ€์ ์ด์ง€ ์•Š์„ ๋•Œ
    • Map → ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•ด์•ผํ•  ๋•Œ
                    ์กฐํšŒ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ€๋ณ€์ ์ผ ๋•Œ
                    ํ…Œ์ด๋ธ” ๋ณ„๋กœ ์ž๋ฐ”๋นˆ์ฆˆ ์ •์˜์—†์ด ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์กฐํšŒํ•˜๊ณ  ์ €์žฅํ•  ๋•Œ

โ‘ฃ resultMap

  • ์กฐํšŒ๊ฒฐ๊ณผ์˜ ์ปฌ๋Ÿผ๋ช…๊ณผ ์ž๋ฐ”๋นˆ์ฆˆ์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ฅผ ๋งคํ•‘์‹œํ‚ค๋Š” resultMap ์„ ์ •์˜ํ•˜๊ณ 
    resultMap์— ์ •์˜๋œ resultMap์˜ ์•„์ด๋””๋ฅผ ์„ค์ •ํ•œ๋‹ค
    ๐Ÿ“Œ resultMap ์—์„œ๋Š”  <association> ๊ณผ  <collection> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด
          ๊ฐ์ฒด ํƒ€์ž… ๋˜๋Š” ์ปฌ๋ ‰์…˜ ํƒ€์ž…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งคํ•‘ ์‹œํ‚ฌ์ˆ˜ ์žˆ๋‹ค

 

๐Ÿ•น ์—ฌ๋Ÿฌ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ

public interface BookMapper {
 	void insertBook(Book book)	;
	List<Book> getBooksByPrice(@Param("min") int min , @Param("max") int max) ;
}
<insert id="insertBook" parameterType="vo.Book">

<select id="getBooksByPrice" resultType="vo.Book">
select * 
from books
where
 book_price between #{min} and #{max}
</select>

 


 

๐Ÿ“š ๋ณ€๊ฒฝ์‚ฌํ•ญ

IBatis์˜ Sql MapClient → SqlSession ๋ณ€๊ฒฝ

  • SqlSession ์ธํ„ฐํŽ˜์ด์Šค
    • MyBatis๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ธํ„ฐํŽ˜์ด์Šค๋กœ , SQL๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณต
    • ์‹คํ–‰ ๋ฉ”์„œ๋“œ , ํŠธ๋žœ์žญ์…˜ ์ œ์–ด ๋ฉ”์„œ๋“œ ๋“ฑ ํฌํ•จ
    • SqlSessionFactory ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด MyBatis Confiiiguration์ •๋ณด์— ํ•ด๋‹น , SqlSession ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
selectList()
selectOne()
insert()
update()
delete() 
commit() 
rollback()

 

  • ์–ด๋…ธํ…Œ์ด์…˜ ๋ฐฉ์‹ ์„ค์ • ๋„์ž…
    • MyBatis๋Š” ๋ณธ๋ž˜ XML ๊ธฐ๋ฐ˜์˜ ํ”„๋ ˆ์ž„์›Œํฌ , MyBatis 3.X ๋ถ€ํ„ฐ ์–ด๋…ธํ…Œ์ด์…˜ ๋ฐฉ์‹์˜ ์„ค์ •์„ ์ง€์›
    • Mapper XML File ๋‚ด SQL๋ฌธ ๋ฐ ๋งคํ•‘ ์ •๋ณด๋ฅผ ์ž๋ฐ”์ฝ”๋“œ ๋‚ด์—์„œ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๊ทธ๋Œ€๋กœ ์ ์šฉ ๊ฐ€๋Šฅ

๐Ÿ”’ IBatis ์™€ MyBatis

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™์„ ์œ„ํ•œ ์ž๋ฐ” ํ”„๋ ˆ์ž„์›Œํฌ

  • IBatis โ–ถ MyBatis ์ด๋ฆ„ ๋ณ€๊ฒฝ
  • IBatis์— ๋น„ํ•ด ์กฐ๊ธˆ ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์œ ์—ฐํ•œ XML ๊ตฌ์„ฑ์„ ์ œ๊ณต
  • MyBatis๋Š” ์ž๋™๋งคํ•‘ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•ด ํŽธ๋ฆฌํ•œ ๊ฐ์ฒด - ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋งคํ•‘์„ ์ œ๊ณต
  • ๋™์ ์ธ SQL ์ž‘์„ฑ - <if>  <choose>  <when>  <oterwise> ๋“ฑ์˜ ๋™์  SQL ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„ํŽธํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค

 


 

๐Ÿ–ฑ Maven pom.xml ์ถ”๊ฐ€ ๋ฐฉ์‹

<!-- iBATIS pom.xml -->
<dependency>
    <groupid>org.apache.ibatis</groupid>
   <artifactid>ibatis-sqlmap</artifactid>
   <version>2.3.4.726</version>
</dependency>

<!-- -->

<!-- MyBatis pom.xml -->
<dependency>
    <groupid>org.mybatis</groupid>
   <artifactid>mybatis</artifactid>
   <version>3.4.5</version>
</dependency>

 

๐Ÿ”ง NameSpace

๊ธฐ์กด iBatis์—์„œ๋Š” <sqlMap namespace = "ibatisDAO">์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ,
MyBatis์—์„œ๋Š” <mapper namespace = "com.gil.log.MybatisMapper">๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.


 

๐Ÿ”จ ์ฃผ์˜

์š”์†Œ ์œ ํ˜• "null"๊ณผ(์™€) ์—ฐ๊ด€๋œ "test" ์†์„ฑ์˜ ๊ฐ’์—๋Š” '<' ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.

if ํƒœ๊ทธ ์•ˆ์— ">"  ๊ด„ํ˜ธ๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ ๋Œ€์ฒด์‹์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

< lt; <if test="paraName1 lt; 0">
> gt; <if test="paraName1 gt; 0">
<=, =< lte; <if test="paraName1 lte; 0">
>=, => gte; <if test="paraName1 gte; 0">

 

 

 


๐Ÿงฉ ๊ฐœ๋ฐœ์ˆœ์„œ

1. ์˜์กด์„ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€
2. myBatis ํ™˜๊ฒฝ์„ค์ • ํŒŒ์ผ์ถ”๊ฐ€
3. Spring Bean Configuration ํŒŒ์ผ์— myBatis ๊ด€๋ จ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ๋นˆ์œผ๋กœ ๋“ฑ๋ก
4. Mapper ์ธํ„ฐํŽ˜์ด์Šค ์ถ”๊ฐ€

 

public interface DepartmentMapper {
	void insertDept(Dept dept);
    void deleteDeptById(int id);
    Dept getDeptById(int id);
    List<Dept> getAllDepts();
}
<mapper namespace = "kr.co.jhta.dao.DepartmentMapper>
    <insert id="insertDept" parameterType="kr.co.hjta.vo.Dept">
        insert into departments
        	(dept_id , dept_name , manager_id , loc_id)
        values
        	(#{id} , #{name} , #{managerId} , #{locID})
    </inssert>
    
    <delete id="deleteDeptById" parameterType="int">
        delete from departments
        where dept_id =  #{value}  
    </delete>
    
    <select id="getDeptByID" parameterType="int" resultType="kr.co.hjta.vo.Dept">
    </select>

    <select id="getAllDepts" resultType="kr.co.hjta.vo.Dept">
    </select>
</mapper>

 


โ› ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—‘์„ธ์Šค ํ•˜๊ธฐ

@Service
public class HrService {

    @Autowired
    private DeptMapper deptMapper;
}

 

 


๐ŸŽฎ MyBatis ๋™์ ์ฟผ๋ฆฌ

 

MyBatis๋Š” <if> , <choose> ~ <when> ~ <otherwise> , <where> , <set> , <foreach> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด
๋™์  SQL๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค

  • myBatis์—์„œ ๋™์  SQL ์ž‘์„ฑ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ์ œ๊ณตํ•˜๋Š” ํƒœ๊ทธ๋Š” JSTL์˜ ํƒœ๊ทธ์™€ ์‚ฌ์šฉ๋ฒ•์ด ๋น„์Šทํ•˜๋‹ค

 

<if> 

<if test="์กฐ๊ฑด์‹">
	SQL ๊ตฌ๋ฌธ
</if>
// ์ œ์‹œ๋œ ์กฐ๊ฑด์‹์€ true / false ๋กœ ํŒ์ •๋˜๋Š” ์กฐ๊ฑด์‹์ด๋‹ค
// ์ œ์‹œ๋œ ์กฐ๊ฑด์‹์ด true๋กœ ํŒ์ •๋˜๋ฉด ํƒœ๊ทธ ๋‚ด๋ถ€์˜ SQL๊ตฌ๋ฌธ์ด ์‚ฌ์šฉ๋œ๋‹ค
<select id="getBooks" parameterType="dto.Criteria" resultType="vo.Book">
	select *
	from  books
	where book_status = 'sell'
            <if test="title != null">
                book_title = #{title}
            </if>
</select>

getBooks() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ title ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ null์ด ์•„๋‹ˆ๋ผ๋ฉด 
book_title = #{ title } ์ด๋ผ๋Š” ์กฐ๊ฑด๋ฌธ์ด SQL ์ฟผ๋ฆฌ์— ์ถ”๊ฐ€๋œ๋‹ค


<choose> ~ <when> ~ <otehrwise>

์—ฌ๋Ÿฌ ์กฐ๊ฑด๋ฌธ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด SQL ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•ด์ค€๋‹ค
๋‚ด๋ถ€์˜ <when>ํƒœ๊ทธ๋กœ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๋ฉฐ ๋ชจ๋“  ์กฐ๊ฑด์ด false ์ผ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  
<otherwise> ํƒœ๊ทธ๋ฅผ ์„ ํƒ์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

<choose>
    <when test ="์กฐ๊ฑด์‹1">
        SQL ๊ตฌ๋ฌธ
    </when>
    <when test = "์กฐ๊ฑด์‹2>
        SQL ๊ตฌ๋ฌธ
    </when>
    <when test = "์กฐ๊ฑด์‹3>
        SQL ๊ตฌ๋ฌธ
    </when>
    <otherwise>
        SQL ๊ตฌ๋ฌธ
    <otherwise>
</choose>

// ์ œ์‹œ๋œ ์—ฌ๋Ÿฌ ์กฐ๊ฑด ์ค‘์—์„œ ์กฐ๊ฑด์‹์ด true๋กœ ํŒ์ •๋˜๋Š” SQL ๊ตฌ๋ฌธ์ด ์‚ฌ์šฉ๋˜๊ณ  
// ์ œ์‹œ๋œ ๋ชจ๋“  ์กฐ๊ฑด์‹์ด false๋กœ ํŒ์ •๋˜๋ฉด otherwise์˜ SQL ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉ
<select id="getBooks" parameterType="dto.Criteria" resyltType="vo.Books">
select *
from  books
where book_status = 'sell'
    <choose>
        <when test="title != null">
            and book_title = #{title}
        </when>
        <when test="author != null" >
            and book_publisher = #{publish}
        </when>
    </choose>
</select<>

 


<where>

ํƒœ๊ทธ ๋‚ด๋ถ€์— <if> <choose> ~ <when> ~ <otherwise> , <foreach> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋™์  SQL ๊ตฌ๋ฌธ์ด ์žˆ์–ด์•ผํ•œ๋‹ค

<where>
	๋™์  SQL๊ตฌ๋ฌธ
</where>

// <where> ํƒœ๊ทธ๋Š” ๋ฐ˜๋“œ์‹œ <if> , <choose> , <otherwise> , <foreach> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”
// ๋™์  SQL ๊ตฌ๋ฌธ์ด ์œ„์น˜ ํ•ด์•ผํ•œ๋‹ค

 

where ํ‚ค์›Œ๋“œ ์ถ”๊ฐ€

<select>
select * 
from books
where book_status = 'sell'
    <if test ="title != null">
    	and book_title = #{title}
    </if>
</select>	
// where ํƒœ๊ทธ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 select * from books where book_status = 'sell'

๐Ÿ“Œ  title์˜ ๊ฐ’์ด null ์ด๋ฉด and book_title = #{title} ์ด ์ถ”๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค

 

<select>
select * 
from book
where
    <if test ="title != null">
        and book_title = #{title}
    </if>
</select>	
// title ๊ฐ’์ด null์ด๋ฉด ์‹คํ–‰๋˜๋Š” SQL์€ ์•„๋ž˜ ์™€ ๊ฐ™๋‹ค

 select * from books where

๐Ÿ“Œ title์˜ ๊ฐ’์ด null ์ด๋ผ๋ฉด where ์ดํ›„ ๋ฌธ์žฅ์ด ์—†์–ด ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

<select>
select * 
from  books
<where>
    <if test ="title != null">
      	and book_title = #{title}
    </if>
</where>
</select>	
// title ๊ฐ’์ด null ์ด๋ฉด ์‹คํ–‰๋˜๋Š” SQL
select * from books

// title ๊ฐ’์ด null์ด ์•„๋‹ˆ๋ฉด ์‹คํ–‰๋˜๋Š” SQL
select * from books where book_title = ?

๐Ÿ“Œ ์•ž์— ๋ณด์—ฌ์ค€๊ฒƒ๊ณผ ๊ฐ™์€ ์ฟผ๋ฆฌ์— <where> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด title์˜ ๊ฐ’์ด null ์ผ ๋•Œ๋„ 

  • title ๊ฐ’์ด null์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” book_title = #{title} ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋˜์–ด ์‹คํ–‰
  • title ๊ฐ’์ด null์ธ ๊ฒฝ์šฐ์—๋Š” <if> ์กฐ๊ฑด๋ฌธ์ด ๋ฌด์‹œ๋˜๊ณ  ์ „์ฒด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰


 <where> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ์กฐ๊ฑด์— ๋”ฐ๋ผ SQL ์ฟผ๋ฆฌ๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
<where> ํƒœ๊ทธ๋Š” ์กฐ๊ฑด์ด ๋น„์–ด์žˆ์„ ๋•Œ WHERE ์ ˆ์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ ,
์กฐ๊ฑด์ด ์กด์žฌํ•  ๋•Œ๋Š” WHERE ์ ˆ๊ณผ ์กฐ๊ฑด์„ ํ•จ๊ป˜ ์ƒ์„ฑํ•œ๋‹ค

and ํ‚ค์›Œ๋“œ ์ œ๊ฑฐ

<where> ํƒœ๊ทธ๋Š”  ์ฟผ๋ฆฌ๋ฌธ์— where ํ‚ค์›Œ๋“œ ๋ฐ”๋กœ ๋’ค์— ์˜ค๋Š” and ํ‚ค์›Œ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค

<select>
select *
from books
where
    <if test ="title != null">
        book_title = #{title}
    </if>
    <if test = "author != null">
        and book_autohr = #{author}
    </if>
    <iftest = "publisher != null">
        and book_publisher = #{publisher}
    </if>
</select>

 

 

title ์ด null 
author ์ด๋‚˜ publisher๊ฐ€ null ์ด ์•„๋‹ˆ๋ผ๋ฉด
// title ์ด null , author ์ด๋‚˜ publisher๊ฐ€ null ์ด ์•„๋‹ˆ๋ฉด
select *
from books 
where 
and book_autohr = #{author}

// where ๋‹ค์Œ์— and๊ฐ€ ์žˆ์–ด์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

<where> ํƒœ๊ทธ์‚ฌ์šฉ

<select>
select *
from books
	<where>
        <if test ="title != null">
            book_title = #{title}
        </if>
        <if test = "author != null">
            book_autohr = #{author}
        </if>
        <if test = "publisher != null">
            book_publisher = #{publisher}
        </if>
    </where>
</select>

// <where> ํƒœ๊ทธ๋Š” and๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค

select * from books
where
	book_author = #{author}

<where> ํƒœ๊ทธ๋กœ ์ธํ•ด where ๋’ค์— ์˜ค๋Š” and ํ‚ค์›Œ๋“œ๊ฐ€์ œ๊ฑฐ๋œ๋‹ค

 


<SET> 

<SET> ํƒœ๊ทธ๋Š” ์ฟผ๋ฆฌ์—์„œ ๋™์ ์œผ๋กœ SET ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ํ•„์š”์—†๋Š” ์ฝค๋งˆ(,)๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค

<update>
    update books
    <set>
        <if test ="title != null">
			book_title = #{title},
        </if>
        <if test ="author!= null">
        	book_author = #{author},
        </if>
        <if test ="price != null">
        	book_price = 0 " ,
        </if>
        <if test ="filename!= null">
        	book_filename = #{filename},
        </if>
    </set>
    where book_no = #{no}
</update>

 

 

update books
set
    book_title= #{title},
    book_author = #{author},
    book_price = #{price}
where
	book_no = #{no}

 

EX

1. title์ด null์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” book_title = #{title}์„ SET ์ ˆ์— ์ถ”๊ฐ€ํ•˜๊ณ , 
2. author๊ฐ€ null์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” author = #{author}์„ SET ์ ˆ์— ์ถ”๊ฐ€ํ•œ๋‹ค
3.์„ค์ •๋œ SET ์ ˆ์€ ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋ฉฐ, null์ธ ์ปฌ๋Ÿผ์€ ์ œ์™ธ๋œ๋‹ค.
4. ์ฝค๋งˆ(,)๋„ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์–ด ์ฝค๋งˆ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ์ œ์™ธ๋œ๋‹ค

 


<foreach> 

<foreach> ํƒœ๊ทธ๋Š” SQL ์ฟผ๋ฆฌ์—์„œ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜๋Š” ํƒœ๊ทธ๋‹ค
์ฃผ๋กœ 'IN'  ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ฝœ๋ ‰์…˜์— ๋Œ€ํ•œ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

<foreach item="a" index="b" collection="productNumbers" 
    open="("separator="m" close=") nullable="true">
</foreach>
// ์ปฌ๋ ‰์…˜์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜๋Š” ํƒœ๊ทธ
// in ์—ฐ์‚ฐ์ž์™€ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค
<delete id="deleteProducts">
delete from products
where product_no
in
<foreach index="index" item ="num" collection="productNumber"
    open="("separator=" , "close =")" >
    #{num}
</delete>
delete 
from products
where product_no 
in (102,104,105)

 

  1. collection : ๋ฐ˜๋ณต๋Œ€์ƒ์ด ๋˜๋Š ๊ฐ’์„ ๋‹ด๊ณ ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ๋ช… ํ˜น์€ ํŒŒ๋ผ๋ฏธํ„ฐ ์ง€์ •
  2. item : collection์—์„œ ์ง€์ •ํ•œ ๋ฐฐ์—ด ํ˜น์€ List์—์„œ ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ์ถ”์ถœ๋˜๋Š” ๊ฐ’์„ ๋‹ด์„ ๋ณ€์ˆ˜๋ช…
  3. index : collection์—์„œ ์ง€์ •๋œ ๋ฐฐ์—ด ํ˜น์€ List๋ฅผ ๋ฐ˜๋ณต์ฒ˜๋ฆฌ ํ• ๋•Œ ๋งˆ๋‹ค 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค ๊ฐ’์„ ๋‹ด์„ ๋ณ€์ˆ˜๋ช…์„ ์ง€์ •ํ•œ๋‹ค ์ •์˜๋งŒํ•˜๊ณ  sql ๊ตฌ๋ฌธ์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค
  4. open : <foreach> ํƒœ๊ทธ๋ฅผ ๋ฐ˜๋ณต์„ ์‹œ์ž‘ํ•˜๊ธฐ์ „์— sql ๊ตฌ๋ฌธ์— ์ถœ๋ ฅํ•  ์ปจํ…์ธ ๋ฅผ ์ง€์ •ํ•œ๋‹ค
  5. close : <foreach> ํƒœ๊ทธ๋กœ ๋ฐ˜๋ณต์ด ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ sql ๊ตฌ๋ฌธ์— ์ถœ๋ ฅํ•  ์ปจํ…์ธ ๋ฅผ ์ง€์ •ํ•œ๋‹ค
  6. separator : #{num}์œผ๋กœ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ’์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ตฌ๋ถ„๋ฌธ์ž ์ง€์ •
728x90

'Back_End > Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ioC , DI  (1) 2024.05.19
Spring MVC , Controller  (1) 2024.03.12
Spring AOP  (0) 2024.03.12
๋นŒ๋“œ๋„๊ตฌ Maven  (0) 2024.03.11
Spring Framework  (0) 2024.03.11