SSM各框架使用及整合
spring
常用注解
配置文件说明
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描组件(除控制层)--> <context:component-scan base-package="com.atguigu.ssm"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--引入jdbc.properties--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启事务的注解驱动 将使用注解@Transactional标识的方法或类中所有的方法进行事务管理 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!--配置SqlSessionFactoryBean,可以直接在Spring的IOC中获取SqlSessionFactory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--设置MyBatis的核心配置文件的路径--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!--设置数据源--> <property name="dataSource" ref="dataSource"></property> <!--设置类型别名所对应的包--> <property name="typeAliasesPackage" value="com.atguigu.ssm.pojo"></property> <!--设置映射文件的路径,只有映射文件的包和mapper接口的包不一致时需要设置--> <!--<property name="mapperLocations" value="classpath:mappers/*.xml"></property>--> </bean> <!-- 配置mapper接口的扫描,可以将指定包下所有的mapper接口 通过SqlSession创建代理实现类对象,并将这些对象交给IOC容器管理 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.atguigu.ssm.mapper"></property> </bean> </beans>
springMVC
常用注解
配置拦截器
方式一:
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <!-- 生效范围/**代表所有 --> <mvc:exclude-mapping path="/testRequestEntity"/> <!-- 不生效的范围--> <ref bean="fistInterceptor"></ref><!-- 使用哪个拦截器拦截 --> </mvc:interceptor> </mvc:interceptors>
方式二:
<mvc:interceptors>--> <bean class="com.atguigu.mvc.interceptors.FistInterceptor"></bean>对所有请求生效 </mvc:interceptors>-->
方式三:
<mvc:interceptors>--> <ref bean="fistInterceptor"></ref> </mvc:interceptors>-->
方式二和方式三两种配置方式都是对DispatcherServlet所处理的所有的请求进行拦截
配置文件说明
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描控制层组件--> <context:component-scan base-package="com.atguigu.ssm.controller"></context:component-scan> <!--配置视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean> <!--配置默认的servlet处理静态资源--> <mvc:default-servlet-handler /> <!--开启mvc的注解驱动--> <mvc:annotation-driven /> <!--配置视图控制器--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <!--配置文件上传解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean> </beans>
Mybatis
配置文件说明
<?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> <!-- MyBatis核心配置文件中的标签必须要按照指定的顺序配置: properties?,settings?,typeAliases?,typeHandlers?, objectFactory?,objectWrapperFactory?,reflectorFactory?, plugins?,environments?,databaseIdProvider?,mappers? --> <settings> <!--将下划线映射为驼峰--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <plugins> <!--配置分页插件--> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> </configuration>