1? 準備
?
首先在mysql數據庫中建表User,并添加相關信息。
user表結構如下。
+-------+--------------+------+-----+---------+----------------+
| Field????| Type??????????? ?|???Null |??Key |???Default |????? Extra????????? |
+-------+--------------+------+-----+---------+----------------+
| id?????? ?| int(11)??????????| NO?? ?| PRI?? | NULL?? ? | auto_increment |
| name ? | varchar(255) | YES?? |????? ??| NULL?????|??????????????????????? |
| pwd??? ?| varchar(255) | YES? ?|?????? ?| NULL??? ?|?????????????????????? ?|
+-------+--------------+------+-----+---------+----------------+
建立User類,添加set,get 方法。
使用hibernate對數據進行增刪改差的步驟:
- 獲得連接
- 開啟事務
- 增刪改查操作
- 提交事務
- 關閉連接
?
2? 對數據進行添加(insert)
?
? ? 插入操作使用 session.save(類對象)如上述代碼14行所示。
?? ?可以使用session.saveorupdate(類對象),由hibernate判斷是添加還是更新。
?
???????????? ?save是返回插入數據的主見的,而saveOrUpdate是void
? save方法更適用與確定了是要插入,而且需要得到插入數據的主鍵而saveOrUpdate更傾向于不確定是插入還是更新,而且你不需要得到他的主鍵?
? 另一方面,如果你無法確定你要插入或更新的對象是不是持久態或游離態時。如果你save一個持久態或更新一個游離態,這都是有問題的,
? 此時你就要用到???? saveOrUpdat
? 總體來說,如果你能確定你即將操作對象的狀態,則不需要用saveOrUpdate
?
1 public void TestInsert() throws Exception{
2 User user= new User(); //初始化User
3 user.setId(1); //設置屬性的數據
4 user.setName("藍冰竹齋");
5 user.setPwd("123");
6
7 Configuration config=new Configuration().configure(); // 加載總配置文件
8 SessionFactory sessionFactory= config.buildSessionFactory(); // 建立工廠
9 Session session=null; //定義Session
10 Transaction tx=null; // 定義事務
11 try{
12 session=sessionFactory.openSession(); // 通過工廠建立連接
13 tx=session.beginTransaction(); // 通過連接開啟事務
14 session.save(user); // 通過連接保存user
15 tx.commit(); // 提交
16
17 }catch(Exception e){
18 tx.rollback(); // 出現異常回滾
19
20 }finally{
21 if(session!=null){
22 session.close(); // 關閉連接
23 }
24 if(sessionFactory!=null){
25 sessionFactory.close(); // 關閉連接工廠
26 }
27 }
28 }
測試后打印出的語句:Hibernate: insert into user (name, pwd) values (?, ?)
?
3?對數據進行修改(update)
?
?修改操作使用session.save(類對象),根據表id知道要修改的對象。 如果id不存在,出現異常org.hibernate.StaleStateException
?如果某列不需要更新,需要在映射文件的相應<property>中加入 update="false",如不更新name,
<propertyname="pwd" column="pwd"? update="false"></property>
? 更新時需要將表中所有字段數據進行設置,不設置的字段,其值默認為null.
?可以使用session.saveorupdate(類對象),由hibernate判斷是添加還是更新。
?
public void TestUpdate() throws Exception{
User user= new User();
user.setId(1);
user.setName("藍冰"); //對name進行了修改
user.setPwd("1321121"); //對pwd進行了修改
Configuration config=new Configuration().configure();
SessionFactory sessionFactory= config.buildSessionFactory();
Session session=null;
Transaction tx=null;
try{
session=sessionFactory.openSession();
tx=session.beginTransaction();
session.update(user); //修改數據
tx.commit();
}catch(Exception e){
tx.rollback();
}finally{
if(session!=null){
session.close();
}
if(sessionFactory!=null){
sessionFactory.close();
}
}
}
Hibernate: update user set name=?, pwd=? where id=?
?
4?刪除數據(deleted)
?
修改操作使用session.delete(類對象)
注意,只能通過id來刪除數據,不能通過title或content來刪除,會報缺少標示符錯誤。
public void TestDelete() throws Exception{
User user= new User();
user.setId(1);
user.setName("藍冰竹齋1"); //根據ID刪除,設置其他屬性不起作用
user.setPwd("1321121");
Configuration config=new Configuration().configure();
SessionFactory sessionFactory= config.buildSessionFactory();
Session session=null;
Transaction tx=null;
try{
session=sessionFactory.openSession();
tx=session.beginTransaction();
session.delete(user); //刪除數據
tx.commit();
}catch(Exception e){
tx.rollback();
}finally{
if(session!=null){
session.close();
}
if(sessionFactory!=null){
sessionFactory.close();
}
}
}
Hibernate: delete from user where id=?
?
5?查詢數據(select)
?
通過ID進行查詢。
查詢有兩種方式,1 session.get(類.class,id);
????????????????????? 2 session.load(類.class,id);
??????????????????????????? 類名.class 返回這個類的Class類型對象
load 支持懶加載,既使用對象時才執行。調用getID() ?和 getclass()兩個方法不執行。
兩者的區別會在后面詳細說明。
public void TestSelect() throws Exception{
User user1=null; // 用戶1,使用get方法查詢
User user2=null; // 用戶2,使用load方法查詢
Configuration config=new Configuration().configure();
SessionFactory sessionFactory= config.buildSessionFactory();
Session session=null;
Transaction tx=null;
try{
session=sessionFactory.openSession();
tx=session.beginTransaction();
user1=(User) session.get(User.class, 3); //用戶1 get查詢 get方法中第一個字段是查詢的類.class,第二個參數是id
user2=(User) session.load(User.class, 3); //用戶2 load查詢 參數同上
System.out.println(user1.getName()+"user1用戶");
System.out.println(user2.getPwd()+"user2用戶");
tx.commit();
}catch(Exception e){
tx.rollback();
}finally{
if(session!=null){
session.close();
}
if(sessionFactory!=null){
sessionFactory.close();
}
}
}
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.pwd as pwd0_0_ from user user0_ where user0_.id=?