1. 程式人生 > >學生成績管理系統-SSM實現(一)——配置檔案篇

學生成績管理系統-SSM實現(一)——配置檔案篇

學生成績管理系統-SSM實現(配置檔案的配置加註解)

(該專案使用maven管理)

  1. 所需jar包(pom.xml)
        <!-- jar包的版本-->
        <spring.version>4.3.9.RELEASE</spring.version>
        <aspectj.version>1.6.11</aspectj.version>
        <junit.version>4.12</junit.version>
        <mysql.version
>
5.1.38</mysql.version> <mybatis.version></mybatis.version> <mybatis-spring.version>1.3.1</mybatis-spring.version> <mybatis.version>3.4.4</mybatis.version> <jstl.version>1.2</jstl.version> <jackson.version
>
1.9.13</jackson.version> <fastjson.version>1.2.46</fastjson.version> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version
>
</dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- springframe end --> <!--mysql資料庫驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--使用AspectJ方式註解需要相應的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <!--使用AspectJ方式註解需要相應的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <!-- batis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- json所需jar start --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>${jackson.version}</version> </dependency> <!-- json所需jar end -->
2. spring.xml
    <!-- 可實現json字串自動轉化為實體類 -->
    <mvc:annotation-driven />

    <!-- 實體 json 自動對映轉化 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  
                <ref bean="mappingJacksonHttpMessageConverter" />  
            </list>  
        </property>  
    </bean>  
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>text/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>


    <!-- 啟用Spring對基於@AspectJ aspects的配置支援 -->
    <!-- 啟用自動代理功能 -->
    <aop:aspectj-autoproxy />

    <!-- 事務管理 註解開啟-->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Initialization for data source -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="" />
        <property name="username" value="" />
        <property name="password" value="" />
    </bean>

    <!-- Initialization for TransactionManager -->
    <!-- 開啟事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- mybatis的配置開始 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.mdy.student.bean" />
        <!--  mybatis的設定-->
        <property name="configLocation" value="classpath:orm/mybatis-config.xml" />
        <!--  mapper類的配置檔案的自動裝載-->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mdy.student.mapper" />
    </bean>
    <!-- mybatis的配置結束 -->
3.web.xml部分內容
<!-- 上下文監聽器 不啟用spring自動注入可能會失敗 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Student</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <!-- servlet的配置檔案載入 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- spring mvc 的配置-->
            <param-value>classpath*:spring/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Student</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
4.mvc-config.xml
<mvc:annotation-driven/>

    <context:component-scan base-package="com.mdy.student.web" />

<!-- js,css等靜態檔案的配置,否則會被spring的過濾器攔截-->
    <mvc:resources mapping="/js/**" location="/resources/js/" cache-period="#{60 * 60}" />
    <mvc:resources mapping="/css/**" location="/resources/css/" cache-period="#{60 * 60}" />
    <!--<mvc:default-servlet-handler />-->

<!-- jsp頁面資源的訪問配置,使用spring mvc時直接return jsp檔名-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 字首配置-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 字尾配置-->
        <property name="suffix" value=".jsp"/>
    </bean>

以上為該專案的配置檔案的具體情況