Spring + Mybatis(MSSQL) + Maven (Mybatis Generator) _ Jqgrid성공

2013. 11. 18. 20:43개발관련기록/Spring

반응형

내용을 작성하기 까지 참고한 참고 Url


http://blog.naver.com/PostView.nhn?blogId=gemini1202&logNo=140187909904

http://blog.kaludin.com/2717395

http://exhandler.tistory.com/7




Spring + Mybatis + MSSQL (Mybatis Generator)


위의 작업을 하기 전에 일단 Jquery 와 Jqgrid의 작업이 완료 된것을 확인하고 작업을 하자.

사실 여기 까지 진행하기 위해 수 많은 내용을 참고 하고 작업했다.

Spring의 이해가 완벽하지 않은 상태에서 구글링과 어마어마한 Test를 통해서 작업이 진행된 사항이다.

해당 내용이 완벽하게 성공이 되었기 때문에 내용을 Posting 한다.


일단 지금까지 발견한 오류를 잡는 부분을 살펴 보겠다.


Hello World의 기본 Template는 Spring Tool suite를 이용하여 만들었으며

Spring Project MVC 로 Template를 만들어 Jqgrid, Mybatis, maven 을 올리는 작업을 진행했다.


1. Mybatis Generator 에서 자동 생성시에 되지 않는 부분 문제 해결

 - sqljdbc4.jar가 필요하다.(Maven에 추가가 되지 않아 Classpath에 지정하여 문제를 해결 했다.)

 - sqljdbc.jar와 함께 있으면 오류가 난다.


2. jqgrid 를 불러 오지 못하는 현상 해결

 - servlet-context.xml에 아래의 코드가 누락되어 나타난 부분


1
2
<!-- css, images, js 가져 오는 부분 -->
    <resources mapping="/resources/**" location="/resources/" />


참고로 jqgrid의 부분을 설정하기 위해 resource의 형식을 아래 Project 구성으로 진행했다.

이게 맞는지 정확하게는 모르겠지만 내가 봤을때는 모든 구글링과 자가 Test를 통해 진행 된 부분이므로 가장 이상적이라는 판단이 든다. 참고로 DAO 라던가 Model이라던가 Mapper의 형식은 비교적 복잡하게 되어 있을 수 있다. 이 부분은 개인적인 개발 성향이 강하게 녹아 들수 있으므로 참고만 하기 바란다.


주의!!!! 아래 폴더 구성에서 잘못된 부분이 있다.

CSS 안에 Images 폴더를 만들어야 한다!!!!!!

안그러면 버튼들이 안보이는 현상 발생!!!




위의 구성으로 프로젝트를 만들었다.


내부 소스는 아래와 같다.




pom.xml 

 - setting 부분 소스

 - 여기다가 적으면 자동으로 jar 파일을 가져 온다.

하지만 mssql을 가져 오려면 그냥 classpath 등록.!

이게 최선이더라..


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// JavaScript Code<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wookjin</groupId>
    <artifactId>myapp</artifactId>
    <name>SpringTest01</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.6</java-version>
        <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
                
        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>    
        
        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>
 
        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
                
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    
        <!-- MSSQL -->
        <!-- dependency>
            <groupId>com.microsoft.sqlserver.jdbc</groupId>
            <artifactId>sqljdbc</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency> -->
        
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.1</version>
        </dependency>
 
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.3</version>
        </dependency>
        
         
         <!-- json 잭슨 라이브러리 -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.11</version>
        </dependency>
        
        <!-- json 라이브러리 -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20131018</version>
        </dependency>
        
        <!-- jsonview 사용 -->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib-ext-spring</artifactId>
            <version>1.0.2</version>
        </dependency> 
 
        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        
 
        
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
 


아래처럼 sqljdbc4를 추가 하고 classpath에 등록한다.

만약 이렇게 하지 않으면 Mybatis Generator가 동작하지 않는다.

오류 화면은 jdbc어쩌고 하면서 경고 창이 떴었다.

참고로 web-inf/lib 안에 sqljdbc.jar 랑 같이 있으면 JRE 버전이 드라이버를 제공하지 않는 다는 엉뚱한 오류를 뱉는다. 참고 바란다.





servlet-context.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
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
 
    <!-- 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>
    
    <!-- css, images, js 가져 오는 부분 -->
    <resources mapping="/resources/**" location="/resources/" />
    
    <context:component-scan base-package="com.wookjin.myapp" />
    
    
    
</beans:beans>
 




generatorConfig.xml


마우스 우측 버튼을 눌러 설정한다. 참고로 이 부분은 마켓에서 플러그 인을 다운 받을 수 없다.

web을 검색하면 다운 받는 방법과 자세한 설명이 포함되어 있으니 잘 찾아서 해 본다.

