1. SqlSessionManager
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionManager {
static SqlSessionFactory sqlSession;
static {
String resource="com/smhrd/database/mybatis-config.xml";
Reader reader;
try {
reader = Resources.getResourceAsReader(resource);
sqlSession = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSession() {
return sqlSession;
}
}
2. Mybatis-config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- Mapper.xml파일안에 parmeterType, resultType 지정하게 될대
alias를 줘서 보다 간단하게 코드를 작성할 수 있게 도와주는 방법 -->
<typeAliases>
<typeAlias type="com.smhrd.model.MemberVO" alias="MemberVO"/>
<typeAlias type="com.smhrd.model.BoardVO" alias="BoardVO"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- DB연결 설정 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="hr"/>
<property name="password" value="hr"/>
</dataSource>
</environment>
</environments>
<!-- Mapper 파일(SQL 쿼리문을 가지고 있는 파일) 설정하기 -->
<!-- 파일이 여러개 생성될 수 있다. -->
<mappers>
<mapper resource="com/smhrd/database/MemberMapper.xml"/>
<mapper resource="com/smhrd/database/BoardMapper.xml"/>
</mappers>
</configuration>
3. MemberMapper
<?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="com.smhrd.model.DAO">
<insert id="memberinsert" parameterType="MemberVO">
insert into message
values(#{email},#{pw},#{phone},#{address})
</insert>
<select id="memberLogin" parameterType="MemberVO" resultType="MemberVO">
select * from message where email=#{email} and pw=#{pw}
</select>
<update id="memberUpdate" parameterType="MemberVO">
update message set pw=#{pw}, phone=#{phone}, address=#{address} where email=#{email}
</update>
<select id="memberSelect" resultType="MemberVO">
select * from message
</select>
<delete id="memberDelete" parameterType="MemberVO">
delete from message where email=#{email}
</delete>
</mapper>
4. BoardMapper
<?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="com.smhrd.model.BoardDAO">
<insert id="boardInsert" parameterType="BoardVO">
insert into messageboard
values(num_board.nextval,#{title},#{writer},#{filename},#{content},sysdate)
</insert>
<select id="boardSelect" resultType="BoardVO">
select * from messageboard order by b_day desc
</select>
<select id="boarddetail" resultType="BoardVO">
select * from messageboard where num = #{num}
</select>
<delete id="boarddelete" parameterType="String">
delete from messageboard where num=#{num}
</delete>
</mapper>