其樂後臺管理系統(二)--整合三大框架(spring+springmvc+mybatis)

前言:這個項目是我第一次以講解的形式,給你們進行詳情介紹,若是有什麼不足請在評論區進行說明,我將及時改正,下面開始今天的內容,建立一個新的sprngboot,並對項目所須要的環境和框架(SSM)進行搭建css

其樂蛋糕店後臺管理系統 地址
Github--其樂後臺管理系統源碼 https://github.com/XINGGou/qile
其樂後臺管理系統(一)--項目介紹 http://www.javashuo.com/article/p-gbpajmha-dd.html
其樂後臺管理系統(二)--整合三大框架(spring+springmvc+mybatis) http://www.javashuo.com/article/p-nzkhhnxu-dp.html
其樂後臺管理系統(三)--整合mybatis框架(三大框架搭建成功) http://www.javashuo.com/article/p-ozfmlifj-ee.html
其樂後臺管理系統(四)--門店管理模塊 http://www.javashuo.com/article/p-ragyplqr-cu.html
其樂後臺管理系統(五)--訂單管理系統 http://www.javashuo.com/article/p-oqpdbazv-en.html
其樂後臺管理系統(三)--整合mybatis框架(框架搭建成功) http://www.javashuo.com/article/p-ozfmlifj-ee.html
其樂後臺管理系統(四)--門店管理模塊 http://www.javashuo.com/article/p-ragyplqr-cu.html

1. 項目環境搭建(SSM整合)

1.1 項目環境搭建

該項目已是我已經完結的一個後臺管理系統,我已將其發佈在了github上 如若感興趣,但願多指教:https://github.com/XINGGou/qilehtml

1.建立Maven的簡單web工程

在這裏插入圖片描述 在這裏插入圖片描述

剛剛建立成功時,pom文件會報錯,是由於缺乏WEN-INF和web.xml的緣由故無需擔憂,若建立後依舊報錯能夠右鍵項目選擇maven>>點擊update project前端

2.添加WEB-INF目錄並再下方添加web.xml文件和pages文件夾

pages中是前端內容請到個人github上進行下載:
https://github.com/XINGGou/qilejava

在這裏插入圖片描述

3.建立包路徑和目錄

在這裏插入圖片描述

4.再pom.xml文件中引入junit、log4j、servlet等必備依賴包
<dependencies>
	<!-- 單元測試 -->
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.10</version>
		<scope>test</scope>
	</dependency>
	<!-- 整合log4j -->
	<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.4</version>
	</dependency>
	<!-- Jackson Json處理工具包 -->
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>2.4.2</version>
	</dependency>
	<!-- Servlet/JSP/JSTL -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jsp-api</artifactId>
		<version>2.0</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>
	
</dependencies>
5.再resources目錄下建立log4j.properties文件
# Global logging configuration
log4j.rootLogger=INFO,A1

log4j.logger.cn.yhmis.mapper =DEBUG

# Console output...
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

1.2整合spring框架

1.在pom.xml文件中引入spring的依賴包
<!-- 整合spring框架(包含springmvc)
	這個jar文件包含springmvc開發時的核心類, 同時也會將依賴的相關jar文件引入進來(spring的核心jar文件也包含在內)
 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>
<!--這個jar文件包含對Spring對JDBC數據訪問進行封裝的全部類 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>
2.在resources/spring目錄下建立spring的核心配置文件applicationContext.xml

在這裏插入圖片描述

暫時將bean中的內容粘貼複製:git

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

</beans>

1.3整合springmvc框架

在1.2中已經將springmvc的jar包導入,故在此沒必要再次導入。github

1. 在resources/spring目錄下建立springmvc的核心配置文件springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
						http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
						http://www.springframework.org/schema/context
          				http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 1.配置前端控制器放行靜態資源(html/css/js等,不然靜態資源將沒法訪問) -->
	<mvc:default-servlet-handler/>
	
	<!-- 2.配置註解驅動,用於識別註解(好比@Controller) -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 3.配置須要掃描的包:spring自動去掃描 base-package 下的類,
		若是掃描到的類上有 @Controller、@Service、@Component等註解,
		將會自動將類註冊爲bean 
	 -->
	<context:component-scan base-package="com.it.controller">
	</context:component-scan>
	
	<!-- 4.配置內部資源視圖解析器
		prefix:配置路徑前綴
		suffix:配置文件後綴
	 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	
</beans>
2.在web.xml中配置springmvc
<!-- 配置springmvc, 將全部請求交給springmvc來處理 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置springmvc核心配置文件的位置,默認Springmvc的配置文件是在WEB-INF目錄下,默認的名字爲springmvc-servlet.xml,若是要放在其餘目錄,則須要指定以下配置: -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/*.xml</param-value>
		</init-param>
	</servlet>
	<!-- 其中的斜槓(/)表示攔截全部請求(除JSP之外), 全部請求都要通過springmvc前端控制器 -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
3. 配置springmvc亂碼處理過濾器

在tomcat8.0以後get請求接受中文已經不會在出現亂碼的問題
而當你使用post請求接受中文時依舊會有亂碼的現象,所以須要本身解決該問題
全部爲了不亂碼的出現,在web.xml中配置以下信息web

<!-- 亂碼處理過濾器 -->
	<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>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<!-- 指定攔截方式爲攔截全部請求 -->
		<url-pattern>/*</url-pattern>
	</filter-mapping>
4.測試springmvc框架

1)在WEB-INF/pages下建立test.jsp頁面spring

<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" 
	uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
	<h1>test springmvc~~~</h1>
	
	
<%-- 	<c:forEach items="${ list }" var="door">
		${ door.id }	| 
		${ door.name }	|
		${ door.tel }	|
		${ door.addr }	<br/>
	</c:forEach> --%>
	
</body>
</html>

2)建立測試Controller,TestControllerapache

package com.it.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 測試類:測試springmvc開發環境
 * @author XINGGou
 */
@Controller
public class TestController {

	@RequestMapping("/hello")
	public String hello(){
		System.out.println("test springmvc~~");
		return "test";
	}
	
}

3)右鍵項目>>run>>點擊run on server>>啓動項目
啓動後在彈出的窗口上輸入hello
顯示效果爲:api

在這裏插入圖片描述

                                                      --------點擊跳轉mybatis框架整合

相關文章
相關標籤/搜索