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. hdu 4676 Sum Of Gcd 莫队+phi反演

    Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...

  2. Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate

    C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. HDU 5137 How Many Maos Does the Guanxi Worth 最短路 dijkstra

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...

  4. NOIP 2002提高组 选数 dfs/暴力

    1008 选数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,…, ...

  5. js取float型小数点后x位数的方法

    js中取小数点后两位方法最常用的就是四舍五入函数了,前面我介绍过js中四舍五入一此常用函数,这里正好用上,下面我们一起来看取float型小数点后两位一些方法总结 以下我们将为大家介绍 JavaScri ...

  6. 从零开始搭建linux下laravel 5.5所需环境(一)

    首先你需要有一台linux服务器,或者虚拟机,这里就不赘述了,不会的可以自行百度. 我这里准备的是一台腾讯云服务器,系统为CentOS 7.4 64位. 你可以使用腾讯云的登录按钮登录到服务器,也可以 ...

  7. jquery的closest方法和parents方法的区别

    今天第一次看到closest方法,以前也从来没用过. 该方法从元素本身开始往上查找,返回最近的匹配的祖先元素. 1.closest查找开始于自身,parents开始于元素父级 2.closest向上查 ...

  8. linux strace追踪mysql执行语句 (mysqld --debug)

    转载请注明出处:使用strace追踪多个进程 http://www.ttlsa.com/html/1841.html http://blog.itpub.net/26250550/viewspace- ...

  9. 【leetcode】sort list(python)

    链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 ...

  10. Bootstrap 3之美04-自定义CSS、Theme、Package

    本篇主要包括: ■  自定义CSS■  自定义Theme■  自定义Package 自定义CSS 有时候,需要自定义或重写Bootstrap默认的CSS.→在css文件夹下创建一个site.css→假 ...