一、新建Maven項目
點擊菜單欄File項,選擇New-》Project,選中Maven Project,如下圖:

二、配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.newProject.framework</groupId> <artifactId>CreateFramework</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- spring版本號 --> <spring.version>4.2.6.RELEASE</spring.version> <!-- mybatis版本號 --> <mybatis.version>3.4.0</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> <!--jackson版本號--> <jackson.version>2.7.1</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 表示開發的時候引入,發布的時候不會加載此包 --> <!--<scope>test</scope>--> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> <!-- 導入Mysql數據庫鏈接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.2</version> </dependency> <!-- 導入dbcp的jar包,用來在applicationContext.xml中配置數據庫 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- JSTL標簽類 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- 格式化對象,方便輸出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- 映入JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> </dependencies> </project>
三、配置springMVC
在resources文件夾下建立spring-mvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自動掃描該包,使用SpringMVC認為包下用了@Controller注解的類是控制器,避免掃描@Service導致事務不能使用 -->
<context:component-scan base-package = "fv">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!--避免IE執行AJAX時,返回JSON出現下載文件 -->
<bean id = "mappingJacksonHttpMessageConverter"
class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 啟動SpringMVC的注解功能,完成請求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1"/>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
</bean>
<!-- 定義跳轉的文件的前后綴 ,視圖模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 自動給后面action的方法return的字符串加上前綴和后綴,變成一個 可用的url地址 -->
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/> -->
</bean>
<!-- 文件上傳 -->
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000"></property>
</bean> -->
<!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/mvc/**"/>
<bean class="test.SpringMVC.Interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors> -->
<!-- 添加后可以顯示welcome-file界面 -->
<mvc:default-servlet-handler/>
<!-- 啟動注解 -->
<mvc:annotation-driven />
</beans>
四、整合mybatis
在resource文件夾下建立spring-mybatis.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自動掃描 -->
<context:component-scan base-package="fv">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入數據庫配置文件 -->
<bean id = "propertyConfigurer"
class = "org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name = "location" value="classpath:jdbc.properties" />
</bean>
<!-- 連接池 -->
<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="initialSize" value="${jdbc.initialSize}" />
<!-- 連接池最大數量 -->
<property name="maxActive" value="${jdbc.maxActive}"/>
<!-- 連接池最大空閑 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!-- 連接池最小空閑 -->
<property name="minIdle" value="${jdbc.minIdle}" />
<!-- 獲取連接最大等待時間 -->
<property name="maxWait" value="${jdbc.maxWait}" />
</bean>
<!-- 結合Spring和Mybatis -->
<bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描mapping.xml文件 -->
<property name = "mapperLocations" value="classpath:fv/mapping/*.xml" />
</bean>
<!-- DAO接口所在包名,Spring會自動查找其下的類 -->
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name = "basePackage" value="fv.dao" />
<property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" />
</bean>
<!-- 定義事務 -->
<bean id = "transactionManager"
class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name = "dataSource" ref = "dataSource" />
</bean>
<!-- 使用注解定義事務 -->
<tx:annotation-driven transaction-manager = "transactionManager" />
</beans>
五、配置屬性文件
在此總共有兩個屬性文件,一個是log4j.properties,另一哥是數據庫連接文件jdbc.properties;
log4j.properties文件內容如下:
#定義LOG輸出級別 log4j.rootLogger=INFO,Console,File #定義日志輸出目的地為控制台 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out #可以靈活地指定日志輸出格式,下面一行是指定具體的格式 log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到達指定尺寸的時候產生一個新的文件 log4j.appender.File = org.apache.log4j.RollingFileAppender #指定輸出目錄 log4j.appender.File.File = logs/ssm.log #定義文件最大大小 log4j.appender.File.MaxFileSize = 10MB # 輸出所以日志,如果換成DEBUG表示輸出DEBUG以上級別日志 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
jdbc.properties文件如下:
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@192.168.1.213:1521:orcl jdbc.username=KYYS jdbc.password=kyys #定義初始連接數 jdbc.initialSize=0 #定義最大連接數 jdbc.maxActive=20 #定義最大空閑 jdbc.maxIdle=20 #定義最小空閑 jdbc.minIdle=1 #定義最長等待時間 jdbc.maxWait=60000
在webapp/WEB-INF/下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_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <!-- Spring和Mybatis的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <!-- 編碼過濾器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- configure the HiddenHttpMethodFilter,convert the post method to put or delete --> <!-- <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <!-- Spring監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring內存溢出監聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Spring MVC servlet --> <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:spring-mvc.xml</param-value> </init-param> <!-- 容器在啟動時就加載這個servlet --> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 設置默認首頁 --> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app>
六、程序內容
程序目錄結構如下如:

index.jsp頁面內容:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
測試框架<a href="test/index" method ="POST">登錄</a>
<form action= "test/indexPost" method ="POST">
<input type= "submit" value ="測試登錄"/>
</form >
<form action= "home/gohome" >
<input type= "text" id="id" name="id" value ="0"/>
<input type= "submit" value ="測試登錄"/>
</form >
<form action= "home/index" >
<input type= "submit" value ="測試登錄"/>
</form >
</body>
</html>
Controller控制器
package fv.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/home")
public class HomeController {
@RequestMapping(value = "/gohome", method = RequestMethod.GET)
@ResponseBody
public String goToHome(@RequestParam("id") String id) {
return "this is my home";//Integer.parseInt(id)
}
@RequestMapping(value = "/index",method = RequestMethod.GET)
public String index()
{
return "/admin";
}
}
package fv.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import fv.Service.TestService;
@Controller
@RequestMapping("/test")
public class TestController {
//@Autowired
private TestService testServiceImp;//
@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
public String getTestByName(@RequestParam("id") String id) {
return testServiceImp.getByName(0L);//Integer.parseInt(id)
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String HellowWorld(@RequestParam("id") String id) {
return "Hello World";//Integer.parseInt(id)
}
@RequestMapping(value = "/index",method = RequestMethod.GET)
public String index()
{
return "/admin";
}
@RequestMapping(value = "/indexPost")
public String indexPost()
{
return "/admin";
}
}
dao層Mapper接口:
package fv.dao;
public interface HomeMapper {
String getByName(Long id);
}
package fv.dao;
public interface TestMapper {
String getByName(Long id);
}
mapping文件xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-.dtd"> -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="fv.dao.HomeMapper">
<!-- <resultMap id="BaseResultMap" type="fv.pojo.Test">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="test1" property="test1" jdbcType="INTEGER" />
<result column="test2" property="test2" jdbcType="VARCHAR" />
</resultMap> -->
<!-- <select id="getByName" parameterType="Long" resultType="String">
<![CDATA[
SELECT NAME
FROM TM_SYS_EMPLOYEE
WHERE ID=#{id}
]]>
</select> -->
<select id="getByName" resultType="java.lang.String">
SELECT NAME
FROM TM_SYS_EMPLOYEE
WHERE ID=#{id}
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-.dtd"> -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="fv.dao.TestMapper">
<!-- <resultMap id="BaseResultMap" type="fv.pojo.Test">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="test1" property="test1" jdbcType="INTEGER" />
<result column="test2" property="test2" jdbcType="VARCHAR" />
</resultMap> -->
<!-- <select id="getByName" parameterType="Long" resultType="String">
<![CDATA[
SELECT NAME
FROM TM_SYS_EMPLOYEE
WHERE ID=#{id}
]]>
</select> -->
<select id="getByName" resultType="java.lang.String">
SELECT NAME
FROM TM_SYS_EMPLOYEE
WHERE ID=#{id}
</select>
</mapper>
服務層
package fv.Service;
public interface HomeService {
String getByName(Long id);
}
package fv.Service;
import org.springframework.beans.factory.annotation.Autowired;
import fv.dao.HomeMapper;
public class HomeServiceImp {
@Autowired(required = false)
private HomeMapper testMapper=null;
public String getByName(Long id) {
return testMapper.getByName(id);
}
}
package fv.Service;
public interface TestService {
String getByName(Long id);
}
package fv.Service;
import org.springframework.beans.factory.annotation.Autowired;
import fv.dao.TestMapper;
public class TestServiceImp implements TestService {
@Autowired(required = false)
private TestMapper testMapper=null;
public String getByName(Long id) {
return testMapper.getByName(id);
}
}
類目錄如下圖:

admin.jsp頁面如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登錄成功!<a href="../test/hello?id=0">測試方法</a><%-- ${dynamicURL} --%>
<a href="../home/gohome?id=0">測試重定向</a>
</body>
</html>
目錄結構如下圖:

