SSM框架搭建

来源:http://www.cnblogs.com/lwj-0923/archive/2017/09/05/7477205.html
-Advertisement-
Play Games

先將Spring和Mybatis整合 1.加入 mybatis 的 jar 包和配置文件: 實際上需要配置的就是 settings 的部分。 2. 加入 Spring 的 jar 包和配置文件 之前是在 mybatis-config.xml 獲取 sqlSessionFactory 和掃描 mapp ...


先將Spring和Mybatis整合

1.加入 mybatis 的 jar 包和配置文件: 實際上需要配置的就是 settings 的部分。

<?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>
      <!-- settings標簽可以設置多個屬性值 -->
      <settings>
            <!-- setting標簽負責每一個屬性的設置 -->
            <setting name="mapUnderscoreToCamelCase" value="true"/>
      </settings>
</configuration>
  2. 加入 Spring 的 jar 包和配置文件   之前是在 mybatis-config.xml 獲取 sqlSessionFactory 和掃描 mapper 介面 但是現在 mybatis-config.xml 只有 settings  所以在 bean.xml 中配置獲得 sqlSessionFactory 和掃描 mapper 介面 Spring具體配置參見上篇《Spring+SpringMVC的整合》
<context:component-scan base-package="com.neuedu">
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!-- 載入外部屬性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0 數據源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="user" value="${jdbc.username}"></property>
      <property name="password" value="${jdbc.password}"></property>
      <property name="driverClass" value="${jdbc.driver}"></property>
      <property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>

<!-- 配置事務管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 開啟基於註解的事務 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="comboPooledDataSource"></property>
      <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- 批量掃描mapper介面包 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.neuedu.sm.mapper"></property>
</bean>

 

3.Test 測試

public class TestEmployeeMapper {
      private ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
      @Test
      public void test() {
            EmployeeMapper bean = ioc.getBean(EmployeeMapper.class);
            Employee employee = bean.getEmpById(1);
            System.out.println(employee);
      }
}

 

以上Spring+Mybatis的配置基本完畢


 

將 SpringMVC整合到上面的項目中   1.配置SpringMVC 配置文件
<context:component-scan base-package="com.neuedu" use-default-filters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!-- 配置視圖解析器 -->
<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/>
<mvc:annotation-driven/>

 

2.創建相應的Controller、Service

 

 3.在Controller層獲取頁面傳來的數據,調用Service層方法【SpringMVC】

 Service層調用Mapper介面,【Mybatis】

 Mapper介面中有相應的方法,與sql映射文件相對應【Mybatis】

   
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一:對象頭 HotSpot虛擬機中,對象在記憶體中存儲的佈局可以分為三塊區域:對象頭(Header)、實例數據(Instance Data)和對齊填充(Padding)。 HotSpot虛擬機的對象頭(Object Header)包括兩部分信息,第一部分用於存儲對象自身的運行時數據, 如哈希碼(Has ...
  • spring boot / cloud (九) 使用rabbitmq消息中間件 前言 rabbitmq介紹: RabbitMQ是一個在AMQP基礎上完整的,可復用的企業消息系統。它可以用於大型軟體系統各個模塊之間的高效通信,支持高併發,支持可擴展。 amqp介紹: 即Advanced Message ...
  • 映射文件:指導著MyBatis如何進行資料庫增刪改查, 有著非常重要的意義; - cache 命名空間的二級緩存配置 - cache-ref 其他命名空間緩存配置的引用。 - resultMap 自定義結果集映射 - parameterMap 已廢棄!老式風格的參數映射 - sql 抽取可重用語句塊 ...
  • 需要編譯的文件: main.c fun.c fun.h 第一種:makefile 第二種使用變數 第三種自動推導 5.自動化變數 6.自動化變數 7. 生成多個可執行文件 8. 引用其他目錄下的 .h文件 9.編譯不同目錄下的文件 ...
  • 1 數組的定義 2 數組的記憶體分配及特點 3 數組操作常見問題 4 數組常見操作 5 數組中的數組(二維數組) ...
  • endl是 end line的意思,表示此行結束,換行,就是回車 ...
  • #include <iostream>using namespace std; int main(){ //求兩數中的大者? int a,b; cin>>a>>b; if(a>b) cout<<"The max number is:"<<a; else cout<<"The max number i ...
  • https://ngrok.com/ G:\ngrok>ngrok helpNAME: ngrok - tunnel local ports to public URLs and inspect traffic DESCRIPTION: ngrok exposes local networked s ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...