Spring+SpringMVC+Mybatis整合
1. 需要jar包(maven管理)
mybatis-spring、mybatis、mysql-connector-java、spring-context、spring-webmvc、spring-orm、spring-test、jackson-databind、javax.servlet-api、javax.servlet.jsp-api、jstl、commons-dbcp2、aspectjweaver、aspectjrt
2. 配置web.xml
2.1 前端控制器
<!-- front controller -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2.2 IoC容器监听器+路径参数
<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>
2.3 编码过滤器
<filter>
<filter-name>enc</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>enc</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.配置applicationContext.xml
在包范围下扫描注解
<context:component-scan base-package="com.kygo.ssm" />
注解代替配置
<context:annotation-config />
切面自动生成代理
<aop:aspectj-autoproxy />
数据源(连接池)
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/dang?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.kygo.ssm.entity" />
<!--
<property name="typeHandlersPackage" value="com.kygo.ssm.typehandlers" />
-->
<property name="mapperLocations" value="classpath*:com/kygo/ssm/mapper/*.xml" />
<!--
<property name="configLocation" value="WEB-INF/mybatis-config.xml" />
-->
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
注解声明事务
<tx:annotation-driven transaction-manager="transactionManager" />
4. 配置dispatcher-sevlet.xml
<context:component-scan base-package="com.kygo.ssm.controller" />
<context:annotation-config />
静态页面使用默认的servlet
<mvc:default-servlet-handler />
<!--
<mvc:resources location="/WEB-INF/*" mapping="/resource" />
-->
视图解析器
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
消息转换器配置: 配置后可以直接返回对应的消息(图片,字符串,json等)
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>