基於IDEA的SSM配置文件整合基礎(有步驟)

来源:https://www.cnblogs.com/yyuanyu/archive/2019/10/29/11762126.html
-Advertisement-
Play Games

今天進行了SSM框架的整合,遇到了很多的錯誤,但所幸都有解決,以下為基礎的整合步驟,後續待完善 1.SSM整合所需要: spring的jar(包含tx)、springmvc的jar、mybatis.jar、mybatis-spring.jar、tomcat、commons-dbcp.jar等 2.創 ...


今天進行了SSM框架的整合,遇到了很多的錯誤,但所幸都有解決,以下為基礎的整合步驟,後續待完善

1.SSM整合所需要

  spring的jar(包含tx)、springmvc的jar、mybatis.jar、mybatis-spring.jar、tomcat、commons-dbcp.jar等

2.創建Maven項目

 

 

 

這裡選擇如下圖,否則Maven創建可能不成功 Name:archetypeCatalog    Value:internal

 

 

 然後一直next,最後Maven項目創建成功,如下圖

 

 

 

 

3.更改web.xml文件,由於預設的是web2.3,這個版本有點老了,這裡我選擇用4.0

 

 

 

 

 

 

 這樣web的版本就更改了

 4.導入所需要的依賴jar包

 

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--springmvc-->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>


    <!--mybatis-->
    <!--Mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.16</version>
    </dependency>
    <!--Mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.2</version>
    </dependency>
    <!--Log4j-->
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <!--dbcp-->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>


    <!--Mybatis與Spring整合jar-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>

    <!--增加事務支持-->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

  </dependencies>

 

  註意:IDEA中的Maven項目需要指定讀取資源路徑,否則可能無法讀取資源(.properties、Mapper)

<build>   
    <resources>
      <!--指定xml文件位置-->
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <!--這裡寫成true會造成編譯註釋錯誤-->
        <filtering>false</filtering>
      </resource>

      <!--指定xml文件位置-->
      <resource>
        <directory>src/main/resource</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <!--這裡寫成true會造成編譯註釋錯誤-->
        <filtering>false</filtering>
      </resource>
    </resources>
</build>

  

 

5.建立需要的包,如dao、service、controller、並根據資料庫建立對應的類

 

 

 

6.配置Mybatis-config.xml、spring-dao.xml、spring-service.xml、spring-mvc.xml、applicationContext.xml(這裡,我將文件分開其實除了Mybatis-config.xml,其餘都可以整合一起寫)

6.1、Mybatis-config.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>
    <!--增加日誌的支持-->
    <settings>
        <setting name="logImpl" value="log4j"/>
    </settings>
    <!--取別名-->
    <typeAliases>
        <package name="com.study.pojo"/>
    </typeAliases>
    
    <mappers>
        <package name="com.study.dao"/>
    </mappers>
</configuration>

  6.2、spring-dao.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
">

    <context:component-scan base-package="com.study.dao.impl"/>
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置數據源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--生成sqlsessionfactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--生成方法-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>


</beans>

  6.3、spring-service.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
">
    <import resource="spring-dao.xml"/>
    <context:component-scan base-package="com.study.service.impl"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

  

6.4、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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
">

    <!--配置掃描器-->
    <context:component-scan base-package="com.study.controller"/>
    <mvc:annotation-driven/>
    <!--靜態-->
    <mvc:default-servlet-handler/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

  

6.5、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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
">

    <import resource="spring-mvc.xml"/>
    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>

</beans>

  6.6、db.properties(使用的dbcp數據連接池,不整合時候可以不加jdbc但是整合後不在前面加上jdbc就會出現錯誤)

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/自己的資料庫名字?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false
jdbc.username=賬戶
jdbc.password=密碼

  6.7、log4j.properties(這個可以網上找)

log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache=INFO

  至此,配置文件基本整合完畢

7、根據需求,在對應層面編寫對應的代碼

提示:

    Controller層的註解為@Controller

    Service層的註解為  @Service

    Dao層的註解為    @Repository

8、關於maven導包問題

建議:儘量使用Maven進行導包,如果手動導包可能會出現一些莫名的錯誤

