1. DataSource 설정

  • pom.xml에 spring-jdbc 추가(https://mvnrepository.com/artifact/org.springframework/spring-jdbc)
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.22.RELEASE</version>
</dependency>
  • servlet-context.xml에 bean 추가
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="com.sql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/spirng"></property>
    <property name="username" value="spring"></property>
    <property name="password" value="spring"></property>
</bean>

2. Properties 파일 생성

  • servlet-context.xml에 property 설정파일 경로 추가
<!-- property 설정파일 경로-->
<context:property-placeholder location="classpath:config/database.properties"/>
  • database.properties 파일 생성(경로: src\main\resource\config\database.properties)
db.driverClass=com.sql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/spirng
db.username=spring
db.password=spring
  • servlet-context.xml에 bean 수정
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="${db.driverClass}"></property>
    <property name="url" value="${db.url}"></property>
    <property name="username" value="${db.username}"></property>
    <property name="password" value="${db.password}"></property>		
</bean>