참고로 마우스 우측 버튼을 눌러 붉은 나비 모양이 보여지면 된다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
  <context id="context1" >
    <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
     connectionURL="jdbc:sqlserver://10.??.???.??:1433;DatabaseName=???????"
     userId="???"
     password="???" />
    <javaModelGenerator targetPackage="org.wook.entity.model" targetProject="SpringTest01" />
    <sqlMapGenerator targetPackage="org.wook.entity.xml" targetProject="SpringTest01" />
    <javaClientGenerator targetPackage="org.wook.entity.mapper" targetProject="SpringTest01" type="XMLMAPPER" />
    <table schema="????" tableName="scr" >
    </table>
    <table schema="????" tableName="so001" >
      <!--<columnOverride column="???" property="???" /> -->
    </table>
  </context>
</generatorConfiguration>



mybatis-config.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>
    <settings>
        <setting name="cacheEnabled" value="false" />
        <setting name="useGeneratedKeys" value="true" />
        <setting name="defaultExecutorType" value="REUSE" />
    </settings>
 
    <typeAliases>
        <typeAlias type="org.wook.entity.model.ScrExample" alias="ScrExample"/>
    </typeAliases>
    <mappers>
        <mapper resource="org/wook/entity/xml/ScrMapper.xml" />
    </mappers>
</configuration>



mybatis-context.xml

참고로 mybatis-config.xml, mybatis-context.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
 
    <!-- Message Source -->
    <bean id="rootMessageSource"
          class= "org.springframework.context.support.ReloadableResourceBundleMessageSource" >
          <property name= "basenames">
               <list>
                  <value>classpath:messages/core</value >
               </list>
          </property>
          <property name="fileEncodings" value="UTF-8" />
          <property name="defaultEncoding" value="UTF-8" />
    </bean>
         
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>
    
    <!-- JNDI 로 datasource를 받아오는 경우 -->
    <!--  
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${jdbc.datasource}"/>
    <property name="resourceRef" value="true" />
    </bean>
    -->
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="/WEB-INF/spring/mybatis/mybatis-config.xml"/>
    </bean>
    
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory"/>
    </bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" >
         <constructor-arg index= "0" ref="sqlSessionFactory" />
    </bean>
 
<!-- 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
         <property name= "basePackage" value="org.wook.entity.mapper" />
    </bean>
  -->
 
    <bean id="transactionManager"
         class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
         <property name= "dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    
<!-- =========================== DAO DEFINITIONS : IBATIS IMPLEMENTATIONS ============ -->    
 
       <bean id="ScrDaoImpl" class="com.wookjin.myapp.dao.ScrDaoImpl">                            
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    
    
    
   
</beans>


root-context.xml


1
2
3
4
5
6
7
8
9
10
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- Root Context: defines shared resources visible to all other web components -->
    <import resource="mybatis/mybatis-context.xml"/>
</beans>





MyFirstGrid.jsp

Jqgrid 가 나오는 페이지.



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
<link rel = "stylesheet" type = "text/css" media = "screen" href = "resources/css/jquery-ui-1.10.3.custom.min.css"/>
<link rel = "stylesheet" type = "text/css" media = "screen" href = "resources/css/ui.jqgrid.css"/>
 
<script src = "resources/js/jquery-1.9.0.min.js" type = "text/javascript"></script>
<script src = "resources/js/i18n/grid.locale-kr.js" type = "text/javascript"></script>
<script src = "resources/js/jquery.jqGrid.min.js" type = "text/javascript"></script>
 
