hibernater學習一(第一個程式)
1 搭建hibernate環境。
(1) 首先需要下載三個檔案:hibernate-annotations-3.4.0.GA,hibernate-distribution-3.3.2.GA-dist,slf4j-1.5.8。最後一個檔案是因為hibernate內部用到了slf4j來處理日誌。
(2) 下載後解壓。開啟Eclipse,這裡可以建立自己的庫檔案。需要包含以下jar包:1、hibernate3.jar 2、hibernate lib下的/required資料夾下的jar包。3、slf4j下的slf4j-nop-1.5.8.jar包。可以將這個庫定義為hibernate。然後在工程中新增進這個庫。
(3) 新增mysql所需要的jar包。
2 新建Java類:Student,放在model包下
public class Student {
int id;
String name;
int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.配置hibernate配置檔案:hibernate.cfg.xml,可以參照官方幫助文件去查詢相應的格式。這個檔案一般預設放在根目錄下。
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/xiyang/model/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4. 新建類對映檔案:student.hbm.xml,一般這個檔案和model放在一起。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xiyang.model">
<class name="Student" table="student">
<id name="id"></id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
5.最後寫一個測試檔案,測試是否可以正確存到資料庫裡面。StudentTest
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.xiyang.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("0001");
s.setAge(1);
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
6.控制檯輸出Hibernate: insert into student (name, age, id) values (?, ?, ?),然後去後臺MySQL資料庫裡檢視可一看到資料已經正確的插入。
相關推薦
hibernater學習一(第一個程式)
這是練習的第一個程式。主要完成的任務是建立一個Student類。用hibernate類向資料庫裡新增資料。1 搭建hibernate環境。 (1) 首先需要下載三個檔案:hibernate-annotations-3.4.0.GA,hibernate-dist
學習筆記(第一個程式HelloWorld的解釋)
關鍵字,主函式的概念 class Demo { public static void main(String[] args) { System.out.println("hellow java"); } } 1、在我們這個類中,Demo稱之為類名,cl
springboot學習總結一(Springboot的第一個程式)
Springboot簡介 Springboot是Spring家族中的一個全新的框架,它用來簡化Spring程式的建立和開發過程,也可以說Springboot能簡化我們之前採用Spring mvc + spring + MyBaits 框架進行開發的過程 在以往我們採用S
APUE原始碼使用方法(帶你執行出APUE的第一個程式)
聽說你在看apue但是不知道這麼執行它書上的程式碼,下面我按照如下步驟來吧! 1、獲取原始碼資源 wget http://www.kohala.com/start/apue.linux3.tar.Z -P ~ 2、tar xzvf apue.linux3.tar.Z 3、cd a
Perl語言入門(01 Perl安裝+編輯第一個程式)
由於公司需要,本人最近在學習Perl這種指令碼語言,本文是我在學習Perl的過程中總結出來的一些心得和筆記,希望能夠幫助也在學習Perl的各位同僚。廢話不多說直接上乾貨!!! ——————————————————————————--乾貨分割線-—————————————
Java學習筆記(第一次課)
stdio.h println 標準 ant 方法參數 ann 控制 大小寫 protect 1.簡單的Java語言模板 package inclass1_1; import java.io.*;//導入包(相當於c語言的#include<stdio.h>)
Git系統從0到1的完整學習歷程(第一節 起步)
主要跟著https://gitee.com/progit/index.html來學習的,知識點來自這裡,新增自己的理解和想法。 首先了解什麼是Git?個人理解就是一個設計合理的版本控制系統(VCS)。 介紹本地版本控制系統、集中化版本控制系統、分佈版本控制系統。 在git出現之前,採
kafka學習一(基本命令篇)
1、前臺程序啟動: ./kafka-server-start.sh ../config/server.properties & 2、後臺程序啟動: ./kafka-server-start.sh -daemon ../config/server.properti
go學習一 (開發環境安裝)
1,首先下載golang原始碼安裝包(https://www.golangtc.com/download),windows版本一般為 go1.9.2.windows-amd64.msi, 直接執行安裝,預設安裝到C:\Go,安裝完後設置系統PATH,增加C:\Go
Android學習筆記(第一行程式碼) 做一個簡單的登入介面
一.建立xml檔案 1.賬戶輸入框和密碼輸入框的編寫 <TextView android:layout_height="wrap_content" android:text="@string/LG_name" />
kaggle房價預測 第一次練習總結(第一個模型)
kaggle房價預測參考danB連結:https://www.kaggle.com/learn/machine-learning以下是用到的輸出#print(original_data.isnull().sum()) #統計na的總數 #print(original_data
ASP.NET C#學習一(環境基礎知識)
什麼是.NET? 是微軟推出的開發動態WEB應用程式的開發平臺 什麼是ASP.NET? 是.NET的一部分,通過.NET平臺來開發ASP.NET 什麼是IIS? 是微軟的WEB伺服器,開發人員可以通過
Three.JS 新增燈光、材質和陰影(第一個例子)
three.js中都多種燈光和材質,這裡只是添加了一種燈光,聚光燈spotLight。 首先構建一個spotLight物件: var spotLight = new THREE.SpotLight(0xFFFFFF); 指定光源的位置,從何處開始照
OpenCV學習筆記(一)——安裝配置、第一個程式
決心開始研究OpenCV。閒言少敘,sourceforge網站最近的版本是2011年8月的OpenCV2.3.1,下載安裝,我這裡使用的開發環境是vs2008,網上搜了一下配置的教程,與之前的幾個OpenCV版本的配置過程大體相同:(教程網上很多,知之為知之,不知百度之,我
安卓學習筆記 -- (安裝環境) Android Studio安裝配置、環境搭建詳細步驟及基本使用 Android Studio和SDK官方開發工具下載 Android Studio教程從入門到精通 Android開發-之第一個程式:HelloWorld!
1、下載Android Studio安裝配置、環境搭建詳細步驟及基本使用 https://www.cnblogs.com/yanglh6-jyx/p/Android_AS_Configuration.html https://blog.csdn.net/k491022087/ar
Mybatis學習筆記(1)——第一個程式
暑期之前就有打算學習SSM,但是我記得當時再配置一個框架瘋狂報錯,弄得我很難受,,再加上當時有點其他事情,所以就放了下來。現在很有需求要會ssm,所以就學了一下。感覺框架這東西配置就煩的要死。錯一丁點就全錯。。下面開始說配置詳情。 介紹一下這些東西,將mybatis的jar包依賴放
Turbo51學習筆記(4) 第一個程式
在高階語言裡,“Hello world"是經典的第一個程式;在與硬體密切相關的嵌入式程式設計裡,點亮LED燈則更具有代表性。 下面的程式碼實現一個LED燈的明、暗迴圈: Program led_test; const flash_time_limit_1=$A0;
入門機器學習的第一個程式!(python版)
一. 前言 前兩天看到了網上一個叫做機器學習中的hello world的入門視訊。覺得非常有意思,也進一步對機器學習有了感性認識,覺得非常適合我們瞭解和認識機器學習這一當前的熱門領域。這篇文章是對它的
OpenGL學習筆記(2)第一個程式——猶他茶壺(Teapot)
好了,python+opengl的開發環境搭建好後,我們就可以開始學習了。 這裡,我們先學習一個常見的例子——猶他茶壺。 先貼程式碼 fromOpenGL.GL import*fromOpenGL.GLU import*fromOpenGL.GLUT import
【記錄】STM32學習之第一個程式(點亮LED)
STM32學習之第一個程式(點亮LED) 開發板:Nucelo L053R8 芯 片:STM32L053R8 軟 件:IAR for ARM 7.4, STM32CubeL0 程式碼示例