<mapper namespace="com.fastcampus.ch4.dao.UserMapper">
<insert id="insert" parameterType="com.fastcampus.ch4.domain.UserDto">
INSERT INTO user_info
VALUES ( #{id}, #{pwd}, ... );
</insert>
</mapper>
private SqlSession session;
private static String namespace = "com.fastcampus.ch4.dao.UserMapper.";
@Override
public int insert(User user) {
return session.insert(namespace+"insert", user);
}
(인터페이스)
(구현체) - mybatis-spring
thread-safe란?
1. BoardDao, UserDao 등 여러개 DAO가 동시에 SqlSessionTemplate를 공유 가능하다!
2. 멀티쓰레드에 안전하다
(in root-context.xml)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
<insert id="insert" parameterType="com.fastcampus.ch3.domain.BoardDto">
INSERT INTO board
(title, content, writer)
VALUES
(#{title}, #{content}, #{writer})
<!-- BoardDto 객체 값들이 #{}에 매핑됨! getter()이용 -->
</insert>
<select id="select" parameterType="int"
resultType="com.fastcampus.ch3.domain.BoardDto">
<!-- int : Java.Lang.Integer -->
SELECT bno, title, content, writer, view_cnt, comment_cnt, reg_date
FROM board
WHERE bno = #{bno}
<!-- #{} : parameterType 객체가 매핑됨 -->
</insert>
<typeAliases>
로 이름 짧게 하기<typeAliases>
<typeAlias alias="BoardDto" type="com.fastcampus.ch4.domain.BoardDto"/>
</typeAliases>
<select id="select" parameterType="int" resultType="BoardDto">
SELECT bno, title, content, writer, view_cnt, comment_cnt, reg_date
FROM board
WHERE bno = #{bno}
</insert>
순수 Data Access 기능 (DAO)
조회, 등록, 수정, 삭제
예외처리 (예외 되던지기)
1) Service 계층
2) Controller (무조건 컨트롤러로 넘기면 됨)
3) 둘 다
<![CDATA[
UPDATE board
SET title = #{title}
where bno = #{bno} and bno <> 0
]]>