1. 程式人生 > >SSM整合開發完整步驟

SSM整合開發完整步驟

SSM程式設計,即SpringMVC + Spring + MyBatis整合,是當前最為流行的JavaEE開發技術架構。其實SSM整合的實質,僅僅就是將MyBatis整合入Spring。因為SpringMVC原本就是Spring的一部分,不用專門整合。

SSM整合的實現方式可分為兩種:基於XML配置方式,基於註解方式。在如今的實際專案中,大多數是xml和註解方式結合使用。

一、步驟


1.新建Dynamic Web Project專案
2.新建mysql的資料庫 springdb, 表student ,有列
        id int 自動增長的。
        name varchar 50 姓名
        age int 年齡
3.匯入jar包
  1.spring的核心jar包:spring-beans.jar,spring-core.jar,spring-context.jar,spring-expression.jar
  2.spring-aop.jar
  3.資料庫相關的jar包:spring-jdbc.jar,spring-tx.jar
  4.web相關的jar包:spring-web.jar,spring-webmvc.jar
  5.mybatis的核心jar包:mybatis-3.4.5.jar
  6.mybatis和spring的整合jar包: mybatis-spring-1.3.1.jar
  7.Jackson的jar包: 三個(用來解析json格式的物件)
  8.其他jar包: mysql的驅動, druid的連線池, log4j.jar, commons-logging.jar
 
4.配置web.xml檔案
  1)註冊spring的監聽器, 建立spring的容器物件, 載入spring的配置檔案(建立Service物件和Dao物件)
  2)註冊springmvc的中央排程器, 建立springmvc的容器物件, 載入springmvc的配置檔案(建立Controller物件)
  3)註冊字符集過濾器,解決post請求亂碼的問題

<?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">
  <display-name>25-SSM</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 註冊spring的監聽器 -->
  <context-param>
  	<!-- 自定義 spring的配置檔案-->
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:conf/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <!-- spring-web.jar -->
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 註冊springmvc的中央排程器 -->
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<!-- spring-webmvc.jar -->
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:conf/dispatcherServlet.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>
  </servlet-mapping>

  <!-- 註冊字符集過濾器 -->
  <filter>
  	<filter-name>characterEncodingFilter</filter-name>
  	<!-- spring-web.jar -->
  	<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>forceRequestEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceResponseEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>characterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


5.定義程式中的包結構: 實體類包  , Dao包名, Service包, Controller包
6.編寫配置檔案
  1)springmvc的配置檔案(檔名自定義)

<?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.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">

	 <!-- springmvc的配置檔案 -->
	 <!-- 宣告元件掃描器 -->
	 <context:component-scan base-package="com.wkcto.controllers" />
	 
	 <!-- 宣告檢視解析器 -->
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<property name="prefix" value="/WEB-INF/jsp/" />
	 	<property name="suffix" value=".jsp" />
	 </bean>

     <!-- 宣告註解驅動 -->
     <mvc:annotation-driven />
</beans>


  2)spring的配置檔案(預設為applicationContext.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">

	  <!-- spring的配置檔案 -->
	  <!-- 宣告元件掃描器 -->
	  <context:component-scan base-package="com.wkcto.service" />
	  
	  <!-- 載入屬性配置檔案 -->
	  <context:property-placeholder location="classpath:conf/jdbc.properties"/>
	  
	  <!-- 宣告資料來源DataSource -->
	  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
	                           init-method="init" destroy-method="close">
	      <property name="url" value="${jdbc.url}" />
	      <property name="username" value="${jdbc.username}" />
	      <property name="password" value="${jdbc.passwd}" />
	      
	  </bean>
	  
	  <!-- 宣告SqlSessionFactoryBean -->
	  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	  	<property name="dataSource" ref="dataSource" />
	  	<property name="configLocation" value="classpath:conf/mybatis.xml" />
	  </bean>
	  
	  <!-- 宣告MyBatis的掃描器 -->
	  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	  	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	  	<property name="basePackage" value="com.wkcto.dao" />
	  </bean>
	  

</beans>


  3)資料庫的屬性配置檔案(一般為:jdbc.properties)

