第一次的ssm整合

来源:https://www.cnblogs.com/happy12123/archive/2022/05/31/16332628.html
-Advertisement-
Play Games

首先請記住一點,在電腦中所有的二進位都是以補碼的形式存儲的,所以你最後取反之後只是這個數的補碼,你還需要轉換成源碼,才是我們最終的十進位數字 下麵是計算過程: 正數取反(123,結果是-124): (1)先將此數變為二進位數,全部位取反(0變1,1變0); (2)由於這個數是補碼,所以要進行再一次 ...


資料庫表

 

 導入依賴

<dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!--Junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--資料庫驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- 資料庫連接池 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--        事務支持-->
        <!--spring事務-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <!--        使用forEach標簽-->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.demidenko05</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.0.5</version>
        </dependency>
    </dependencies>

編寫mybatis-config.xml配置文件,這裡已經綁定了SQL的映射文件,所以在spring的配置中就不要再次引入mapperLocations

<?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.smu.pojo"/>
    </typeAliases>
    <mappers>
      <!--載入sql映射文件-->
<!--        <mapper resource="com/smu/mapper/BooksMapper.xml"/>-->
        <!--Mapper代理方式-->
        <package name="com.smu.mapper"/>
    </mappers>
</configuration>

寫jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
#????MySQL8.0+,???????????; &serverTimezone=Asia/Shanghai
#jdbc.url=jdbc:mysql://localhost:3306/testProject?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.url=jdbc:mysql://localhost:3306/testproject?useSSL=false
jdbc.username=root
jdbc.password=889886hp

spring整合mybatis

編寫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
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!--    DataSource:使用Spring的數據源替換MyBatis的配置 c3p0/dbcp/druid-->
    <!--    1.關聯資料庫配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--    2.連接池
            dbcp:半自動化操作,不能自動連接
            c3p0: 自動化操作(自動化的載入配置文件,並且可以自動設置到對象中!)
            druid:   hikari:
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- c3p0連接池的私有屬性    可預設不使用-->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 關閉連接後不自動commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 獲取連接超時時間 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 當獲取連接失敗重試次數 -->
        <property name="acquireRetryAttempts" value="10"/>
    </bean>

    <!--    3.配置sqlSessionFactory對象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--  註入資料庫連接池  -->
        <property name="dataSource" ref="dataSource"/>
        <!--       綁定MyBatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--        <property name="mapperLocations" value="classpath:com/smu/mapper/BooksMapper.xml"/>-->

    </bean>

    <!--    使用繼承SqlSessionDaoSupport的方式-->
    <bean id="booksMapperImpl" class="com.smu.mapper.BooksMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>


    <!-- 4. 配置dao介面掃描包,動態的實現了Dao介面可以註入到Spring容器中! -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--        註入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--        給出需要掃描的Dao介面包  -->
        <property name="basePackage" value="com.smu.mapper"/>
    </bean>

</beans>

編寫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
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 用於掃描註解 -->
<!-- <context:component-scan base-package="com.smu.service"/>-->
<!-- 使用掃描介面的方式註冊bean-->
<bean id="booksServiceImpl" class="com.smu.service.BooksServiceImpl">
<property name="booksMapper" ref="booksMapper"/>
</bean>
<bean id="usersServiceImpl" class="com.smu.service.UsersServiceImpl">
<property name="usersMapper" ref="usersMapper"/>
</bean>
</beans>

配置總的applicationContext.xml文件用於整合,引入的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="classpath:spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="springmvc.xml"/>
</beans>

sql映射文件需要和mapper介面同一個目錄

 

 service層寫一個業務需要的介面,然後寫一個實現類,實現類註入mapper介面

public class BooksServiceImpl implements BooksService{
    private BooksMapper booksMapper;

    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }

    @Override
    public List<Books> selectAll() {
        return booksMapper.selectAll();
    }

    public Books selectById(int id) {
        return booksMapper.selectById(id);
    }

    @Override
    public int add(Books books) {
        return booksMapper.add(books);
    }

    @Override
    public int deleteById(int id) {
        return booksMapper.deleteById(id);
    }

    @Override
    public int update(Books books) {
        return booksMapper.update(books);
    }

    @Override
    public List<Books> selectByName(String bookName) {
        return booksMapper.selectByName(bookName);
    }
}

此類已經在spring-service.xml中註冊過bean,代碼如下

<bean id="booksServiceImpl" class="com.smu.service.BooksServiceImpl">
        <property name="booksMapper" ref="booksMapper"/>
    </bean>
    <bean id="usersServiceImpl" class="com.smu.service.UsersServiceImpl">
        <property name="usersMapper" ref="usersMapper"/>
    </bean>

寫一個util包,將獲得bean的操作簡化

public class BooksServiceUtil {
    public static BooksServiceImpl getBooksServiceBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        return context.getBean("booksServiceImpl", BooksServiceImpl.class);
    }
}   

