第二次作業 單例模式的SessionFactory
阿新 • • 發佈:2018-05-13
logs ide clas dial query 代碼塊 ssi rri transacti
一、基礎Hibernate環境搭建(參見http://www.cnblogs.com/sangewuxie/p/9004968.html)
二、實體類User及User.hbm.xml配置
1.User類
package com.domain; public class User { private Integer id; private String username; private String password; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]"; } }
2.User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- name: 完整類名 table:數據庫表名 --> <class name="com.domain.User" table="user"> <!-- id配置表的主鍵 name代表表中屬性 --> <id name="id" column="id"> <!-- 主鍵生成策略 --> <generator class="native"></generator> </id> <property name="username" column="username" type="string"></property> <property name="password" column="password" type="string"></property> <property name="age" column="age" type="integer"></property> </class> </hibernate-mapping>
三、hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 指定方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 數據庫驅動 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 數據庫url --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate02_1514010311</property> <!-- 數據庫連接用戶名 --> <property name="hibernate.connection.username">root</property> <!-- 數據庫連接密碼 --> <property name="hibernate.connection.password">0x3137</property> <!-- 將hibernate生成的sql語句打印到控制臺 --> <property name="hibernate.show_sql">true</property> <!-- 將hibernate生成的sql語句格式化(語法縮進) --> <property name="hibernate.format_sql">true</property> <mapping resource="com/domain/User.hbm.xml" /> </session-factory> </hibernate-configuration>
四、HibernateUtil
在這個工具類中寫一個通過靜態代碼塊生成唯一的SessionFactory,通過一個方法返回一個SessionFactory
package com.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static Configuration cfg; private static SessionFactory sessionFactory; static { try { cfg = new Configuration().configure(); sessionFactory = cfg.buildSessionFactory(); }catch (Exception e) { e.printStackTrace(); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
五、測試類TestUser
package com.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.junit.Test; import com.domain.User; import com.util.HibernateUtil; public class TestUser { @Test public void saveUser() { SessionFactory sessionFactory = null; Session session = null; Transaction transaction = null; User user = new User(); user.setUsername("三哥無邪"); user.setPassword("1514010311"); user.setAge(21); try { sessionFactory = HibernateUtil.getSessionFactory(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); session.save(user); transaction.commit(); } catch (Exception e) { if (transaction!=null) { transaction.rollback(); } e.printStackTrace(); }finally { if(session!=null) { session.close(); } } } @Test public void updateUser() { SessionFactory sessionFactory = null; Session session = null; Transaction transaction = null; try { sessionFactory = HibernateUtil.getSessionFactory(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); User user = session.get(User.class, 1); user.setUsername("三哥無邪啊"); user.setPassword("1514010311"); user.setAge(21); session.update(user); transaction.commit(); } catch (Exception e) { if (transaction!=null) { transaction.rollback(); } e.printStackTrace(); }finally { if(session!=null) { session.close(); } } } @Test public void queryUser() { SessionFactory sessionFactory = null; Session session = null; Transaction transaction = null; try { sessionFactory = HibernateUtil.getSessionFactory(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); User user = session.get(User.class, 2); System.out.println("ID:"+user.getId()+" 用戶名:"+user.getUsername()+" 密碼:"+user.getPassword()); transaction.commit(); } catch (Exception e) { if (transaction!=null) { transaction.rollback(); } e.printStackTrace(); }finally { if(session!=null) { session.close(); } } } @Test public void deleteUser() { SessionFactory sessionFactory = null; Session session = null; Transaction transaction = null; try { sessionFactory = HibernateUtil.getSessionFactory(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); User user = session.get(User.class, 1); session.delete(user); transaction.commit(); } catch (Exception e) { if (transaction!=null) { transaction.rollback(); } e.printStackTrace(); } } }
測試一次saveUser方法,結果如圖
就此獲取單例模式的SessionFactory的方式就完成了
第二次作業 單例模式的SessionFactory