๐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
๐ SQLSession, SQLSessionFactory
1. SQLSessionFactorys๋ SQLSession๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋ธ๋ค.
2. SQLSession์ผ๋ก connection ์์ฑ, SQL์ง์, ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฑ์ ํ ์ ์๋ค.
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>

@Component
@AllArgsConstructor
public class SampleDAO {
//private DataSource dataSource;
private SqlSessionFactory sqlSessionFactory;
public void select(){
String sql = "select * from tbl_board where bno > ?";
try {
@Cleanup SqlSession session = sqlSessionFactory.openSession();
@Cleanup Connection connection=session.getConnection();
@Cleanup PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 4);
@Cleanup ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
resultSet.getInt(1);
resultSet.getString(2);
resultSet.getString(3);
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
๐ Mapper - 1 (annotation)
@Data
public class SampleDTO {
private int bno;
private String title;
}
public interface SampleDAO {
@Select("select * from tbl_board where bno > 4")
List<SampleDTO> select();
}
<mybatis-spring:scan base-package="com.company.dao" />
<!--
<context:component-scan base-package="com.company.dao">
</context:component-scan>
-->
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"})
public class Tests {
@Setter(onMethod_ = @Autowired)
private SampleDAO dao;
@Test
public void test1() throws Exception {
dao.select();
}
}

?????????????????
์ด๋ ธํ ์ด์ ์ฒ๋ฆฌ๋ฅผ ํ์์ผ๋ statement ์ค๋น(prepare), ์งํ(execute), ๋ฐํ(resultSet)๊น์ง๋ ์ดํดํ๊ฒ ๋ค.
DTO ํ๋์ DB์ปฌ๋ผ์ ์ด๋ฆ์ด ๊ฐ์์ ์์์ ๋ค์ด๊ฐ ๊ฒ์ผ๊น, ์น์์์ EL์ด getter ํ๋กํผํฐ๋ฅผ ์ด์ฉํ๋ ๊ฒ์ฒ๋ผ, setter์ ํ๋กํผํฐ์ ๊ฐ์์ ๋ค์ด๊ฐ ๊ฒ์ผ๊น?
select * from ์ผ๋ก ์ปฌ๋ผ 6๊ฐ๋ฅผ ๊ตฌํด์์ง๋ง, DTO์ ์๋ ๊ฒ๋ค๋ก๋ง ์ ์ฅ๋จ์ ํ์ธํ ์ ์๋ค.
์์์ List๋ก ๋ฆฌํด๋ ๊ฒ ์ธ์์ ์ด๋ค.
์ ์ฒด์ ์ผ๋ก conn, stmt, rs ๋ฐ return ๊ณผ์ ์ด ์๋ต๋์๋ค๊ณ ๋ณผ ์ ์๋ค.
context:component-scan + @Component ์ด ์๋

Q. ์ปค๋ฅ์
์ ์ด๋ป๊ฒ ๊ตฌํ์ง?
-> ์ ์ง sqlFactory๋ฅผ mybatis์์ ์์์ ์ธ ๊ฒ ๊ฐ๋ค.(์๊น ์์ ์ค์ตํ๋ฉด์ ์ด๋ด๊ฑฐ๋ฉด ์์ผ์ง ํ๋ค..)
-> ์์ ๊ณ ์๋ฌ๋๋ฉด ๋ด ๋ง์ด ๋ง๋ค.


Q. ํ๋๋ช ๊ณผ ์ปฌ๋ผ๋ช
@Data
public class SampleDTO {
private int bno;
private String title2;
public void setTitle(String title2) {
this.title2 = title2;
}
}
๐ Mapper - 2 (xml)
- pstmt.setXXX() ์ฒ๋ผ ์นํํ๊ณ ์ถ๋ค!!
- ๊ทธ ๋ฐ์ ๋ณต์กํ sql์ ์ฌ์ฉํ๊ธฐ ์ํจ
- ๋ฌผ๋ก , ์ด๋ ธํ ์ด์ , xml ๋ ๋ฐฉ์์ ํผ์ฉํ ์ ์์
public interface SampleDAO {
List<SampleDTO> select();
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.company.dao.SampleDAO">
<select id="select" resultType="com.company.dto.SampleDTO" >
select * from tbl_board
</select>
</mapper>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.company.dto.SampleDTO" alias="SampleDTO" />
</typeAliases>
</configuration>
<select id="select" resultType="SampleDTO" >
select * from tbl_board
</select>
<select id="select" resultType="SampleDTO">
<![CDATA[
select bno from tbl_board where bno > #{bno}
]]>
<!-- <, > ๋ฑ์ ํ๊ทธ๋ก ์ธ์ํ๊ธฐ ๋๋ฌธ์ <![CDATA[ ]]>๋ฅผ ์ฌ์ฉ -->
</select>
public interface SampleDAO {
List<SampleDTO> select(int bno);
//List<SampleDTO> select(SampleDTO dto);
}
@Test
public void test1() throws Exception {
dao.select(4);
/*
SampleDTO dto = new SampleDTO();
dto.setBno(4);
dao.select(dto);
*/
}
์ฃผ์ ์, ์ฃผ์ ๋ฐ ๋ ๋ค ๊ฐ๋ฅํ๋ค.
๊ฐ์ฒด๋ฅผ ๋ฃ์์ ๊ฒฝ์ฐ, getter ํ๋กํผํฐ๋ก ๋์ํ๋ค.(์คํ์๋ฃ)
๊ธฐ๋ณธ ์๋ฃํ์ด ๋ค์ด๊ฐ๋ ๊ฒฝ์ฐ ์กฐ๊ธ ๋ ๊ณต๋ถํด๋ณด์์ผ ํ ๊ฒ ๊ฐ๋ค.