spring整合springMVC

在web.xml下部署dispatchSerclet

<?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_4_0.xsd"
         version="4.0">
    <!--    <display-name>springMVC</display-name>-->
    <!-- 部署 DispatcherServlet -->
    <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:applicationContext.xml</param-value>
        </init-param>

        <!-- 表示容器再啟動時立即載入servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 處理所有URL -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--    解決編碼問題-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

編寫springmvc.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:nvc="http://www.springframework.org/schema/mvc"
       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
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!--  配置SpringMVC  -->
    <!--    1.開啟SpringMVC註解驅動 -->
    <nvc:annotation-driven/>

    <!--    2.靜態資源過濾預設servlet配置 -->
    <mvc:default-servlet-handler/>
    <!--    3.掃描相關的controller-->
    <context:component-scan base-package="com.smu.controller"/>

    <!--    處理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--    處理器適配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--    視圖解析器 以後可能會用到的模板引擎Thymeleaf Freemarker-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--        首碼-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--        尾碼-->
        <property name="suffix" value=".jsp"/>
    </bean>

        <!--    攔截器配置-->
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/book/**"/>
                <bean class="com.smu.interceptor.LoginInterceptor"/>
            </mvc:interceptor>
        </mvc:interceptors>

        <!--    文件上傳支持-->
    <!--    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
    <!--        &lt;!&ndash;        請求表單的格式,需要和jsp的pageEncoding一樣,預設為ISO-8859-1&ndash;&gt;-->
    <!--        <property name="defaultEncoding" value="utf-8"/>-->
    <!--        &lt;!&ndash;        上傳文件的大小上限,單位為位元組10485760=10m&ndash;&gt;-->
    <!--        <property name="maxUploadSize" value="10485760"/>-->
    <!--        <property name="maxInMemorySize" value="40960"/>-->
    <!--    </bean>-->
</beans>

controller層的部分代碼截圖

 


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

-Advertisement-
Play Games
更多相關文章
  • ##前言 在 React 開發中如果不去管組件的重覆渲染問題,項目稍微複雜一點性能將不堪入目,下麵將介紹項目中最常見的例子及解決方案(僅 hooks 組件)。 ##預先瞭解所用知識 React.memo React.useCallback React.useMemo 沒錯,只需使用上面三點即可解決大 ...
  • 前臺部分到此結束,一路走來還挺懷念,今天主要是對整個項目的完成做一個最後的收尾工作,對於功能上的需求沒有什麼了,主要就是項目上線的一些註意事項。 一.個人中心二級路由 當我們點擊查看訂單應該跳轉到個人中心 個人中心拆分兩個子路由組件 分好組件後,在routes裡面父組件寫上childre配置項 寫路 ...
  • 前端周刊:2022-9 期 前端開發 網路視頻的防盜與破解 網路視頻(Web 視頻)是指利用 HTML5 技術在瀏覽器中播放的視頻,這類視頻資源通常可以被隨意下載,某些行業(比如教培行業)如果希望保護自己的視頻資源不被下載,就需要對視頻做防盜鏈處理。 Vue 組件復用問題的一個解決方法 關於 vue ...
  • 1. id選擇器 <style> #container { width: 100px; } </style> <body> <div id="container"></div> </body> 2. class選擇器 | 類選擇器 <style> .container { width: 100px; ...
  • Tooltip常用於展示滑鼠 hover 時的提示信息。 而在實際過程中,有這麼一個需求:只有文字內容排不下,出現省略號,才需要顯示tooltip的提示內容。 本文章的思路是通過一個自定義指令實現如下效果:姓名欄位過長時才顯示tooltip ...
  • 緊急通知!更新中.... (一)FastJson反序列化漏洞。據國家網路與信息安全信息通報中心監測發現,阿裡巴巴公司開源Java開發組件FastJson存在反序列化漏洞。FastJson被眾多java軟體作為組件集成,廣泛存在於java應用的服務端代碼中。攻擊者可利用上述漏洞實施任意文件寫入、服務端 ...
  • 摘要:NumPy中包含大量的函數,這些函數的設計初衷是能更方便地使用,掌握解這些函數,可以提升自己的工作效率。這些函數包括數組元素的選取和多項式運算等。下麵通過實例進行詳細瞭解。 前述通過對某公司股票的收盤價的分析,瞭解了某些Numpy的一些函數。通常實際中,某公司的股價被另外一家公司的股價緊緊跟隨 ...
  • 背景 框架之前完成了多數據源的動態切換及事務的處理,想更近一步提供一個簡單的跨庫事務處理功能,經過網上的搜索調研,大致有XA事務/SEGA事務/TCC事務等方案,因為業務主要涉及政府及企業且併發量不大,所以採用XA事務,雖然性能有所損失,但是可以保證數據的強一致性 方案設計 針對註冊的數據源拷貝一份 ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...