jdbc.url=jdbc:mysql://localhost:3306/springdb
jdbc.username=root
jdbc.passwd=root


  4)mybatis的主配置檔案(一般為:myBatis.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 別名 -->
	<typeAliases>
		<package name="com.wkcto.beans"/>
	</typeAliases>
	<!-- 指定sql對映檔案 -->
	<mappers>
		<package name="com.wkcto.dao"/>
	</mappers>
</configuration>


7.定義實體類, Dao介面和Sql對映檔案, Service介面和實現類, Controller類

在Dao層中還需要配一個mapper.xml檔案,用來訪問資料庫。名字和Dao層中的類名相同。例如:Dao層有一個類叫做StudentDao.java,那麼mapper.xml檔案的名字就叫做StudentDao.xml。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wkcto.dao.StudentDao">
	<!-- insert、select標籤對中的內容就是對資料庫的操作-->
	<insert id="insertStudent">
		insert into student(name,age) values(#{name},#{age})
	</insert>
	
	<select id="selectStudents" resultType="Student">
		select id,name,age from student order by id desc
	</select>
</mapper>


8.定義檢視檔案(jsp)

相關推薦

SSM整合開發完整步驟

SSM程式設計,即SpringMVC + Spring + MyBatis整合,是當前最為流行的JavaEE開發技術架構。其實SSM整合的實質,僅僅就是將MyBatis整合入Spring。因為SpringMVC原本就是Spring的一部分,不用專門整合。 SSM整合的實現方

SSM整合開發實戰-poi匯入匯出excel-mvc三層模式開發體驗(福利:內附完整視訊教程)

    前面幾篇部落格雖然簡單的介紹了基於SSM的框架實現POI匯入匯出的介紹,但是也就只是“簡單的介紹”而已,而且沒有物盡其用。即整合出來的SSM其實還可以做其他很多的事情,閒暇之餘我又基於SSM開發了“產品資訊”的“增加 刪除 修改 更新”功能,目的是為了體會體會目前企業

SSM整合開發之CSV檔案匯入匯出實戰-鍾林森-專題視訊課程

SSM整合開發之CSV檔案匯入匯出實戰—65人已學習 課程介紹         本課程將給大家分享如何基於SSM實現CSV檔案的匯入匯出,並講解目前企業級JavaWeb應用mvc三層模式的開發流程,

SSM整合開發實戰-poi匯入匯出excel-尾聲

     前面介紹了ssm框架的整合開發以及後端介面部分的實現(如今對這些介面有了新的變動以及優化),本文將整合前端框架jquery-easyui來實現前後端介面的對接,更加直接的展示poi匯入匯出excel的效果。(視訊教程地址:https://edu.csdn.

SSM整合開發實戰-poi匯入匯出excel-開戰

   上一篇部落格介紹瞭如何完整的基於ssm搭建一個可用於實戰開發的專案,本文開始將用此專案用來開發若干idea。本文就先小試牛刀開發基於ssm框架實現poi匯入匯出excel檔案,憑良心講,這一需求其實在目前企業級應用開發中幾乎都用得到,那麼本文我將基於自己在專案中遇到的需

ssm整合使用的步驟

一、依賴的包必不可少(spring+springmvc+mybatis+他們整合需要的依賴包) 二、分別配置他們的主配置檔案 1、web.xml,2、springmvc-servlet.xml ,3、applicationContext.xml,4、mybatisConfig.xml 三、根

SSM整合開發xml配置檔案內容springmvc-config/application/web

SSM(spring-springmvc-mybatis)整合開發xml配置檔案內容springmvc-config.xml,application.xml,web.xml 這是我寫的一個人事管理系統其中的一部分 寫此博文為了將其分享出來,一方面為了儲存下來,以後開發專案

ssm 整合開發時出現時單元測試Mapper 注入不進去的情況的解決方法

第一步:檢查相應的是否構建了spring的容器環境UserMapper  userMapper = context.getBean(UserMapper.class);或者寫一個基類直接繼承  例如:最後最後測試相應的dao:如果還有問題就是xxxMapper.xml檔案的問

SSM 整合開發+通用的增刪改查實現

        前一段時間開發java web專案都是採用的SSH框架,開發中發現在使用Hibernate做持久層開發的時候用法不夠靈活,過於笨重,因此,改用MyBaties,結合Spring、SpringMvc框架,現將配置過程梳理如下: 一、專案結構    

SSM整合開發

[TOC] ## 一、整體思路 SSM: SpringMVC + Spring + MyBatis. SpringMVC:檢視層,介面層,負責接收請求,顯示處理結果的。 Spring:業務層,管理service,dao,工具類物件的。 MyBatis:持久層, 訪問資料庫的 > 使用者發起請求--Spr

SSM整合步驟

== ssa ttr bpa retrieve sco line eat spring配置 SSM- CRUD SSM : SpringMVC+Spring+Mybatis Create (新建) +Retrieve (查詢) +Update(更新)+Delete(刪除)

JavaEE互聯網輕量級框架整合開發(書籍)閱讀筆記(2):SSM+Redis概念理解

重復 技術 理解 size 從數據 一個 ron bat 互聯網 一、SSM+Redis的結構圖 在Java互聯網中,以Spring+SpringMVC+MyBatis(SSM)作為主流框架,SSM+Redis的結構圖如下: 二、下面介紹它們各自承擔的功能: 1.S

SSM整合+視訊網站開發(day03)

1.修改視訊資源功能 之前已經完成了檢視和新增視訊資源的功能,接下來我們來實現修改的功能 第一步:修改IndexVideoInfo.jsp (解釋:傳id給controller層的editVideoPre是因為editVideoPre方法裡面需要根據id查videoInfo實體,然後轉發

IDEA環境下SSM整合------註解開發

        根據前一篇文章的步驟,目前專案進度應該是:核心過濾器配置完成、DispatcherServlet和ContextLoader配置完成、資料庫dataSource配置完成、檢視解析器配置完成、Mapper sql和dao method對應沒有問題、註解驅動、事務啟動、default-Serv

SSM整合步驟.md

​ SSM整合步驟 1,建立spring.xml 去課件資料找頭標籤 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/sch

SSM整合+視訊網站開發(day02)

四、後臺管理 1.VideoInfoMapper.xml <select id="selectAll" parameterType="com.itmayiedu.entity.VideoInfo" resultType="com.itmayiedu.

Spring+Hibernate整合開發配置,完整例項

目錄結構 Jar包 配置 一、beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.o

maven下的ssm整合配置步驟

maven下的ssm整合配置步驟 在此之前大部分專案都是使用ssh框架,前幾年爆出struts存在致命bug,Hibernate的封裝深度較高,較mybatis效率低等弊端,現在ssh貌似是在漸漸的退出歷史舞臺,而取而代之的自然是現在用的最多的ssm框架。接下來的內容就是小

SSM框架的搭建及專案開發步驟

第一階段: 1、用PowerDesign建資料模型,並匯出SQL檔案; 2、將SQL檔案匯入到MySQL客戶端,建立表格;   MySQL資料遠端訪問:GRANT ALL PRIVILEGES ON . TO ‘root’@’%’IDENTIFIED BY

SSM全註解完整開發框架基礎搭建

整套框架是以SpringBoot的為基礎,使傳統SSM專案拋棄了XMl的繁瑣配置,又可以擴充套件SpringBoot存在的侷限性,使之可以用於大型專案並減少配置。一、替代web.xml的啟動端       這裡選擇的方式是實現WebApplicationInitializer