SSM:Spring+SpringMVC+MyBatis
项目(提取码:ssm1)结构:
===================================================================
1、 所需要的jar包(提取码:jars )
2、SpringMVC的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- spring-mvc 管理表现层配置 -->
<!-- prefix + return返回值+ suffix
/WEB-INF/views/success.jsp
-->
<!-- 视图解析器 就是为了简写路径
可以直接在<bean>里加属性,p:prefix="/WEB-INF/views/" p:suffix=".jsp"
这里是用别人写好类,所以只能通过xml注入,不可能去.class文件(可读)里加注解吧
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 放行静态资源 -->
<mvc:default-servlet-handler />
<!-- 使@requestMapping注解生效 -->
<mvc:annotation-driven />
<!-- 开启注解扫描 -->
<context:component-scan base-package="cn.itsource.controller"></context:component-scan>
</beans>
3、Spring的配置(其实二者的配置信息可以写在一个配置文件中,分开写是为了各司其职)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 管理三层(除表现层的,已经有spring-mvc管了) -->
<!-- 加载jdbc.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
</bean>
<!-- spring管理sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="cn.itsource.domain"></property>
<!-- 加载mapper.xml映射文件 -->
<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml"></property>
</bean>
<!-- mapper 接口交给spring管理 获取的就是代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.mapper"></property>
</bean>
<!-- 开启扫描包注解 service层 -->
<context:component-scan base-package="cn.itsource.service"></context:component-scan>
</beans>
4、web.xml配置:spring+springMVC+过滤器+前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 加载spring的核心配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 服务器启动的时候加载sprin的 配置文件,spring跟着启动,这样spring才能有创建对象的能力
已经有一个配置文件跟着前端控制器了,所以这个配置文件得用监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载spring-mvc核心配置文件 -->
<init-param>
<!-- name固定的,不能乱起名,否则还是会去默认路径(WEB-INF)下找配置文件 -->
<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>*.do</url-pattern> -->
<!-- 拦截所有,eg:.xml、.css、.jsp -->
<!-- <url-pattern>/*</url-pattern> -->
<!-- 拦截所有,但是把静态资源这些也拦住了,eg:xml、css,所以在application.xml里配置放开静态资源 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- filter:过滤器,为了中文不乱码,前端请求先经过过滤器,再到达前端控制器 -->
<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>