Ssm集成
思路:
Spring+sringmvc+mybatis
通过spring去管理springmvc编写的handler(*)
还有管理mybatis的sqlsessionfactory mapper
第一步:
整合dao(spring+mybatis)
第二步:
整合service(spring管理service)service可以调用dao(mapper)
第三步:
整合controller,使用spring进行管理,spring管理controller接口
第四步:
在controller接口调用service,
第五步:
添加jar包
Mybatis3.2.7 spring3.2
配置文件
Web.xml文件的配置(处理中文乱码)
<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>
applicationContext-dao.xml 配置数据源datasource sqlsessionfactory mapper扫描器
applicationContext-service.xml 配置service接口
applicationContext-transcation.xml事务管理
springmvc.xml springmvc的配置 处理器 处理器映射器 处理器适配器 处理器解析器
web.Xml配置
<!-- -加载spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classPath:applicationContext-*.xml</param-value>
</context-param>
<!-- 处理中文乱码过滤器 -->
<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>
<!-- -前端控制器 -->
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
Springmvc-servlet.xml文件配置
<!-- -配置 -->
<!-- -处理器映射器
handlermapping映射器实现所有的接口
将action的url配置在bean的name的属性中
-->
<bean id="" name="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- -处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- -注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- -使用spring组件扫描器 -->
<context:component-scan base-package="com.wang.ssm.controller"></context:component-scan>
<!-- 配置试图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 后缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
applicationContext-dao.xml的配置
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- -使用spring的单利方式管理sqlsessionfactory进行管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- -spring整合mybatis -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
</bean>
<!-- -basepackage -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wang.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
SqlMapConfig.xml的配置
<configuration>
<!-- -配置包扫描器 -->
<mappers>
<package name="com.wang.ssm.mapper"/>
</mappers>
</configuration>
Applaction-transcation.xml文件配置
<!-- -配置事务 -->
<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="add*" propagation="REQUIRED"/>
<!-- -删除 -->
<tx:method name="delte*" propagation="REQUIRED"/>
<!-- -修改 -->
<tx:method name="update*" propagation="REQUIRED"/>
<!-- -查询 -->
<tx:method name="select*" propagation="REQUIRED"/>
<!-- -查询 -->
<tx:method name="find" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wang.ssm.service.impl.*.*(..))"/>
</aop:config>
applicationContext-service.xml的配置
Productmapper.xml文件的配置
<mapper namespace="com.wang.ssm.mapper.productMapper">
<!-- 代理模式的开发 -->
<!-- -查询 -->
<!-- sql片段 -->
<sql id="query_product_where">
<if test="ProductCustomer!=null">
<if test="ProductCustomer.pname!=null">
and pname like '%${ProductCustomer.pname}%'
</if>
</if>
</sql>
<select id="findProductAll" parameterType="com.wang.ssm.entity.ProductQueryVo"
resultType="com.wang.ssm.entity.ProductCustomer">
select * from product
<!-- -使用sql片段 -->
<where>
<include refid="query_product_where"></include>
</where>
</select>
ssm集成配置
最新推荐文章于 2023-10-24 11:42:09 发布