Day73 SSM项目 搭建框架

编码风格变换

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

猜你喜欢

转载自blog.csdn.net/qq_21953671/article/details/79965481