ssh整合思想 Spring與Hibernate的整合 項目在伺服器啟動則自動創建資料庫表

来源:https://www.cnblogs.com/qingyundian/archive/2018/01/01/8167727.html
-Advertisement-
Play Games

Spring整合Hibernate Spring的Web項目中,web.xml文件會自動載入,以出現歡迎首頁。也可以在這個文件中對Spring的配置文件進行監聽,自啟動配置文件, 以及之前的整合Struts2,放置過濾器 在Spring的核心配置文件中,進行資料庫連接池配置,建立sessionFac ...


Spring整合Hibernate

Spring的Web項目中,web.xml文件會自動載入,以出現歡迎首頁。也可以在這個文件中對Spring的配置文件進行監聽,自啟動配置文件,

以及之前的整合Struts2,放置過濾器

<?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>2017-12-30_SSH</display-name>
  
  <!-- 監聽的文件名 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:bean.xml</param-value>
  </context-param>
  

    <!-- 伺服器啟動自動載入XML配置文件 監聽器 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
      <!-- structs2過濾器 -->
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  <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>
</web-app>

在Spring的核心配置文件中,進行資料庫連接池配置,建立sessionFactory對象,直接dao操作,也可以在Spring配置文件中配置Struts2的Action對象

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- c3p0連接池得到dataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lastday"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    
    <bean id="userAction" class="com.swift.action.UserAction" scope="prototype"></bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <!-- Hibernate核心配置文件沒有連接資料庫,所以需要註入 -->
    <property name="dataSource" ref="dataSource"></property>
    <!-- Hibernate核心配置文件的位置 -->
    <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
    </bean>

</beans>

Hibernate的核心配置文件中,不需要在寫連接資料庫的屬性,因為已經在Spring的配置文件中用連接池了

<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC   
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sw_database</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property> -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
        
        
        <property name="hibernate.show_sql">true</property>

        <!-- create: 先刪表,再建表。 create-drop: 啟動時建表,退出前刪表。 update: 如果表結構不一致,就創建或更新。 
            validate: 啟動時驗證表結構,如果不致就拋異常。 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        

        <!--指定映射文件,可映射多個映射文件 -->
        <mapping resource="com/swift/entity/User.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>  

Hibernate的這個核心配置文件需要實體類映射文件,體現映射關係

<?xml version="1.0" encoding='UTF-8'?>     
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 實體類映射文件 -->
<hibernate-mapping>

    <class name="com.swift.entity.User" table="t_user">
        <!-- 主鍵 -->
        <id name="uid">
            <generator class="native"></generator>
        </id>
        <!-- 其他屬性 -->
        <property name="username"/>
        <property name="address"/>
    </class>
    
</hibernate-mapping>

實體類的屬性設置setter和getter方法即可

package com.swift.entity;

public class User {
    
    private Integer uid;
    private String username;
    private String address;
    public Integer getUid() {
        return uid;
    }
    public void setUid(Integer uid) {
        this.uid = uid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    
}

Struts2的UserAction類代碼

package com.swift.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
    
    @Override
    public String execute() throws Exception {
        System.out.println("action..................");
        return NONE;
    }

}

需要繼承ActionSupport類,覆寫execute()方法

在瀏覽器中運行項目後的地址,加上對象名.action(如userAction.action)就可以了

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
 
<struts>
    <package name="default" extends="struts-default" namespace="/">
    
    <!-- action的class不要寫全名會創建兩個對象,而寫Spring配置文件中id的內容,只建一個對象
                  前提有struts2-spring-plugin-2.3.4.1.jar -->
        
        <action name="userAction" class="userAction">
        </action>
    </package>
</struts>

 


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

-Advertisement-
Play Games
更多相關文章
  • 這周遇到了一個需要處理String字元串的問題,用到了split將字元串解析為一個String的數組,還用到了某些替換字元的操作。 String類適用於描述字元串事物。 那麼它就提供了多個方法對字元串進行操作,現在將Java中一些常用的對String的操作整理如下。 常用的方法如下: 1、獲取: 2 ...
  • uWSGI伺服器遇到invalid request block size錯誤解決方法 ...
  • #server 同時進行了md5加密 1 import socket 2 import hashlib 3 4 client = socket.socket() 5 client.connect(('localhost',9998)) 6 while True: 7 cmd = input("請輸入... ...
  • 微信最新的小程式裡面出了個叫“跳一跳”的小游戲,一經推出立馬刷爆了朋友圈,而一些大神們也通過Python實現了自動玩游戲具體代碼見(Github地址:https://github.com/wangshub/wechat_jump_game)。我也通過一些研究成功搭建了這個程式運行的環境,在朋友圈小刷 ...
  • Dict 1 使用鍵-值(key-value)存儲,具有極快的查找速度。 2 eg:d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} >>> d['Michael'] 95 3 要避免key不存在的錯誤,有兩種辦法,一是通過in判斷key是否存在: 二是通過di ...
  • 工作中,需要處理與另一方系統數據交換的問題,採用的是調用遠程介面的方法,數據格式選擇的是json,今天就來聊一聊json,主要分析json數據和java Bean之間的轉換問題。 一、json是什麼 json,全稱是JavaScript Object Notation,中文翻譯是JS對象標記語言,是 ...
  • 官方描述Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open.Python是一個易於學習且功能強大的編程語言.他具有高效率的數據結構,... ...
  • 如何在 Linux 上安裝 Nginx 1、下載 nginx 鏈接 : https://pan.baidu.com/s/1sll0Hrf 密碼 : xnem 2、終端依次執行下麵命令 3、解壓 4、進入解壓後的目錄 5、使用 configure 命令創建一 Makefile 文件 ( 直接在終端中輸 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...