ssm综合练习总结

jar包的总结

ssm框架

spring框架用到的jar包

spring            IOC DI AOP
spring-beans
spring-context
spring-core
spring-expression

spring-context-support

如果想用aop切面
aop切面有关的jar包
spring-aop
spring-sapect
aspectjweaver

aopaliance
spring事务管理
spring-tx

springmvc用到的jar包

spring-web
spring-webmvc

commons-logging ------spring框架依赖的
servlet ------ 底层servlet

fastjson --------- JSON转换工具类
fileupload
commons-io
------- 文件上传

spring和springmvc属于同一个公司,maven又会自动导入依赖的jar包。
spring+springmvc在maven中需要引入
其所依赖的jar包都标相同颜色了
spring-webmvc
javax.servlet-api
commons-logging
spring-context-support

用到aop切面才引入的jar
spring-sapect
aopaliance
用到事务才引入的jar
spring-tx
用到json数据转化才用到的jar
fastjson
用到文件上传才用到的jar
fileupload

mybaits用到的jar

连接数据库,jdbc基础上完成复杂的操作
mysql连接数据库用到的jar
mysql-connector-java
dbcp   c3p0   druid         不同的连接池用不同的jar
mybaits
pagehelper ------ 分页插件
log4j ------- 日志输出
在maven中需要引入的jar
mybaits
mysql-connector-java
c3p0
pagehelper
log4j

各个框架结合用到的jar

spring+springmvc 同一个公司不需要引入
spring+jdbc
spring-jdbc ----------- 两个结合使用
spring+mybaits
spring-orm
mybaits-spring
在maven中需要引入的jar
spring-jdbc
spring-orm
mybaits-spring

我所用到的jar包有以上这些

ssm框架中配置文件的详细解

web.xml中

tomcat启动时自动加载web.xml配置文件,tomcat启动时,如果需要自动加载spring,springmvc的配置文件,就需要在web.xml配置
spring:创建bean工厂 bean对象
springmvc:监听请求,处理请求,响应数据

指定spring配置文件位置,加载spring配置文件

	 <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-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--指定配置文件位置-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

上面的只要是ssm都是必须要配置的
post请求中文乱码问题解决方案

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

springmvc-servlet.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       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
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

定义注解扫描的包

必须的

<context:component-scan base-package="com.lanou.controller"></context:component-scan>

内部视图资源解析器 转发有效
可要可不要

  	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--拼接返回页面的前缀路径-->
        <property name="prefix" value="/jsp/"></property>
        <!--拼接返回页面的后缀路径-->
        <property name="suffix" value=".jsp"></property>
    </bean>

解决返回json数据乱码问题

   <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                        <value>text/plain;charset=UTF-8</value>
                        <value>application/xml;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

springmvc配置静态资源处理

springmvc处理不了静态资源的时候配置(我觉得也是可有可无,看你项目中是否有静态资源啦!但一般项目都会有静态资源)

<mvc:resources mapping="/static/**" location="/static/" ></mvc:resources>

处理文件上传

 <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
          p:defaultEncoding="UTF-8"
          p:maxUploadSize="5242880"
          p:uploadTempDir="file:/d:/temp"
    />

定义拦截器

	<mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/user/**"/>
            <bean class="com.lanou.interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

applicationContext.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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 
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd">

开启注解模式 可以省略不写,默认开启状态

<context:annotation-config></context:annotation-config>

定义spring组件扫描的包

<context:component-scan base-package="com.lanou.util,com.lanou.service,com.lanou.mapper"></context:component-scan>

开启aop注解功能

 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

加载db.properties配置文件

 <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

通过spring创建一个数据源(连接池)

 	<bean name="dataSource"
          class="com.mchange.v2.c3p0.ComboPooledDataSource"
          p:jdbcUrl="${jdbc.jdbcUrl}"
          p:driverClass="${jdbc.driverClass}"
          p:user="${jdbc.user}"
          p:password="${jdbc.password}">
    </bean>

bean jdbcTemplate spring框架结合jdbc时用的工具类

 <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

平台事务管理器

<bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

创建SqlSessionFactory

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--mybatis核心配置文件位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

Mapper接口所在包名,Spring会自动查找其下的Mapper

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lanou.mapper" />
    </bean>

通知

 <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 传播行为 -->
            <!-- REQUIRED:如果有事务,则在事务中执行;如果没有事务,则开启一个新的事物 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="transfer" propagation="REQUIRED" />
            <!-- SUPPORTS:如果有事务,则在事务中执行;如果没有事务,则不会开启事物 -->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>

启用事务注解模式

 <tx:annotation-driven></tx:annotation-driven>

配置事务织入过程

<aop:config>
        <!--切入点-->
        <aop:pointcut id="txPointCut" expression="execution(* com..service..*.*(..))" />
        <!--配置切入点和切面的关联-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

定义切面bean

<bean id="logAdvice" class="com.lanou.util.LogAdvice"></bean>
    <aop:config>
        <aop:pointcut id="pc"
                      expression="execution(* com.lanou.service.*.*(..))"/>
        <aop:aspect ref="logAdvice">
            <aop:around method="round" pointcut-ref="pc"></aop:around>
            <aop:before method="before" pointcut-ref="pc"></aop:before>
            <aop:after method="after" pointcut-ref="pc"></aop:after>
            <aop:after-returning method="afterReturning" pointcut-ref="pc"></aop:after-returning>
            <aop:after-throwing method="afterException" pointcut-ref="pc"></aop:after-throwing>
        </aop:aspect>
    </aop:config>

mybatis-config.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">

mybaits核心配置

<configuration></configuration>

settings

 <settings>
        <!--开启下划线到驼峰式命名法的自动映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--是否启用缓存 默认值true-->
        <setting name="cacheEnabled" value="true"></setting>
        <!--是否启用延迟加载功能  多对象关联-->
        <setting name="lazyLoadingEnabled" value="true"></setting>
        <!--是否积极加载所有属性-->
        <setting name="aggressiveLazyLoading" value="false"></setting>
        <!--是否使用自动生成主键-->
        <setting name="useGeneratedKeys" value="true"></setting>
        <!--配置mybatis日志输出-->
        <setting name="logImpl" value="LOG4J"></setting>
    </settings>

包起别名

 <typeAliases>
        <!--类起别名-->
        <!--
          <typeAlias type="com.lanou.pojo.ProductType" alias="ProductType"></typeAlias>
          <typeAlias type="com.lanou.pojo.ProductType" alias="ProductType"></typeAlias>
          <typeAlias type="com.lanou.pojo.ProductType" alias="ProductType"></typeAlias>
        -->
        <!--该包下所有的类统一起别名-->
        <package name="com.lanou.pojo"></package>
    </typeAliases>

引入 pageHelper插件

<plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--reasonable:分页合理化参数,默认值为false,直接根据参数进行查询。
              当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。-->
            <!--<property name="reasonable" value="true"/>-->
        </plugin>
    </plugins>

加载所有相关的sql mapper 映射文件

	<mappers>
        <mapper resource="com/lanou/mapper/ArAccountMapper.xml"/>
        <mapper resource="com/lanou/mapper/ArOrderMapper.xml"/>
    </mappers>

sql mapper 映射文件

文件头

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

命名空间

<mapper namespace="com.lanou.mapper.OrdersMapper"></mapper>

db.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mybaits
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

lo4j.properties

# Global logging configuration
log4j.rootLogger=TRACE, stdout
# MyBatis logging configuration...
log4j.logger.com.lanou.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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值