在Java Web程序中使用Hibernate
在Java Web程序中使用Hibernate与普通Java程序一样。本文中将使用Servlet和JSP结合Hibernate实现数据库表的增删改查操作。
Web程序中,hibernate.cfg.xml中必须配置current_session_context_class参数。如果使用JBoss等内置Hibernae的容器,参数值要配置为jta,其他容器(如Tomcat等)需要配置为thread。
1. 创建工程并搭建Hibernate框架
在MyEclipse中创建一个Web工程,工程名为hibernate_web,把MySQL数据库驱动包和JSTL需要的jar包复制到WebRoot/WEB-INF/lib目录下;然后使用MyEclipse向导把Hibernate的jar包导到工程中。关于搭建Hibernate框架,可以参考网上的教程,这里就不再介绍了。接着,使用Hibernate连接数据库,并通过数据库表自动生成数据库对应的实体类和实体类映射文件。所使用的数据库表是MySQL的bank数据库中的users表。
自动生成及修改的代码如下:
package com.cn.vo; /**
* UsersVo entity. @author MyEclipse Persistence Tools
*/ public class UsersVo implements java.io.Serializable { // Fields private Integer id;
private String name;
private Integer age;
private String tel;
private String address; // Constructors /** default constructor */
public UsersVo() {
} /** minimal constructor */
public UsersVo(Integer id) {
this.id = id;
} /** full constructor */
public UsersVo(Integer id, String name, Integer age, String tel,
String address) {
this.id = id;
this.name = name;
this.age = age;
this.tel = tel;
this.address = address;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return this.age;
} public void setAge(Integer age) {
this.age = age;
} public String getTel() {
return this.tel;
} public void setTel(String tel) {
this.tel = tel;
} public String getAddress() {
return this.address;
} public void setAddress(String address) {
this.address = address;
} }
UsersVo.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.cn.vo.UsersVo" table="users" catalog="bank">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
<property name="tel" type="java.lang.String">
<column name="tel" length="20" />
</property>
<property name="address" type="java.lang.String">
<column name="address" length="50" />
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml(修改)
<?xml version='1.0' encoding='gbk'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<!-- 声明使用SQL语句是MySQL数据库的SQL语句(MySQL方言) -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property> <!-- 在控制台打印执行的SQL语句 -->
<property name="show_sql">true</property> <!-- 指定Hibernate启动的时候自动更新表,如果不存在则创建 -->
<property name="hbm2ddl.auto">update</property> <!-- 使用线程,防止遇到异常 -->
<property name="current_session_context_class">thread</property> <!-- JDBC配置代码包括数据库驱动、用户名密码及连接地址 -->
<property name="connection.url">
jdbc:mysql://localhost:3306/bank
</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property> <!-- 指定Hibernate的映射文件 -->
<mapping resource="com/cn/vo/UsersVo.hbm.xml" /> </session-factory> </hibernate-configuration>
HibernateSessionFactory.java
package com.cn.hibernate; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration; /**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
} /**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
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;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
} }
2. 编写数据持久层
为了使程序结构清晰,数据持久层独立出来,放在DAO层中,在DAO层的类中编写数据的增删修改查方法,通过这些方法操作数据。在使用时,只需要根据实际情况来调用DAO层中的方法就可以了。这个例子中,DAO层只有一个类,类名为HibernateDao,HibernateDao类的代码如下:
package com.cn.dao; import org.hibernate.HibernateException;
import org.hibernate.Session; import java.util.List; import com.cn.hibernate.HibernateSessionFactory;
import com.cn.vo.UsersVo; public class HibernateDao {
//添加数据的方法
public void add(UsersVo usersVo){
//调用HibernateSessionFactory的会话
Session session = HibernateSessionFactory.getSession();
try {
session.beginTransaction(); //开启事务
session.persist(usersVo); //将对象添加到数据库
session.getTransaction().commit(); //提交事务 } catch (Exception e) {
session.getTransaction().rollback(); //回滚事务
} finally {
session.close(); //关闭session
}
} //修改数据的方法
public void modifyUsers(UsersVo usersVo){
Session session = HibernateSessionFactory.getSession();
try {
session.beginTransaction(); //开启事务
session.update(usersVo); //修改数据
session.getTransaction().commit(); //提交事务
} catch (Exception e) {
session.getTransaction().rollback(); //回滚事务
} finally {
session.close(); //关闭session
}
} //从表中删除数据
public void delete(int id){
Session session = HibernateSessionFactory.getSession();
try {
session.beginTransaction();
UsersVo users = (UsersVo)session.get(UsersVo.class, id);
session.delete(users);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
} finally {
session.close();
}
} //根据id查找数据
@SuppressWarnings("unchecked")
public UsersVo queryUsersById(int id){
Session session = HibernateSessionFactory.getSession();
UsersVo users = (UsersVo)session.get(UsersVo.class, id);
return users;
} //查找多条数据
@SuppressWarnings("unchecked")
public List<UsersVo> showlist(String hql){
Session session = HibernateSessionFactory.getSession();
try {
session.beginTransaction();
return session.createQuery(hql).list(); //使用HQL查询结果,返回List对象
} catch (Exception e) {
session.getTransaction().rollback();
} finally {
session.getTransaction().commit();
session.close();
}
return null;
}
}
该类中接受UsersVo类,Hibernate能够判断实体类的类型,决定操作哪个数据表。HibernateDao封装了最基本的CURD操作。
在Java Web程序中使用Hibernate的更多相关文章
- 在Java Web程序中使用监听器可以通过以下两种方法
之前学习了很多涉及servlet的内容,本小结我们说一下监听器,说起监听器,编过桌面程序和手机App的都不陌生,常见的套路都是拖一个控件,然后给它绑定一个监听器,即可以对该对象的事件进行监听以便发生响 ...
- java web程序中项目名的更改(http://localhost:8080/)后面的名字
在MyEclipse中的Web项目,如果想另换名称(Context-root)进行发布,可以在项目的属性中进行设置.设置路径如下: 右击项目XX(或选中项目XX,按快捷键Alt+Enter), 打开项 ...
- 在Java Web项目中添加定时任务
在Java Web程序中加入定时任务,这里介绍两种方式:1.使用监听器注入:2.使用Spring注解@Scheduled注入. 推荐使用第二种形式. 一.使用监听器注入 ①:创建监听器类: impor ...
- 那个执事,争先:我如何于 2015 年在 Java Web 项目中推动 HTTP/2
2015 年 5 月,HTTP/2 发布. 2015 年第 3 季度,我所在企业的一个战略级客户(而且是第二大客户)说,他们需要在当年年底之前支持 HTTP/2(原因忘了,且与本文无关,从略). 而在 ...
- (转)Java web 项目中文件路径
文件路径分为绝对路径和相对路径,在项目中页面跳转.配置文件读写.文件上传下载等等许多地方都涉及到文件路径问题. 一篇好文转载于此:http://blog.csdn.net/shendl/archive ...
- JAVA WEB项目中各种路径的获取
JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报 分类: JAVA开发(41) 1.可以在s ...
- Java Web开发中MVC设计模式简介
一.有关Java Web与MVC设计模式 学习过基本Java Web开发的人都已经了解了如何编写基本的Servlet,如何编写jsp及如何更新浏览器中显示的内容.但是我们之前自己编写的应用一般存在无条 ...
- Java Web程序工作原理
Web开发的最重要的基本功能是HTTP:Java Web开发的最重要的基本功是Servlet Specification.HTTP和Servlet Specitication对于Web Server和 ...
- 对Java Web项目中路径的理解
第一个:文件分隔符 坑比Window.window分隔符 用\;unix采用/.于是用File.separator来跨平台 请注意:这是文件路径.在File f = new File(“c:\\hah ...
随机推荐
- PHP程序员未来路在何方
PHP 从诞生到现在已经有20多年历史,从Web时代兴起到移动互联网退潮,互联网领域各种编程语言和技术层出不穷, Node.js . GO . Python 不断地在挑战 PHP 的地位.这些技术的推 ...
- CodeForces - 600C Make Palindrome 贪心
A string is called palindrome if it reads the same from left to right and from right to left. For ex ...
- Spring Boot使用Log4j Implemented Over SLF4J生成日志并在控制台打印
Spring Boot设置切面,执行方法的时候在控制台打印出来,并生成日志文件 引入依赖: <!--日志--> <dependency> <groupId>org. ...
- 【基础知识】.Net基础加强第01天
1.#region *** 可以将一个代码块折叠起来 #endregion 2.Visiual stdio 快捷方式 Ctrl + K + C //注释代码 Ctrl + K + U //取消代码注释 ...
- JAVA基础部分面试
1:面向对象编程有很多重要的特性: 封装,继承,多态和抽象. 2:什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? (1)Java虚拟机是一个可以执行Java字节码的虚拟机进程.J ...
- [ 转载 ] Java中常用的设计模式
Java中常用的设计模式 1.单例模式 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建自己的唯一实例. 3.单例类必须给所有其他对象提供这一实例. 单例模式确保某个类只有一个 ...
- [ 原创 ]学习笔记-Android中隐式Intent 的使用
Android中Intent的使用分为显示Intent和隐式Intent 之前已经介绍过显示Intent的用法了,今天来介绍一下隐式Intent的用法. 当我们在使用一款软件时,如果需要从该软件内部开 ...
- HDU 5682 zxa and leaf 二分 树形dp
zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...
- BZOJ 1007: [HNOI2008]水平可见直线 栈/计算几何
1007: [HNOI2008]水平可见直线 Time Limit: 1 Sec Memory Limit: 162 MB 题目连接 http://www.lydsy.com/JudgeOnline ...
- 通过yum来安装vsftpd
Linux系统:centos6.6. 安装步骤 1.通过yum来安装vsftpd [root@localhost ~]# yum -y install vsftpd 2.设置为开机启动 [root ...