• SSM整合笔记


    spring 依赖注入

    1导入坐标
    2配置application文件
    配置组件扫描(注解扫描 一般不扫描controller层)
    (配置mybatis整合)
    配置aop 事物控制  
    3使用spring步骤
    代理模式 加载application文件(在web.xml中配置监听器 自动装配)
    (整合mybatis 可以把 jdbc文件 mybatis的映射文件 sqlsessionfactory都注入到spring中 在为mapper配置一个实现类 )

    <?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"
           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/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
        <!--    组件扫描 只扫描service 和 mapper-->
        <context:component-scan base-package="com.jc">
            <!--排除controller的扫描-->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
        </context:component-scan>
    
        <!--    以下整合mybatis -->
        <!--    加载jdbc-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <!--    加载数据源-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="user" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
        <!--    配置session工厂-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--        加载数据源-->
            <property name="dataSource" ref="dataSource"></property>
            <!--        加载 mybatis 配置文件 sqlmapconfig-spring文件-->
            <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
            <property name="mapperLocations" value="classpath:mapper/UserMapper.xml"></property>
    
        </bean>
        <!-- 配置映射 为mapper创建实现类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--        这里配置value映射写mapper类-->
            <property name="basePackage" value="com.jc.mapper"></property>
        </bean>
    <!--    整合end -->
    <!--    以下为aop 事物管理-->
    
        <!--声明式事务控制-->
        <!--平台事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <!--配置事务增强-->
        <tx:advice id="txAdvice">
            <tx:attributes>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>
    
        <!--事务的aop织入-->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.jc.Service.impl.*.*(..))"></aop:advisor>
        </aop:config>
    
    
    </beans>

    spring-mvc 前后端交互
    1导入坐标
    2配置springmvc文件
    配置组件扫描(注解扫描 一般扫描controller层)
    注解驱动
    配置内部资源视图解析(配置访问路径前后缀)
    配置开放静态资源访问权限

    3使用springmvc步骤
    配置前端控制器 初始化为一开始就执行(加载springmvc。xml)

    一开始先拦截住访问 然后加载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:context="http://www.springframework.org/schema/context"
           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
    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">
    
    <!--组件扫描  主要扫描controller-->
        <context:component-scan base-package="com.jc.Controller"></context:component-scan>
        <!--配置mvc注解驱动 数据回写和转发要用到吧-->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!--内部资源视图解析器 -->
        <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    <!--    自定义开放权限-->
    <!--    <mvc:resources mapping="/xx/xx 映射路径 就是访问路径" location=" webapp下的路径"-->
        <!--开放静态资源访问权限-->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>
    
    </beans>

    mybatis(可以不用写 都集成到spring中就好了)
      1导入坐标
      2mapper 映射文件
        对应的类的mapper
        对应的方法的 数据操作
      3config配置文件
        导入jdbc.properties
        定义别名
        environment环境配置
        加载映射
      使用步骤
        加载config文件
        获取映射文件

    <?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>
    
        <!--定义别名-->
        <typeAliases>
            <typeAlias type="com.jc.domain.User" alias="user"></typeAlias>
    <!--        <package name="com.jc.domain"></package>-->
        </typeAliases>
    
    
    <!--    &lt;!&ndash;加载映射&ndash;&gt;-->
    <!--    <mappers>-->
    <!--        &lt;!&ndash;<mapper resource="com/itheima/mapper/AccountMapper.xml"></mapper>&ndash;&gt;-->
    <!--&lt;!&ndash;        <package name="mapper"></package>&ndash;&gt;-->
    <!-- <mapper resource="mapper/UserMapper.xml"></mapper>-->
    <!--    </mappers>-->
    
    
    </configuration>

    其他
      乱码过滤器

    <?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_2_5.xsd" id="WebApp_ID" version="2.5">
    
      <!--spring 监听器 ContextLoaderListener监听器的作用:
    
    当启动web容器时。自动装配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-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <!--乱码过滤器-->
      <filter>
        <filter-name>CharacterEncodingFilter</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>
      </filter>
      <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
    </web-app>


    controller层(控制层)
      页面和后端交互层
      一般就是获取视图给的东西然后通过service进行相应的操作
    domain
      实体层 编写对应实体类
    mapper dao
      映射接口层 编写对应实体方法
    service(业务层)
      controller与mapper交互层
      编写方法具体获取以及返回参数给controller

  • 相关阅读:
    find命令详解
    wget命令
    国内镜像源
    向linux服务器上传下载文件方式收集
    一些初学shell自己写的一些练习题脚本
    在Linux系统下mail命令的用法
    MAC 下安装 SVN
    天气预报api整理
    pdi vcard-2.1
    Android Studio 问题锦集【持续更新】
  • 原文地址:https://www.cnblogs.com/ziwang520/p/15843750.html
Copyright © 2020-2023  润新知