1、首先还是引入所须要的包

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

2、然后是配置hibernate.cfg.xml配置文件。连接mysql数据库信息,以及引入其它子模块的映射文件

<hibernate-configuration>
<session-factory>
<!-- 数据库连接信息 -->
<property name="show_sql">true</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property> <mapping resource="/hibernateConfig/Login.hbm.xml" />
</session-factory>
</hibernate-configuration>

3、编写子模块的映射文件,这里是一个简单的登录信息表。Login.hbm.xml

<hibernate-mapping package="com.demo.model">

    <class name="Login" table="login">
<id name="id" column="id">
<generator class="increment"/>
</id>
<property name="username" column="username" length="20"/>
<property name="password" column="password" length="20"/>
</class> </hibernate-mapping>

4、编写model层的对象映射javabean,和普通的javabean没有什么大的差别。仅仅是加了一些构造函数,属性和数据库表的字段相应

public class Login {
private int id;
private String username;
private String password; (getter/setter) public Login() {
} public Login(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
}

5、编写DAO层。DAO负责底层的数据库的一些操作。这里须要实现一个DAO接口,使得业务逻辑组件依赖DAO接口而不是详细实现类,将系统各组件之间的依赖提升到接口层次。避免类层次直接耦合(假如系统有所改变,仅仅要接口层次没有改变,那么依赖该组件的上层组件也不须要改变,从而提供了良好的复用)

LoginDao接口:

public interface LoginDao {
public void saveLogin(Login login); public void deleteLogin(Login login); public void updateLogin(Login login); public Login findLogin(int id); public Login findLogin(String name);
}

LoginDaoImpl实现类:

public class LoginDaoImpl implements LoginDao {
public void deleteLogin(Login login) {
HibernateUtil.delete(login);
} public Login findLogin(int id) {
return (Login) HibernateUtil.findById(Login.class, id);
} public Login findLogin(String name) {
return (Login) HibernateUtil.findByName(name);
} public void saveLogin(Login login) {
HibernateUtil.add(login);
} public void updateLogin(Login login) {
HibernateUtil.update(login);
}
}

6、编写业务逻辑组件service,DAO已经帮我们实现了数据库的操作,在业务逻辑组件中我们则仅仅须要调用DAO组件并关注于业务逻辑的实现就可以

LoginService接口:

public interface LoginService {
public void save(Login login); public void delete(Login login); public void update(Login login); public Login findById(int id); public Login findByName(String name);
}

LoginServiceImpl实现类:

public class LoginServiceImpl implements LoginService {
private LoginDao loginDao; public LoginDao getLoginDao() {
return loginDao;
} public void setLoginDao(LoginDao loginDao) {
this.loginDao = loginDao;
} public void delete(Login login) {
loginDao.deleteLogin(login);
} public Login findById(int id) {
return loginDao.findLogin(id);
} public Login findByName(String name) {
return loginDao.findLogin(name);
} public void save(Login login) {
loginDao.saveLogin(login);
} public void update(Login login) {
loginDao.updateLogin(login);
}
}

7、编写获取hibernate的SessionFactory类的工具类,这里编写一个简单的工具类,一般应用是在spring容器里来管理SessionFactory的

public class HibernateUtil {
private static SessionFactory sf;
static {
Configuration cfg = new Configuration();
cfg.configure("hibernateConfig/hibernate.cfg.xml");
sf = cfg.buildSessionFactory();
} public static Session getSession() {
return sf.openSession();
} public static void add(Object entity) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(entity);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
throw e;
} finally {
if (session != null) {
session.close();
}
}
} public static void delete(Object entity) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.delete(entity);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
throw e;
} finally {
if (session != null) {
session.close();
}
}
} public static void update(Object entity) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.update(entity);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
throw e;
} finally {
if (session != null) {
session.close();
}
}
} public static Object findById(Class clazz, Serializable id) {
Session session = null;
try {
session = HibernateUtil.getSession();
Object ob = session.get(clazz, id);
return ob;
} catch (HibernateException e) {
e.printStackTrace();
throw e;
} finally {
if (session != null) {
session.close();
}
}
} public static Object findByName(String name) {
Session session = null;
try {
session = HibernateUtil.getSession();
Query query = session.createQuery("from test where name = :name");
query.setParameter("name", name);
Object ob = query.uniqueResult();
return ob;
} catch (HibernateException e) {
e.printStackTrace();
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
}

注意:当hibernate.cfg.xml不放在src下时。在这里设置一下,让应用能找到这个配置文件

Configuration cfg = new Configuration();
cfg.configure("hibernateConfig/hibernate.cfg.xml");

8、action中调用业务逻辑组件提供一个保存usernamepassword的实现

public String execute(){
Login login=new Login();
login.setUsername(getUsername());
login.setPassword(getPassword());
ls.save(login);
return SUCCESS;
}

9、在spring配置文件里配置一下各个bean,依赖注入一下

<bean id="loginDao" class="com.demo.dao.daoImpl.LoginDaoImpl" />
<bean id="loginService" class="com.demo.service.serviceImpl.LoginServiceImpl">
<property name="loginDao" ref="loginDao" />
</bean>
<bean id="registerAction" class="com.demo.action.RegisterAction"
scope="prototype">
<property name="ls" ref="loginService" />
</bean>

10、測试

一个简单的注冊页面中输入usernamepassword。点击注冊后保存到数据库中



数据库中保存成功

hibernate4.3.10环境搭建的更多相关文章

  1. struts2+Hibernate4+spring3+EasyUI环境搭建之一:准备工作

    SSHE环境搭建第一步:安装软件(经验:安装软件路径最好不要有空格.括弧.中文等特殊符号)1.Jdk72.tomcat73.maven34.MyEclipse10.7 破解及优化设置(设置本地安装jd ...

  2. struts2+Hibernate4+spring3+EasyUI环境搭建之四:引入hibernate4以及spring3与hibernate4整合

    1.导入hibernate4 jar包:注意之前引入的struts2需要排除javassist  否则冲突 <!-- hibernate4 --> <dependency> & ...

  3. struts2+Hibernate4+spring3+EasyUI环境搭建之三:引入sututs2以及spring与sututs2整合

    1.引入struts2 <!-- struts2 和心包 排除javassist 因为hibernate也有 会发生冲突--> <dependency> <groupId ...

  4. struts2+Hibernate4+spring3+EasyUI环境搭建之二:搭建spring

    三.搭建spring3 1.引入spring3依赖 <!-- spring3 --> <dependency> <groupId>org.springframewo ...

  5. struts2+Hibernate4+spring3+EasyUI环境搭建之五:引入jquery easyui

    1.下载jquery easyui组件     http://www.jeasyui.com/download/index.php 2.解压 放到工程中  如图 3.jsp引入组件:必须按照如下顺序 ...

  6. spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明

    一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 struts2.3 ...

  7. Windows 10 IoT Serials 1 - 针对Minnow Board MAX的Windows 10 IoT开发环境搭建

    目前,微软针对Windows IoT计划支持的硬件包括树莓派2,Minnow Board MAX 和Galileo (Gen 1和Gen 2).其中,Galileo (Gen 1和Gen 2)运行的是 ...

  8. 在macOS Sierra 10.12搭建PHP开发环境

    macOS Sierra 11.12 已经帮我们预装了 Ruby.PHP(5.6).Perl.Python 等常用的脚本语言,以及 Apache HTTP 服务器.由于 nginx 既能作为 HTTP ...

  9. 在 Ubuntu 13.10 中搭建Java开发环境 - 懒人版

    本文记录我在Ubuntu 13.10中搭建Java开发环境. 本文环境: Ubuntu 13.10 x64运行在Win7下的VMware Workstation 10中. 1. 安装JDK与JRE s ...

随机推荐

  1. 简单的php自定义错误日志

    平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的<最佳日志实践>觉得写一个清晰明了,结构分明的日志还是非常有必要的. 在写日志前,我们问问自己:为什么我们有时要记录自定义 ...

  2. antd 父组件获取子组件中form表单的值

    还是拿代码来讲吧,详情见注释 子组件 import React, { Component } from 'react'; import { Form, Input } from 'antd'; con ...

  3. 你的C/C++程序为什么无法运行?揭秘Segmentation fault (1)

    什么让你对C/C++如此恐惧? 晦涩的语法?还是优秀IDE的欠缺? 我想那都不是问题,最多的可能是一个类似这样的错误: 段错误(Segmentation fault) 这是新手无法避免的错误,也是老手 ...

  4. MyBatis insert 返回主键的方法

    数据库:SqlServer2005 表结构: /*==============================================================*//* Table: D ...

  5. 编写简单登陆和注册功能的demo时遇到的问题

    一.注册功能中添加数据不成功 给数据库添加EditText中的内容后,数据库中找不到添加后的数据,并且存在字符串为空的数据 解决方法:EditText registerAccount = (EditT ...

  6. 【scrapy】使用方法概要(三)(转)

    请初学者作为参考,不建议高手看这个浪费时间] 前两篇大概讲述了scrapy的安装及工作流程.这篇文章主要以一个实例来介绍scrapy的开发流程,本想以教程自带的dirbot作为例子,但感觉大家应该最先 ...

  7. 用户空间程序的函数跟踪器 (Function Tracer)

    http://blog.csdn.net/robertsong2004/article/details/38499995

  8. 多线程调试必杀技 - GDB的non-stop模式

     作者:破砂锅  (转) 开源的GDB被广泛使用在Linux.OSX.Unix和各种嵌入式系统(例如手机),这次它又带给我们一个惊喜. 多线程调试之痛 调试器(如VS2008和老版GDB)往往只支持a ...

  9. DTree的改进与使用经验

    1.dtree.js源码 function Node(id, pid, name, url, title, target, icon, iconOpen, open) { this.id = id; ...

  10. 阿里云服务器IIS启用HTTPS协议(转)

    https://www.cnblogs.com/randytech/p/7017188.html