2021. 2. 2. 23:44ㆍBACK END/SPRING
Album
mybatis와 album테이블을 사용하여 CRUD 작업 수행
주요 기능
1. 앨범목록 조회
2. 페이징 처리 기능
3. 필드 검색 기능
4. 앨범 등록과 수정 시 유효성 검사 수행, 파일업로드 가능
5. Mapper Interface 기능 사용
1. 앨범목록 조회
1) dbScript.txt 참고하여 db 테이블 생성
album테이블, checkes테이블 등 생성
2) pom.xml
데이터베이스 관련 항목, 오라클 관련 항목, mybatis 관련 항목 등 추가
<!-- 오라클 관련 항목들 -->
<repositories>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
DataSource 관련된 라이브러리를 참조하기 위함
<!-- 데이터 베이스 관련 항목들 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!-- mybatis 관련 항목 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
3) web.xml 서블릿 맵핑을 사용하여 DispatcherServlet 등록
<!-- album section -->
<servlet>
<servlet-name>album</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/album-context.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>album</servlet-name>
<url-pattern>*.al</url-pattern>
</servlet-mapping>
> 위 코드는 DispatcherServlet 정의 방법
> contextConfigLocation : MVC설정파일의 위치와 이름을 설정하고 있음
> *.al 은 확장자가 해당 요청에 대해서만 응답을 해주는 서블릿임
4) applicationContext.xml 스프링 설정 파일
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- DBCP(커넥션 풀링)를 사용한 DataSource 설정 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<!-- 이 데이터의 출처는 곰곰이 -->
<property name="username" value="gomgomi" />
<property name="password" value="gomgomi" />
</bean>
<!-- Mybatis 설정 (sqlSessionFactoryBean, SqlSessionTemplate) -->
<!-- dataSource 객체와 맵 설정 파일의 경로는 setterInjection을 이용하여 설정해주도록 합니다. -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/album/mybatis/SqlMapConfig.xml" />
</bean>
<!-- SqlSessionTemplate 객체는 setterInjection을 지원하지 않습니다. -->
<!-- 반드시 생성자 인젝션으로 처리해야 합니다. -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactoryBean">
</constructor-arg>
</bean>
</beans>
> dataSource객체
***교안p.350참고
사용하고자 하는 데이터베이스의 출처를 알려주는 객체
스프링 자체가 제공하는 것이 아닌, DBCP풀링 기능이 제공함
가장 많이 쓰이는 파일은 BasicDataSource이며,
이것을 사용하기 위해서 commons-dbcp를 pom.xml에 명시해야 함
***DBCP풀링 기능 : Apache Commons Database Connection Pool
> SqlSession : 실제 SQL을 실행하는 객체로 SQL을 처리하기 위해 JDBC드라이버를 사용
> SqlSessionFactoryBean객체 : Mybatis를 사용하는 경우에 구현
> SqlSessionTemplate 객체 : Mybatis를 사용하는 경우에 구현
SqlSession의 구현체, SqlSessionFactory를 생성자 주입시켜 생성함
'BACK END > SPRING' 카테고리의 다른 글
MVC 모델2 회원가입 예제 (0) | 2021.02.01 |
---|---|
MVC를 이용한 웹 요청 처리, Human Bean 회원가입 양식 만들기 (0) | 2021.01.28 |
MvcBasic 프로젝트 생성 (0) | 2021.01.28 |
20210126 Spring 복습 (0) | 2021.01.26 |
20210125 Spring (0) | 2021.01.25 |