随笔 - 35  文章 - 0  评论 - 0  阅读 - 9036

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

 

posted on   ziwang520  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示