DB연동(MyBatis)
- SQL Mapper
- Java와 Sql(Xml)분리
- 자동으로 파라미터 값 매핑
- insert into student(a,b,c,d,e,f) values(변수,변수,…)
- 자동으로 결과값 객체로 매핑
- select * from student → StudentVO 매핑해서 리턴
구조
- 바깥에 mapper태그
- mapper안에 <select> <insert> <update> <delete>
- DAO SQL 실행할때(지정 방법)
- mapper 태그의 namespace, 태그의 id ⇒ namespace.id
- (student.all)
메서드
- selectList() : 여러개 (1개도 가능)
- selectOne() : 1개
- insert(), update(), delete()
실행시 : .메서드명 (” namespace.id ”[ , 파라미터 (객체) ] )
#{ 변수명 } - preparedstatement : ?처리
- where id = #{id} → where id=?
${ 변수명 } - statement : 그대로 처리
- where id = ${id} → where id = abc
- where id = ‘${id}’
iBatis와의 차이
#변수명# or $변수명$
동적SQL
- <where> : where 명령어를 추가/제외
- <if test =”조건”>SQL</if> : 조건식이 참이면 포함되는 SQL
- <sql> : 독립적으로 저장
- <include> : 포함
- <resultMap> : 결과
- <parameterMap> : 파라미터 a매핑
- <forEach>: 반복
//student.xml
<mapper namespace="student">
<select id="all" resultType="chapter07.StudentVO">
SELECT * FROM student
</select>
<select id="view" resultType="chapter07.StudentVO" parameterType="int">
SELECT * FROM student WHERE studno = ""
</select>
</mapper>
parameterType 은 원래 java.lang.Integer 혹은 Integer을 써야하는데 MyBatis에서 Alias를 걸어줘서 int로 사용해도 된다.
'Spring' 카테고리의 다른 글
[ Spring ] 트랜잭션 Transaction (0) | 2024.08.02 |
---|---|
[ Spring ] DI, IOC, AOP (0) | 2024.08.02 |
[ Spring ] 어노테이션 모음 (0) | 2024.08.02 |
[ Spring ] 스프링 기초, 의존성 주입 (0) | 2024.07.31 |