[MyBatis] MyBatis 환경설정 세팅
mybatis mapper.xml 작성방법에 대해 궁금하다면?
mybatis select 태그 작성방법에 대해 궁금하다면?
MyBatis란
SQL 매핑 프레임워크중에 하나로 분료되며 JDBC 코드의 복잡하고 중복되는 코드들을 좀더 편리하게 처리하기 위해 사용된다.
기본적인 MyBatis 특징
- 자동으로 Connection close() 가능
- MyBatis 내부적으로 PreparedStatement 처리
- #{prop}와 같이 속성 지정하면 내부적으로 자동으로 처리
- 리턴 타입을 지정하는 경우 자동으로 객체 생성 및 ResultSet 처리
Mybatis 환경 설정
1) mybatis 라이브러리 추가 (pom.xml)
groupId/artifactId/version 정보 설정
2)SQLSessionFactory 추가 (root-context.xml / mybatis-context.xml)
Mybatis에서 가장 핵심적인 객체는 SQLSession이라는 존재와 SQLSessionFactory이다.
내부적으로 SQLSession을 생성하여 Connection을 생성하고 원하는 sql을 전달 리턴 받는 구조로 되어 있다.
보통은 root-context 파일에 작업을 해도 되지만 편리성을 위해 mybatis-context 파일을 만들었다.
-root-context.xml
-mybatis-context.xml
3) 실제로 작업을 수행할 쿼리문 작성(Mapper.xml)
-mapper.xml
기본적으로 preparedStatement의 쿼리문을 대신하는 역할인데 다른점은 db에 보내는 파라미터를 ? 대신 #{prop}형식으로 전송한다. mapper 설정은 다음 포스팅에 올릴예정
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="score">
<!-- namespace : 폴더와 같은 개념 -->
<insert id="insertScore" parameterType="com.sp.app.score.Score">
INSERT INTO score(hak, name, birth, kor, eng, mat)
VALUES(#{hak},#{name},#{birth},#{kor},#{eng},#{mat})
<!-- #{} : preparedstatement 에서 ? 로 처리하던것과 유사 -->
</insert>
<!-- 반복적으로 사용되는 구문을 따로 작성하여 쿼리에 추가할 수 있다. -->
<sql id="where-list">
<if test="condition=='hak'">
hak = #{keyword}
</if>
<if test="condition=='name'">
INSTR(name,#{keyword}) >=1<!-- gt 크다 / 띄어쓰기 금지> -->
</if>
<if test="condition=='birth'">
(TO_CHAR(birth,'YYYY-MM-DD')= #{keyword} OR TO_CHAR(birth,
'YYYYMMDD') = #{keyword} )
</if>
</sql>
<!-- select 타입만 resultType을 가지고 있다. -->
<select id="dataCount" parameterType="map" resultType="Integer">
SELECT NVL(COUNT(*),0) FROM score
<where>
<if test="keyword!=null and keyword !=''">
<include refid="where-list" />
</if>
</where>
</select>
<select id="listScore" parameterType="map"
resultType="com.sp.app.score.Score">
SELECT hak, name, TO_CHAR(birth, 'YYYY-MM-DD') birth, kor, eng, mat,
(kor+eng+mat) tot, (kor+eng+mat)/3 ave
FROM score
<where>
<if test="keyword!=null and keyword !=''">
<include refid="where-list" />
</if>
</where>
ORDER BY hak ASC
OFFSET #{offset} ROWS FETCH FIRST #{rows} ROWS ONLY
</select>
<select id="readScore" parameterType="String"
resultType="com.sp.app.score.Score">
SELECT hak, name, TO_CHAR(birth, 'YYYY-MM-DD') birth, kor, eng, mat
FROM score
WHERE hak= #{hak}
</select>
<update id="updateScore" parameterType="com.sp.app.score.Score">
UPDATE score SET name=#{name}, birth=#{birth}, kor=#{kor}, eng=#{eng},
mat=#{mat} WHERE hak=#{hak}
</update>
<delete id="deleteScore" parameterType="String">
DELETE FROM score WHERE hak = #{hak}
</delete>
<!-- collection 의 속성값(파라미터 타입 변수명)은 항상 list로 설정해야함 -->
<delete id="deleteList" parameterType="java.util.List">
DELETE FROM score WHERE hak IN
<foreach collection="list" index="index" item="hak" open="(" separator="," close=")">
#{hak}
</foreach>
</delete>
</mapper>
|
cs |
'DB > Mybatis' 카테고리의 다른 글
ibatis mybatis 차이점 총정리! (0) | 2023.05.16 |
---|---|
[마이바티스] mybatis # $ 사용법 (0) | 2023.05.16 |
mybatis select 태그 정리 (0) | 2023.05.15 |
[MyBatis] mapper.xml 작성법 알아보기 (0) | 2020.12.04 |
댓글