1. 외부 DB 설정 값 읽어옴
<!-- 위 코드의 구성은 PropertyPlaceholderConfigurer클래스를 사용하여 프로퍼티 파일에서 값들을 읽어와 빈(bean)에 설정하는 구성입니다. -->
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>/WEB-INF/config/jdbc/jdbc.properties</value>
</property>
</bean>
2. 읽어온 DB값 커넥션 풀에 저장 , 연결 정보 설정
<!-- 위 코드로 생성된 dataSource 빈은 MyBatis에서 사용되어 데이터베이스와의 연결을 관리합니다.
외부 프로퍼티 파일에서 읽어온 값들을 사용하여 데이터베이스 연결 설정을 동적으로 구성할 수 있습니다.
이 빈이 사용할 클래스는 PooledDataSource 입니다.
PooledDataSource는 MyBatis에서 제공하는 데이터 소스 중 하나로 커넥션 풀 역할을 합니다. -->
<bean id="dataSource"
class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<property name="driver"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 위 코드로 생성된 sqlSessionFactory 빈은 MyBatis에서 SQL세션을 생성하는데 사용됩니다.
SQL세션은 데이터베이스와의 상호작용을 담당하며, MyBatis를 통해 정의된 SQL 쿼리를 실행하는데 사용됩니다.
즉! Mybatis에서의 세션범위는 트랜잭션이다. -->
3. sql 세션 생성 코드 하나하나 뜯어봐야돼서 아래에 기입
<bean id ="sqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:mybatis/models/modelConfig.xml" />
<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml" />
</bean >
<!-- 이 코드는 Spring에서 데이터베이스 트랜잭션을 관리하기 위한 DataSourceTransactionManager를 빈으로 등록하는 설정입니다. -->
<bean id ="transactionManager"
class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name ="dataSource" ref= "dataSource"></property >
</bean >
<!--SqlSessionTemplate은 SqlSessionFactory를 생성자로 받아 MyBatis의 SqlSession을 관리하며, Spring의 트랜잭션과 연동하여 사용할 수 있도록 합니다.
SqlSessionFactory-> SQL세션생성
SqlSessionTemplate -> SqlSessionFactory를 생성자로 받아 Mybatis의 SQL세션을 관리하며 Spring의 트랜잭션과 연동하여 사용할 수 있도록 합니다. -->
<bean id ="sqlSession"
class= "org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg ref= "sqlSessionFactory"></constructor-arg >
</bean >
</beans>
2.dataSource
MyBatis가 데이터베이스에 연결하는 데 필요한 정보를 제공합니다.
3.configLocation
MyBatis 설정 파일의 위치를 지정
4.mapperLocations
MyBatis 매퍼 파일들의 위치를 지정합니다. 매퍼 파일에는 SQL 쿼리와 결과 매핑 정보가 포함되어 있습니다.
5.SqlSessionTemplate:
Spring의 트랜잭션 관리 기능과 통합되어, 데이터베이스 작업을 트랜잭션 범위 내에서 수행합니다.
흐름 정리)
SqlSession이 생성되면 다음과 같은 과정을 통해 JDBC와 연결되고, MyBatis 설정 파일(4)을 읽고, 매퍼 파일(5)을 찾으며, 이 모든 작업은 트랜잭션 범위 내에서 이루어집니다.
사용자의 요청이 들어오면 sql template 클래스가 요청이 들어올 때 마다 sql session생성해준다.
위에 까지가 흐름이고 이제 요청이 들어오면 interceptor -> controller -> service -> DAO -> DB - 흐름으로 data가 움직인다.
root-context -> mybatis-context.xml -> mybatis/models/modelConfig.xml
modelConfig.xml
<?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.attiWell.goods.vo.GoodsVO" alias="goodsVO" />
<typeAlias type="com.attiWell.goods.vo.ImageFileVO" alias="imageFileVO" />
<typeAlias type="com.attiWell.member.vo.MemberVO" alias="memberVO" />
<typeAlias type="com.attiWell.cart.vo.CartVO" alias="cartVO" />
<typeAlias type="com.attiWell.order.vo.OrderVO" alias="orderVO" />
<typeAlias type="com.attiWell.mypage.vo.MyPageVO" alias="mypageVO" />
</typeAliases>
</configuration>
실제 vo가지고 와서 별명 설정으로 간편하게 mapping하기 위함
public List<GoodsVO> selectGoodsList(List<CartVO> cartList) throws DataAccessException {
List<GoodsVO> myGoodsList;
myGoodsList = sqlSession.selectList("mapper.cart.selectGoodsList", cartList);
return myGoodsList;
}
여기서는 selectGoodsList 를 찾는다.
root-context -> mybatis-context.xml -> mybatis/mappers/*.xml
예를들어 cart.xml 파일을 보면
<?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="mapper.cart">
<!-- 리절트 맵 정의 -->
<resultMap id="cartResult" type="CartVO">
<result property="cart_id" column="cart_id" />
<result property="goods_id" column="goods_id" />
<result property="member_id" column="member_id" />
<result property="cart_goods_qty" column="cart_goods_qty" />
<result property="creDate" column="creDate" />
</resultMap>
<resultMap id="goodsResult" type="GoodsVO">
<result property="goods_id" column="goods_id" />
<result property="goods_title" column="goods_title" />
<result property="goods_price" column="goods_price" />
<result property="goods_status" column="goods_status" />
<result property="goods_sales_price" column="goods_sales_price" />
<result property="goods_delivery_price" column="goods_delivery_price" />
<result property="goods_delivery_date" column="goods_delivery_date" />
<result property="goods_fileName" column="fileName" />
<result property="goods_sort" column="goods_sort" />
<result property="goods_intro" column="goods_intro" />
</resultMap>
<select id="selectCartList" parameterType="cartVO" resultMap="cartResult" >
<![CDATA[
select cart_id, goods_id, member_id, cart_goods_qty, creDate
from t_shopping_cart
where member_id=#{member_id}
]]>
</select>
<select id="selectGoodsList" resultMap="goodsResult" parameterType="java.util.Map">
<![CDATA[
select g.*,d.fileName from t_shopping_goods g, t_goods_detail_image d
where g.goods_id=d.goods_id
and d.filetype='main_image'
and g.goods_id in
]]>
<foreach item="item" collection="list" open="(" separator="," close=")">
#{item.goods_id}
</foreach>
order by g.goods_creDate desc
</select>
<select id="selectCountInCart" resultType="String" parameterType="cartVO">
<![CDATA[
select decode(count(*), 0, 'false', 'true') from t_shopping_cart
where goods_id=#{goods_id}
and member_id=#{member_id}
]]>
</select>
<insert id="insertGoodsInCart" parameterType="cartVO">
<![CDATA[
insert into t_shopping_cart(cart_id,
goods_id,
member_id)
values(#{cart_id},
#{goods_id},
#{member_id})
]]>
</insert>
<update id="updateCartGoodsQty" parameterType="cartVO">
<![CDATA[
update t_shopping_cart
set cart_goods_qty=#{cart_goods_qty}
where member_id=#{member_id}
and goods_id=#{goods_id}
]]>
</update>
<delete id="deleteCartGoods" parameterType="int">
<![CDATA[
delete from t_shopping_cart
where cart_id=#{cart_id}
]]>
</delete>
<select id="selectMaxCartId" resultType="int">
<![CDATA[
select nvl(max(cart_id), 0) + 1 from t_shopping_cart
]]>
</select>
</mapper>
위에서 selectGoodsList 찾아서 sql문 실행