Hibernate的简单使用
2016-07-12 00:26
288 查看
首先在eclips中下载hibernate插件http://download.jboss.org/jbosstools/neon/stable/updates/
建立hibernate.cfg.xml
hibernate步骤:
建立User类与数据库中的user表对应,形成持久化对象
User.hbm.xml如下
<class name="org.hibernate.entity.User" table="USER">
<id name="id" type="int">
<column name="USER_ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String" length="20">
<column name="NAME" />
</property>
<property name="password" type="java.lang.String" length="12">
<column name="PASSWORD" />
</property>
<property name="type" type="java.lang.String" length="6">
<column name="TYPE" />
</property>
</class>
其中id标签对应表中的主键
UserDAO接口
public interface UserDAO {
void save(User user);
User findById(int id);
void delete(User user);
void update(User user);
}
UserDAOImpl实现类
public class UserDAOImpl implements UserDAO{
public void save(User user) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
session.save(user);
tx.commit();
} catch(Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession();
}
}
public User findById(int id) {
User user = null;
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
user = (User)session.get(User.class, id);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession();
}
return user;
}
public void delete(User user) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
session.delete(user);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession();
}
}
public void update(User user) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
session.update(user);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession();
}
}
}
HibernateUtil辅助类
private static SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
static {
try {
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch(Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession() throws HibernateException {
Session session = (Session)threadLocal.get();
if(session == null || !session.isOpen()) {
if(sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session)threadLocal.get();
threadLocal.set(null);
if(session != null) {
session.close();
}
}
public static void rebuildSessionFactory() {
try {
configuration.configure("/hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
} catch(Exception e) {
System.out.println("Error Creating SessionFactory");
e.printStackTrace();
}
}
public static void shutdown() {
getSessionFactory().close();
}
}
Configuration--SessionFactory---Session---Transaction
测试类如下
@Test
public void testSave() {
UserDAO userdao = new UserDAOImpl();
try {
User u = new User();
u.setId(22);
u.setName("freshbin000");
u.setPassword("tyb");
u.setType("admin");
userdao.save(u);
} catch (Exception e) {
e.printStackTrace();
}
}
经过一天的努力,终于实现了hibernate的第一个例子
建立hibernate.cfg.xml
hibernate步骤:
建立User类与数据库中的user表对应,形成持久化对象
User.hbm.xml如下
<class name="org.hibernate.entity.User" table="USER">
<id name="id" type="int">
<column name="USER_ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String" length="20">
<column name="NAME" />
</property>
<property name="password" type="java.lang.String" length="12">
<column name="PASSWORD" />
</property>
<property name="type" type="java.lang.String" length="6">
<column name="TYPE" />
</property>
</class>
其中id标签对应表中的主键
UserDAO接口
public interface UserDAO {
void save(User user);
User findById(int id);
void delete(User user);
void update(User user);
}
UserDAOImpl实现类
public class UserDAOImpl implements UserDAO{
public void save(User user) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
session.save(user);
tx.commit();
} catch(Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession();
}
}
public User findById(int id) {
User user = null;
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
user = (User)session.get(User.class, id);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession();
}
return user;
}
public void delete(User user) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
session.delete(user);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession();
}
}
public void update(User user) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
session.update(user);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession();
}
}
}
HibernateUtil辅助类
private static SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
static {
try {
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch(Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession() throws HibernateException {
Session session = (Session)threadLocal.get();
if(session == null || !session.isOpen()) {
if(sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session)threadLocal.get();
threadLocal.set(null);
if(session != null) {
session.close();
}
}
public static void rebuildSessionFactory() {
try {
configuration.configure("/hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
} catch(Exception e) {
System.out.println("Error Creating SessionFactory");
e.printStackTrace();
}
}
public static void shutdown() {
getSessionFactory().close();
}
}
Configuration--SessionFactory---Session---Transaction
测试类如下
@Test
public void testSave() {
UserDAO userdao = new UserDAOImpl();
try {
User u = new User();
u.setId(22);
u.setName("freshbin000");
u.setPassword("tyb");
u.setType("admin");
userdao.save(u);
} catch (Exception e) {
e.printStackTrace();
}
}
经过一天的努力,终于实现了hibernate的第一个例子
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- 数据库链接字符串查询网站
- DB2实例管理
- DB2实例管理
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- 第三章 数据库备份和还原
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法
- 一个简单的asp数据库操作类
- CentOS下DB2数据库安装过程详解
- EasyASP v1.5发布(包含数据库操作类,原clsDbCtrl.asp)第1/2页
- sql2008 还原数据库解决方案
- Oracle 数据库自动存储管理-安装配置
- Oracle数据库执行脚本常用命令小结
- Oracle 数据库 临时数据的处理方法
- 数据库分页查询语句数据库查询
- 最近比较流行的数据库挂马