9、問題總結

  Ⅰ、在創建Maven項目的時候有兩點需要註意:①.需要指定資源文件路徑、也就是在pom.xml文件的build中添加指定。②.在進行導包的時候儘量使用Maven進行導包

  Ⅱ、註解的使用,不同的層使用不同的註解(@Controller、@Service、@Repository),但是這些註解都來自同一個父註解,可以自行網上查看

  Ⅲ、關於在配置文件中必須配置的屬性(大概):①.DispatcherServlet(SpringMVC的核心) ②、dataSource(數據源)③、DataSourceTransactionManager(事務支持,沒這個整合會出錯)④、SqlSessionFactoryBean(獲取sqlsessionFactory)⑤、SqlSessionTemplate

 

 

關於後臺傳Json到前端亂碼問題:

解決方式一:在RequestMapping中添加produces = "application/json;charset=utf- 8"
@RequestMapping(value = "/a",produces = "application/json;charset=utf- 8")

 

解決方式二:在<mvc:annotation-driven>中加入如下代碼
    <!--配置掃描器-->
    <context:component-scan base-package="com.study.controller"/>
    <mvc:annotation-driven>
        <!--解決Json亂碼問題-->
        <mvc:message-converters register-defaults="true">

            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
            </bean>

            <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                        <property name="failOnEmptyBeans" value="false"/>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

  

 

附:有問題歡迎大家指出

 


 

 

 

 

 

 

 


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

-Advertisement-
Play Games
更多相關文章
  • 2019-10-29-23:08:00 目錄 1.內部類 2.成員內部類 3.局部內部類 4.局部內部類的final問題 5.匿名內部類 內部類: what:內部類(nested classes),面向對象程式設計中,可以在一個類的內部定義另一個類 分類: 1.成員內部類 2.局部內部類(包含匿名內 ...
  • 1、I/O多路復用指:通過一種機制,可以監視多個描述符,一旦某個描述符就緒(一般是讀就緒或者寫就緒),能夠通知程式進行相應的讀寫操作。 2、I/O多路復用避免阻塞在io上,原本為多進程或多線程來接收多個連接的消息變為單進程或單線程保存多個socket的狀態後輪詢處理。 selectselect是通過 ...
  • 一、多態的語法 1.關於多態中涉及到幾個概念 (1)向上轉型(upcasting) 子類型轉換為父類型,又被稱為自動類型轉換 (2)向下轉型(downcasting) 父類型轉換為子類型,又被稱為強制類型轉換(需要加強制類型轉換符) (3)無論是向上轉型還是向下轉型,它們之間都必須有繼承關係,否則編 ...
  • 微服務架構 網關:路由用戶請求到指定服務,轉發前端 Cookie 中包含的 Session 信息; 用戶服務:用戶登錄認證(Authentication),用戶授權(Authority),用戶管理(Redis Session Management) 其他服務:依賴 Redis 中用戶信息進行介面請求 ...
  • 從 MariaDB 一張表內讀 10 萬條記錄,經處理後寫到 MongoDB 。 Batch 任務模型 具體實現 1、新建 Spring Boot 應用,依賴如下: 2、創建一張表,並生成 10 萬條數據 3、創建 Person 類 4、創建一個中間處理器 5、創建 ,用戶資料庫映射 6、創建任務完 ...
  • 微服務是什麼? Spring Cloud是什麼? Spring Cloud版本命名方式? Spring Cloud版本選擇? ...
  • 方法一:下載Pycharm與安裝 下載地址:https://www.jetbrains.com/pycharm/ Pycharm專業版和社區版對大多數人來說差別不大,區別如下: 我們下載Linux的社區版: 右擊安裝包 -> Extract here 生成一個Pycharm的文件夾 cd到該文件夾的 ...
  • 1、首先創建一個抽象父類: 2、創建兩個列印類繼承抽象父類: 3、在創建一個properties配置文件,文件名為pro.properties 4、利用反射和多態調用列印類中的方法 測試得到結果 結論: 利用好java反射和多態機制,可以在不改變代碼的情況下,根據鍵值創建對應的類對象,通過多態方式執 ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...