前幾篇文章整合了Spring+Mybatis,這篇文章將最終整合SpringMVC,最終形成SSM基礎框架。本篇文章與之前文章結構類似,先單獨搭建SpringMVC,再統一整合Spring+Mybatis。
SpringMVC的單獨搭建與使用
之前幾篇的項目我都是以一個普通的項目在運行程序,但是如果運行SpringMVC的首要條件是項目必須是一個Web項目,所以,我首先要將原項目更改成一個Web項目:添加Web module。
(1)打開project structure設置,選擇添加module
(2)更改目錄
(3)pom文件添加打包方式爲war
OK,現在這個項目已經是一個Web項目了。開始搭建SpringMVC。
1.環境準備
(1)導入SpringMVC的jar包
<!--SpringMVC的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
(2)編寫web.xml配置
web程序運行在web容器中必須要實現至少一個Servlet,並且需要配置在web.xml中。SpringMVC搭建需要在web.xml中配置自己的組件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置SpringMVC的前端控制器DispatcherServlet,並且<init-param>中配置了初始化DispatcherServlet時掃描springmvc.xml -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<!--默認匹配所有請求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--url訪問時,tomcat會執行filter過濾器,在這裏將編碼統一 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<!--因爲靜態資源不需要處理,所以直接由tomcat的默認Servlet處理靜態資源的獲取 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC最重要的就是DispatcherServlet,他是SpringMVC的核心,管理者所有的請求,所以我們只需要配置這一個Servlet,交由tomcat反射代理執行,即可完成轉發處理請求。
SpringMVC的流程圖(網圖):
2.程序編寫
SpringMVC的基礎環境搭建完成,那麼可以開始編寫controller測試是否可以成功運行:
(1)Controller:
package WholeSSM.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("myController")
public class MyController {
@RequestMapping("test")
public String test()
{
System.out.println("SpringMVC運行成功!");
return "test";
}
}
(2)jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
SpringMVC運行成功!
</body>
</html>
(3)部署tomcat
3.運行結果
Spring整合SpringMVC以及Mybatis
Spring整合SpringMVC的重點是該項目已經成爲一個Web項目,Web項目的一個特點是沒有main函數,所有方法的執行都是依靠web容器,所以ApplicationContext的創建就要使用其他方法:
(1)
配置一個監聽器,在web容器創建的時候啓動Spring容器完成掃描以及創建Bean對象並存儲。這種情況適用於MVC框架是其他廠商的產品時。
(2)
直接使用DispatcherServlet加載Spring容器。
1.環境準備
(1)方法一:配置監聽器啓動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>
在web.xml配置此監聽器。
(2)SpringMVC直接一起加載Spring容器。
更改加載參數:
在這裏的初始加載文件由springmvc改成了spring*,代表所有符合spring前綴的xml全部加載。
所以DispatcherServlet還一起加載了springdao。
springdao.xml:
<?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"
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">
<!-- 掃描service以及mapper包下所有使用註解的類型 -->
<context:component-scan base-package="WholeSSM" >
<!--這裏配置不掃描controller -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置整合mybatis過程 -->
<!-- 數據庫連接池 其中,JDBC的屬性參數可以單獨寫一條properties文件,然後在此引用-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置連接池屬性 -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/october?characterEncoding=utf8&useSSL=false"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<!-- c3p0連接池的私有屬性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 關閉連接後不自動commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 獲取連接超時時間 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 當獲取連接失敗重試次數 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 配置SqlSessionFactory對象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入數據庫連接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 掃描entity包 使用別名 -->
<property name="typeAliasesPackage" value="com.test.entity"/>
<!-- 掃描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置掃描Dao接口包,動態實現Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 給出需要掃描Dao接口包 -->
<property name="basePackage" value="WholeSSM.Mapper"/>
</bean>
</beans>
所以,service以及mapper的接口也都被異同加載了。
2.代碼
包結構:
package WholeSSM.controller;
import Entity.StudentEntity;
import Mapper.StudentMapper;
import WholeSSM.Service.myService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("myController")
public class MyController {
@Autowired
myService service;
@RequestMapping("test")
public String test()
{
System.out.println("SpringMVC運行成功!");
List<StudentEntity> list= service.selectAll();
for (StudentEntity studentEntity : list) {
System.out.println(studentEntity);
}
return "test";
}
}
package WholeSSM.Mapper;
import Entity.StudentEntity;
import java.util.List;
public interface StudentMapper {
List<StudentEntity> selectAll();
void update(StudentEntity student);
}
package WholeSSM.Service;
import Entity.StudentEntity;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface myService {
List<StudentEntity> selectAll();
void update(StudentEntity student);
}
package WholeSSM.Service;
import Entity.StudentEntity;
import WholeSSM.Mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("Service")
public class myServiceImpl implements myService {
@Autowired
StudentMapper mapper;
public List<StudentEntity> selectAll()
{
return mapper.selectAll();
}
public void update(StudentEntity student)
{
mapper.update(student);
}
}
3.運行結果
OK,到此,SSM框架搭建完成。