# hibernate 環境設定 跟專二專案的差別在於 1. tomcat版本10.1 (context.xml設定資料庫),自己再去下載 2. maven(請參考0512 hibernate筆記) 3. cfg.mxl 設置關聯和資料庫 - server下的context.xml ,跟你專案的cfg.mxl 檔裡面的property標籤 去關聯(資料庫) ``` <property name="hibernate.connection.datasource">java:comp/env/connectSqlServerJdbc/SystemService</property> ``` ```=java //老師上課的版本 //SystemService可以改成自己取的名子 //username和password可以改成自己的資料庫使用者帳號密碼 <Resource name="connectSqlServerJdbc/SystemService" type="javax.sql.DataSource" auth="Container" username="watcher" password="P@ssw0rd" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;databaseName=LeonPower;encrypt=true;trustServerCertificate=true" /> ``` ```=java //自己的版本 <Resource name="connectSqlServerJdbc/GymProject" type="javax.sql.DataSource" auth="Container" username="sa" password="sa1234" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;databaseName=servdb;encrypt=true;trustServerCertificate=true" /> ``` -pom.xml ```=java <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>HibernateWebProject</groupId> <artifactId>HibernateWebProject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> //設置jar檔 dependencies <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>6.2.2.Final</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>11.2.3.jre17</version> </dependency> <dependency> <groupId>jakarta.servlet.jsp.jstl</groupId> <artifactId>jakarta.servlet.jsp.jstl-api</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>jakarta.servlet.jsp.jstl</artifactId> <version>2.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>17</release> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.3</version> </plugin> </plugins> </build> </project> ``` - web.xml 在你new servlet 時候就會自動建立```<servlet-mapping>``` new dynamic project 然後convert to maven project hbm.xml 可以用annotation來取代,是相同功能 cfg.xml 要寫mapping class來註冊映射關係 ``` <mapping class="tw.Leonchen.model.Stock"/> <mapping class="tw.Leonchen.model.StockTransaction"/> ``` ``` mapping resource是舊的,來標示hbm.mxl的,可以不用寫了 <mapping resource="tw/Leonchen/model/HouseBean.hbm.xml"/> ``` --- ![](https://hackmd.io/_uploads/rJ57UKVrh.png) 4.0 改成5.0 tomcat 9 改成10.1 ![](https://hackmd.io/_uploads/HywN8F4Sn.png) 可以解決版本不同import錯誤的問題(JSP) 一改完,紅字就沒了 怎麼改啊 專案右建/properties --- 原本java部分(後端bean/servlet/dao部分如果有錯)要改import改成jakarta ``` import javax.naming.NamingException; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.MultipartConfig; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; import jakarta.servlet.http.Part; ```