2024-04-24
DataSource
๋ DB ์ฐ๊ฒฐ(Connection) ์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํ JDBC ํ์ค ์ธํฐํ์ด์ค์
๋๋ค.
DriverManager
๋ฐฉ์์ ๋งค๋ฒ ์๋ก์ด ์ฐ๊ฒฐ์ ์์ฑ โ ๋นํจ์จ์ DriverManagerDataSource
์ฌ์ฉ (๊ฐ๋จ ์ค์ )<!-- Spring JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- MySQL ๋๋ผ์ด๋ฒ -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>9.2.0</version>
</dependency>
<!-- ์ปค๋ฅ์
ํ -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>6.3.0</version>
</dependency>
โ ๏ธ ์ฃผ์: MySQL Connector ๋๋ฝ ์ DB ์ฐ๊ฒฐ ๋ถ๊ฐ!
<!-- DataSource ๋น ๋ฑ๋ก -->
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/testDB"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<!-- ํจํค์ง ์ค์บ -->
<context:component-scan base-package="com.example.app.domain.dao" />
<context:component-scan base-package="com.example.app.domain.service" />
@Repository
public class MemoDaoImpl {
@Autowired
private DataSource dataSource1; // DataSource ์ฃผ์
/**
* ๋ฉ๋ชจ ๋ฐ์ดํฐ ์ฝ์
*/
public int insert(MemoDto memoDto) throws SQLException {
Connection con = dataSource1.getConnection(); // ์ปค๋ฅ์
ํ๋
PreparedStatement pstmt = con.prepareStatement(
"INSERT INTO tbl_memo VALUES (?, ?, ?, ?)"
);
pstmt.setInt(1, memoDto.getId());
pstmt.setString(2, memoDto.getText());
pstmt.setString(3, memoDto.getWriter());
pstmt.setTimestamp(4, Timestamp.valueOf(memoDto.getCreateAt())); // LocalDateTime ๋ณํ
return pstmt.executeUpdate();
}
}
@Service
public class MemoServiceImpl {
@Autowired
private MemoDaoImpl memoDaoImpl;
public boolean registraionMemo(MemoDto memoDto) throws SQLException {
int result = memoDaoImpl.insert(memoDto);
return result > 0; // ์ฑ๊ณต ์ฌ๋ถ ๋ฐํ
}
}
@Controller
@RequestMapping("/memo")
@Slf4j
public class MemoController {
@Autowired
private MemoServiceImpl memoServiceImpl;
@PostMapping("/add")
public void add_post(@Valid MemoDto dto, BindingResult bindingResult, Model model) throws Exception {
if(bindingResult.hasErrors()) {
for(FieldError error : bindingResult.getFieldErrors()) {
model.addAttribute(error.getField(), error.getDefaultMessage());
}
return; // ์ ํจ์ฑ ์คํจ ์ ์ค๋จ
}
boolean isAdded = memoServiceImpl.registraionMemo(dto);
}
@GetMapping("/ex")
public void ex1_1() throws FileNotFoundException {
throw new FileNotFoundException("ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค.");
}
}
@ExtendWith(SpringExtension.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
class DataSourceTests {
@Autowired
private MemoDaoImpl memoDaoImpl;
@Test
void testInsert() throws Exception {
int result = memoDaoImpl.insert(new MemoDto(1, "Test", "tester@example.com", LocalDateTime.now(), null));
assert(result == 1); // ์ฝ์
์ฑ๊ณต ์ฌ๋ถ ํ์ธ
}
}
Cannot convert value of type 'java.time.LocalDateTime' to required type 'java.sql.Timestamp'
โก๏ธ ํด๊ฒฐ:
pstmt.setTimestamp(4, Timestamp.valueOf(memoDto.getCreateAt()));
java.sql.SQLException: Parameter index out of range
โก๏ธ VALUES (?, ?, ?, ?)
์๋ฆฌ์ ์ฒดํฌ!
INFO : POST /memo/add... MemoDto(id=123, text=asdfasdf, writer=example@example.com, createAt=2025-04-24T17:11, dateTest=null)
dateTest=null : @InitBinder ์ฃผ์ ์ฒ๋ฆฌ๋ก ๋ฐ์ธ๋ฉ ์ ๋ ์ํ