999.포트폴리오 만들기

Maven+Spring+Mybatis+Bootstrap 사용하여 개발하기

 

 

 1. Perspective 'Spring'인 상태에서 Spring Legacy Project 선택하기

 

 2. Spring MVC Project 선택하고 패키지는 3단계로 적어준 후 'Finish' 

 

3. pom.xml 에 mybatis 의존성 부분 설정하기

pom.xml 에 아래 내용을 작성하면 프로젝트에 자동으로 mybits.jar 라이브러리가 추가된다. 이것이 바로 Maven을 사용하면 좋은 점!

/Book_SpringMybatis/pom.xml

1
2
3
4
5
6
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.1</version>
        </dependency>    
cs

 

 

의존성 관련 소스 구하는 방법은 아래 사이트에서.

소스는 www.mvnrepository.com 에서 찾을 수 있다.

(캡쳐이미지의 spring 관련 부분은 무시)

 

pom.xml Dependencies 탭에서 mybatis 3.3.1 추가된 것이 확인된다.

 

4. servlet-context.xml 확인

소스는 resources 폴더에, 뷰는 컨트롤러가 요청하면 머리말, 꼬리말 붙여서 찾아가도록 설정한다.

/Book_SpringMybatis/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

1
2
3
4
5
6
7
8
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
 
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
cs

 

5. web.xml <filter> 추가하기

한글처리 때문에 해줘야 한다.

/Book_SpringMybatis/src/main/webapp/WEB-INF/web.xml

1
2
3
4
5
6
7
8
9
10
11
12
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>  
cs

 

 

6. 패키지와 중요 파일들 만들기

프로젝트 만들 때 com.mycompany.mybook 3단계 레벨로 패키지를 만들었다.
com.mycompany 밑에 config, model, util 패키지를 추가로 만들고,
해당 패키지에 알맞은 파일들을 새로 만든다.

com.mycompany.

config.

 - MybatisManager.java
 - Configuration.xml
 - db.properties
 - MapperBook.xml
 - MapperAuthor.xml
 - MapperPublishCompany.xml
 - MapperTranslator.xml

model.

 - Book.java
 - BookDAO.java
 - BookDAOImpl.java
 - Author.java
 - AuthorDAO.java
 - AuthorDAOImpl.java
 -

mybook.

 - BookController.java

util.

 - PagingAction.java

config 부분부터 소스를 하나하나 보자면 아래와 같다.

 

1. 마이바티스

  • SqlSessionFactory 선언
  • 리소스 Configuration.xml 경로 명시하고 있다.

/Book_SpringMybatis/src/main/java/com/mycompany/config/MybatisManager.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.mycompany.config;
 
import java.io.IOException;
import java.io.Reader;
 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
public class MybatisManager {
    
    public static SqlSessionFactory sqlMapper;
    
    static{
        String resource = "com/mycompany/config/Configuration.xml";
        Reader reader;
        
        try {
            reader = Resources.getResourceAsReader(resource);
            sqlMapper = new SqlSessionFactoryBuilder().build(reader);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static SqlSessionFactory getMapper(){
        return sqlMapper;
    }
 
}
cs

 

2. Configuration

  • db.properties 리소스 위치 명시 (url, username, password 등 관리하는 파일을 별도로 관리고 싶을 경우에...)

  • alias 선언

  • sql이 작성되어 있는 Mapper.xml 파일을 명시하고 있다.

/Book_SpringMybatis/src/main/java/com/mycompany/config/Configuration.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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>
    
    <properties resource="com/mycompany/config/db.properties"/>
    
    <typeAliases>
        <typeAlias type="com.mycompany.model.Book" alias="book"/>
        <typeAlias type="com.mycompany.model.Member" alias="member"/>
        <typeAlias type="com.mycompany.model.Admin" alias="admin"/>
        <typeAlias type="com.mycompany.model.Inventory" alias="inventory"/>
        <typeAlias type="com.mycompany.model.CodeGroup" alias="code"/>
        <typeAlias type="com.mycompany.model.Author" alias="author"/>
    </typeAliases>
    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/mycompany/config/MapperBook.xml"/>
        <mapper resource="com/mycompany/config/MapperMember.xml"/>
        <mapper resource="com/mycompany/config/MapperAdmin.xml"/>
        <mapper resource="com/mycompany/config/MapperInventory.xml"/>
        <mapper resource="com/mycompany/config/MapperCodeGroup.xml"/>
        <mapper resource="com/mycompany/config/MapperAuthor.xml"/>
    </mappers>
        
</configuration>
cs

 

3. DB접속정보

/Book_SpringMybatis/src/main/java/com/mycompany/config/db.properties

1
2
3
4
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=BOOK
password=RENTAL
cs

+ Recent posts