<bean>
bean
๋ฑ๋ก(๊ฐ์ฒด๋ฑ๋ก) ํด์ค์.
- ํด๋์ค๋ฅผ ๊ฐ์ฒดํํ์ฌ
spring
์ด ๊ทธ ์ฃผ์๊ฐ๋ค์ ๊ฐ์ง๊ณ ์๊ณ ์์ฒญํ ๋ ๋ง๋ค ๊ฐ์ ธ๋ค ์ค์.
<property>
<property name="๋ฉค๋ฒํ๋" value ๋๋ ref="๋ฉค๋ฒํ๋์ ๊ฐ์ ๋ฃ์ด์ค๋ค">
setter
๋ฉ์๋๋ฅผ ์ฐ๋ ๊ฒ๊ณผ ๊ฐ์์.value
์์ฑ์ ๊ฐ์,ref
์์ฑ์ ๊ฐ์ฒด๋ฅผ ๋ฉค๋ฒํ๋์ ๋ด์์ค์.
<constructor-arg>
<constructor-arg value ๋๋ ref=""> </constructor-arg>
- ์์ฑ์์ ์ธ์๋ฅผ ๋ฃ๋ ๊ฑฐ์์.
- ์ด๋์๋
classpath
๋ก ์์ํ๋ฉดresources
ํด๋์์ ๋ถํฐ ์ฐพ๊ธฐ ์์ํด์.
<context:property-placeholder location="classpath:application.properties"/>
<beans></beans>
์์์ ์์ฑ๋ผ์.
<context:property-placeholder location="classpath:application.properties"/>
properties
ํ์ผ์ ์์น์ ํ์ผ์ด๋ฆ์ ์ ์ด์ค์.properties
ํ์ผ์๋ DB์ ๋ณด๊ฐ ์์ด์.
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="${db.driver}" />
<property name="jdbcUrl" value="${db.host}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
HikariConfig
๊ฐ์ฒด๋ฅผHikariDataSource
๊ฐ์ฒด ์์ฑ์์ ์ธ์๋ก ๋ฃ์ด์ค์.
MyBatis
์ค์ ํ๋ ๋ถ๋ถ
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.koreait.first" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<mybatis-spring:scan base-package="com.koreait.first"/>
- MyBatis๋ Mapper.interface์ Mapper.xml์ ์กฐํฉํ์ฌ DAOํด๋์ค๋ฅผ ๋ง๋ค์ด์.
- ๋ฐ์ดํฐ์์ค์
mapper
๋ฅผ ๋ชจ์์.- ๋ฉค๋ฒํ๋
dataSource
๋HikariDataSource
ํด๋์ค์ ๊ฐ์ฒด๋ฅผset
ํ์ด์.- ๋ฉค๋ฒํ๋
typeAliasePackage
์๋Mapper.interface
๊ฐ ์๋ ๊ฒฝ๋ก๋ฅผset
ํด์- ๋ฉค๋ฒํ๋
mapperLocations
์๋Mapper.xml
๊ฐ ์๋ ๊ฒฝ๋ก๋ฅผset
ํด์.
๋ญ๊ฐ
DB
์ ๋ํ ์ค์ ๋ง ํด๋์ ๊ณณ ๊ฐ์์.
๐ ๋งํฌ๋ฅผ ๋๋ฅด๋ฉด ์์ค๋ฅผ ๋ณผ ์ ์์ด์.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd">
<!--์ด๋์๋ classpath๋ก ์์ํ๋ฉด resourcesํด๋์์ ๋ถํฐ ์ฐพ๊ธฐ ์์ํ๋ค.-->
<context:property-placeholder location="classpath:application.properties"/>
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="${db.driver}" />
<property name="jdbcUrl" value="${db.host}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<!--Mybatis ์ค์ -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.koreait.first" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<mybatis-spring:scan base-package="com.koreait.first"/>
</beans>