SSM整合

文章目录

  • 1.环境准备
    • spring环境搭建
      • applicationContextConfigxml
    • mybatis环境搭建
      • SqlMapConfig.xml
    • springmvc环境搭建
      • web.xml 与 springmvc.xml
  • 2.整合
    • spring整合mybatis
      • 修改applicationContext.xml
      • 添加事务
    • spring整合springmvc
      • 配置ContextLoaderListener监听器
      • controller中注入service对象

1.环境准备

spring环境搭建

applicationContextConfigxml

1 第一步:编写 spring 配置文件并导入约束 - (applicationContextConfig.xml)

  • 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架
    去管理

2 第二步: 使用注解配置业务层和持久层(实现类)

mybatis环境搭建

SqlMapConfig.xml

  1. 在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件。(配置连接数据库的四要素,以及结果集映射,注意使用注解时的配置方式)
<!-- 使用的是注解 -->
<mappers>
	<!-- <mapper class="cn.itcast.dao.AccountDao"/> -->
	<!-- 该包下所有的dao接口都可以使用 -->
	<package name="cn.itcast.dao"/>
</mappers>
  1. 在AccountDao***接口***的方法上添加注解,编写SQL语句

springmvc环境搭建

web.xml 与 springmvc.xml

1 第一步: 在 web.xml 中配置核心控制(DispatcherServlet)

  1. 配置前端控制器:服务器启动必须加载,需要加载springmvc.xml配置文件
    -配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件
    -服务器启动的时候,让DispatcherServlet对象创建
  2. 配置解决中文乱码的过滤器

2 第二步: 创建springmvc.xml的配置文件,编写配置文件

  1. 扫描controller的注解,别的不扫描
  2. 配置视图解析器
  3. 设置静态资源不过滤
  4. 开启对SpringMVC注解的支持
    3 第三步 创建AccountController***类***,写入注解

2.整合

spring整合mybatis

目的: 把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中。把dao的代理对象存到IOC容器中

修改applicationContext.xml

  1. 修改applicationContext.xml
<!-- 配置C3P0的连接池对象 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver" />
	<property name="url" value="jdbc:mysql:///ssm" />
	<property name="username" value="root" />
	<property name="password" value="root" />
</bean>
<!-- 配置SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置扫描dao的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="cn.itcast.dao"/>
</bean>
  1. 在AccountDao接口中添加@Repository注解
  2. 在service中注入dao对象,

添加事务

  1. 配置Spring的声明式事务管理
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="*" propagation="REQUIRED" read-only="false"/>
		<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
	</tx:attributes>
</tx:advice>
<!-- 配置 aop -->
<aop:config>
<!-- 配置切入点表达式 -->
	<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))"
	id="pt1"/>
	<!-- 建立通知和切入点表达式的关系 -->
	<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>

spring整合springmvc

目的:在controller中能成功的调用service对象中的方法。

方法: 在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)。这样加载后,扫描生效,service类就放入容器中了。从而可以通过依赖注入的方法,获取对象。

配置ContextLoaderListener监听器

<!-- 配置Spring的监听器 -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listenerclass>
</listener>
<!-- 配置加载类路径的配置文件 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>

controller中注入service对象

  1. 在controller中注入service对象,调用service对象的方法进行测试

你可能感兴趣的:(Spring全家桶)