<script>
    $(window.document).ready(function() {
        $("#grid").jqGrid({
            url : 'http://apis.daum.net/socialpick/search?output=json',
            caption : 'Daum SNS Pic',    // caption : 그리드의 제목을 지정한다.
            datatype : 'jsonp',               // datatype : 데이터 타입을 지정한다.
                                                    // (json 으로 외부에 요청을 보내면 보안정책에 위배되어 요청이 나가질 않는다.
                                                    //  따라서 datatype 을 jsonp로 변경하고 보내야 한다.)
 
            mtype : 'get',                     // mtype : 데이터 전송방식을 지정한다.
            height : '500px',                 // height : 그리드의 높이를 지정한다.
            pager : '#pager',               // pager : 도구 모임이 될 div 태그를 지정한다.
            rowNum : 3,                      // rowNum : 한 화면에 표시할 행 개수를 지정한다.
            loadonce : true,                // loadonce : rowNum 설정을 사용하기 위해서 true로 지정한다.
            rowList : [3, 6, 9, 10],       // rowList : rowNum을 선택할 수 있는 옵션을 지정한다.
 
            // colNames : 열의 이름을 지정한다.
            colNames : [ '순위''소셜픽키워드''이슈에 대한 링크 URL''요약문''검색수''트윗수''검색 결과의 페이지 번호''순위 상승/하락수''카테고리' ],
            colModel : [
                        { name : 'rank',            index : 'rank',                width : 40,        align : 'center' },
                        { name : 'keyword',         index : 'keyword',            width : 100,    align : 'left'   },
                        { name : 'link',             index : 'link',                width : 100,    align : 'left'   },
                        { name : 'content',         index : 'content',            width : 200,    align : 'left'   },
                        { name : 'count',             index : 'count',            width : 100,    align : 'center' },
                        { name : 'quotation_cnt',    index : 'quotation_cnt',    width : 100,    align : 'center' },
                        { name : 'comment_cnt',     index : 'comment_cnt',         width : 100,    align : 'center' },
                        { name : 'rank_diff',         index : 'rank_diff',         width : 100,    align : 'center' },
                        { name : 'category',         index : 'category',         width : 100,    align : 'center' }
                        ],
            
            // jqGrid 에서 default 형태의 JSON 이 아닌 소셜픽만의 고유의 json 형태로 날아오기에.
            // 소셜픽의 API 를 바꿀 순 없어, jqGrid 에서 json을 
            // customizing하여 사용하기 위해 jsonReader를 사용했다.
            jsonReader : {
                            repeatitems : false,
                            id : "rank",
                            root : function (obj) { return obj.socialpick.item; },
                            page : function (obj) { return 1; },
                            total : function (obj) { return 1; },
                            records : function (obj) {return obj.socialpick.item.length; }
            }
 
        // navGrid() 메서드는 검색 및 기타기능을 사용하기위해 사용된다.
        }).navGrid('#pager', {
            search : true,
            edit : true,
            add : true,
            del : true
        });
    });
</script>
<title>jqGrid</title>
</head>
<body>
     // jqGrid 플러그인을 사하기위한 table 태그와 div태그 사용
    <table id = "grid"></table>
    <div id = "pager"></div>
</body>
</html>



이 화면이 아래의 화면처럼 나온다.




web.xml

web의 시작을 나타내는 부분


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
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/spring/root-context.xml
        </param-value>
    </context-param>
    
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
        
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
</web-app>




jdbc.properties

DB 연결 정보 담고 있는 부분 4줄이면 된다.

1433은 Mssql 의 고유 Port 번호이다.


jdbc.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver

jdbc.url=jdbc:sqlserver://??????:1433;DatabaseName=?????

jdbc.username=?????

jdbc.password=?????




HomeController.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.wookjin.myapp;
 
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.wook.entity.mapper.ScrMapper;
import org.wook.entity.model.ScrExample;
 
import com.wookjin.myapp.dao.ScrDaoImpl;
 
/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    @Autowired
    private ScrMapper ScrMapper;
    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);
        
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        
        String formattedDate = dateFormat.format(date);
        ScrExample scre = new ScrExample();
        System.out.println("Test : " + ScrMapper.countByExample(scre));
        model.addAttribute("serverTime", formattedDate );
        
        //return "MyFirstGrid";
        return "MyFirstGrid";
    }
    
}
 



ScrDaoImpl.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.wookjin.myapp.dao;
 
import java.util.List;
 
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.wook.entity.mapper.ScrMapper;
import org.wook.entity.model.Scr;
import org.wook.entity.model.ScrExample;
 
//@Repository
public class ScrDaoImpl extends SqlSessionDaoSupport implements ScrMapper{
 
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
 
    @Override
    public int countByExample(ScrExample example) {
        // TODO Auto-generated method stub
        return (Integer) getSqlSession().selectOne("org.wook.entity.mapper.ScrMapper.countByExample" );
    }
 
    @Override
    public int deleteByExample(ScrExample example) {
        // TODO Auto-generated method stub
        return 0;
    }
 
    @Override
    public int insert(Scr record) {
        // TODO Auto-generated method stub
        return 0;
    }
 
    @Override
    public int insertSelective(Scr record) {
        // TODO Auto-generated method stub
        return 0;
    }
 
    @Override
    public List<Scr> selectByExample(ScrExample example) {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    public int updateByExampleSelective(Scr record, ScrExample example) {
        // TODO Auto-generated method stub
        return 0;
    }
 
    @Override
    public int updateByExample(Scr record, ScrExample example) {
        // TODO Auto-generated method stub
        return 0;
    }
 
}



나머지 java 소스는 Mybatis Generator로 만들어진 부분이다.


현재 까지 완료 된 부분은 아래와 같다.


Jqgrid/Jquery/Mybatis(Mssql)/Maven/Spring/DB연결해서 정보 가져 오기(Table count 값)


이제 Table에서 조회된 List 형식의 데이터를 Json으로 받고 Jqgrid로 뿌려 주는 부분까지 작업을 하면 기본적인 Setting이 끝날꺼 같다.


재미 있다.


반응형