Day73 SSM專案 搭建框架

神一樣的我發表於2018-05-10

編碼風格變換

1、程式設計風格:
在Eclipse上建立Web專案,預設會產生一個WebRootWEB-INFlib目錄,jar包複製到該目錄後會自動載入到Web App Libraries庫中
配置編碼格式:
 window-preferences 搜尋encoding  
                          workspace     textfile encoding  :utf-8
                          jsp files          encoding:   utf-8
                          其他預設為utf-8
UTF-8簡介:
UTF-88-bit Unicode Transformation Format)是一種針對Unicode的可變長度字元編碼,又稱萬國碼。由Ken Thompson於1992年建立。現在已經標準化為RFC 3629。UTF-816個位元組編碼Unicode字元。用在網頁上可以統一頁面顯示中文簡體繁體及其它語言(如英文,日文,韓文)。
2、配置檔案:
  applicationContext-dao.xml
  applicationContext-service.xml
  applicationContext-tx.xml
  jdbc.properties
 springmvc.xml
 log4J.properties
  mybatis.xml                      配置log4J   配置實體類別名
  web.xml
      在寫配置檔案時,ctrl+shift+t 搜尋需要的類名的全限定路徑。
3、包形式
 com.bjsxt.專案名.mapper
 com.bjsxt.專案名.service
 com.bjsxt.專案名.service.impl
 .......................4、SVN的使用。
使用方式一:直接使用eclipseSVN外掛
使用方式二:使用客戶端+eclipseSVN外掛   客戶端更新,然後在eclipse下建立專案進行開發。


疑問:  不使用依賴注入 
          沒有設定編碼過濾器的init.


其他知識:
網站: http://apache.org/
maven :http://mvnrepository.com/
所有的配置檔案在spring都有預設設定,如果自定義,就走預設設定

        樂觀鎖
        悲觀鎖

使用的SSM框架檔案:

使用的jar包:
aopalliance.jar
asm-3.3.1.jar
aspectjweaver.jar
cglib-2.2.2.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-logging-1.1.1.jar
commons-logging-1.1.3.jar
jackson-annotations-2.4.0.jar
jackson-core-2.4.1.jar
jackson-databind-2.4.1.jar
javassist-3.17.1-GA.jar
jstl-1.2.jar
kaptcha-2.3.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.30.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
standard-1.1.2.jar

applicationContext-dao.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="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">

       <!--配置資料來源檔案掃描  -->
       <context:property-placeholder location="classpath:jdbc.properties"/>
       <!--配置資料來源  -->
       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="${jdbc.driverClassName}"></property>
       <property name="url" value="${jdbc.url}"></property>
       <property name="username" value="${jdbc.username}"></property>
       <property name="password" value="${jdbc.password}"></property>
       </bean>
       <!--配置Mybatis的sqlSessionFactory  -->
       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation" value="classpath:mybatisConfig.xml"></property>
       </bean>
       <!--mapper包掃描  -->
       <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.bjsxt.carrental.mapper"></property>
       </bean>
    </beans>

applicationContext-service.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="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">
        <!-- 配置註解掃描service實現類的包,管理所有的service實現類物件 -->
    <context:component-scan base-package="com.bjsxt.carrental.service.impl" />
</beans>

applicationContext-tx.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"
    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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--配置代理模式為cglib  -->
        <aop:aspectj-autoproxy proxy-target-class="true" ></aop:aspectj-autoproxy>
    <!-- 配置事務管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 事務通知 -->
    <tx:advice id="advice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="ins*" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"/>
            <tx:method name="del*" isolation="DEFAULT" propagation="REQUIRED"/>
            <!-- 對於查詢,設定只讀事務,提高效率 -->
            <tx:method name="sel*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
            <tx:method name="getUsers*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!-- 事務切面 -->
    <aop:config>
        <!-- 切點 -->
        <aop:pointcut expression="execution(* com.bjsxt.carrental.service.*.*(..))" id="my"/>
        <aop:advisor advice-ref="advice" pointcut-ref="my"/>
    </aop:config>

</beans>

jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/carrental
jdbc.username=root
jdbc.password=root

log4j.properties:

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.bjsxt.carrental.mapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mybatisConfig.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>

    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <typeAliases>
        <package name="com.bjsxt.carrental.pojo"/>
    </typeAliases>

</configuration>

springmvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <!--配置註解掃描  -->
        <context:component-scan base-package="com.bjsxt.carrental.controller"></context:component-scan>
        <!--配置mvc註解驅動  -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!--配置靜態資源方式  -->
        <mvc:resources location="/WEB-INF/js/" mapping="/js/**"></mvc:resources>
        <mvc:resources location="/WEB-INF/css/" mapping="/css/**"></mvc:resources>
        <mvc:resources location="/WEB-INF/images/" mapping="/images/**"></mvc:resources>
        <mvc:resources location="/WEB-INF/My97DatePicker/" mapping="/My97DatePicker/**"></mvc:resources>
        <mvc:resources location="/WEB-INF/upload/" mapping="/upload/**"></mvc:resources>
        <!--配置自定義檢視解析器  -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/"></property>
        <property name="suffix" value=".jsp"></property>
        </bean>
          <!-- 配置攔截器  -->
         <mvc:interceptors>
                <mvc:interceptor>
                    <mvc:mapping path="/**"/>
                    <mvc:exclude-mapping path="/system/login"/>
                    <mvc:exclude-mapping path="/images/**"/>
                    <mvc:exclude-mapping path="/css/**"/>
                    <mvc:exclude-mapping path="/js/**"/>
                    <mvc:exclude-mapping path="/My97DatePicker/**"/>
                    <mvc:exclude-mapping path="/upload/**"/>
                    <bean id="my" class="com.bjsxt.carrental.interceptor.MyInter"></bean>
                </mvc:interceptor>
            </mvc:interceptors> 
        <!--配置上傳資源解析物件  -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="utf-8"></property><!--設定解析編碼格式  -->
                <property name="maxInMemorySize" value="1000000"></property><!--記憶體大小  -->
                <property name="maxUploadSize" value="10000000"></property><!--檔案大小  -->
         </bean> 
        <!--配置異常解析器  -->
        <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
        <props>
        <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">sizeLimit</prop>
        </props>
        </property>
        </bean>
        </beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <!-- 將/favicon.ico請求交給tomcat預設的servlet來處理 -->
    <!-- 由於這個對映在springmvc的servlet對映之前,所以這裡default servlet是可以獲取這個請求的 -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/favicon.ico</url-pattern>
    </servlet-mapping>
  <!--配置Spring  -->
  <!--配置applicationContext,xml的路徑  -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>
  <!--配置監聽器  -->
  <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
  <!--配置SpringMVC 的servlet  -->
  <servlet>
    <servlet-name>carrental</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置Spring MVC 的配置檔案路徑  -->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>carrental</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--配置編碼過濾器  -->
  <filter>
  <filter-name>encoding</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>
  <init-param>
 <param-name>forceEncoding</param-name>
 <param-value>true</param-value>
 </init-param>
  </filter>
  <filter-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

原始碼:
連結:https://pan.baidu.com/s/1UR89HfygpUxKAeeaSxh_SQ 密碼:eii4

相關文章