第二次作业 单例模式的SessionFactory
Posted sangewuxie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二次作业 单例模式的SessionFactory相关的知识,希望对你有一定的参考价值。
一、基础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的主要内容,如果未能解决你的问题,请参考以下文章