在Java应用总使用hibernate的步骤:
1创建hibernate的配置文件
2创建持久化类
3创建对象-关系映射文件
4通过hibernate API编写访问数据库的代码
:主键的生成方式,此为递增
:name指javabean中的属性名字,column指数据库中对应表的字段
创建持久化类:
1持久化类符合javabean的规范,包含一些属性,以及与之对应的getxxx()和setxxx()方法。
2持久化类有一个id属性,用来唯一标志Person类的每个对象。在面向对象术语中,这个id属性被称为对象标识符
3Hibernate要求持久化类必须提供一个不带参数的默认构造方法
一个SessionFactory实例对应着一个数据存储源,应用从SessionFactory中获得Session实例
Session也被成为持久化管理器,它提供了和持久化相关的操作,如添加、更新、删除、加载和查询对象
注意:此session非彼session(HttpSession)
HQL面向的是类(对象)而不是数据库中的表,这是与SQL之间的差别。
Session的get的load方法都可以获取相应的持久化对象,如果该对象存在,那么这两个方法的行为是一样的;如果该对象不存在,那么get方法会返回null而load方法则抛出异常
setAutoCommit(boolean autoCommit):事物是否自动提交,一般设置为false(比如有5组实物,前4组成功,但第5组失败,则必须全部认为失败回滚,若为true则前4组自动提交)
我也刚刚实训过 最后做的是SSH的项目 说说我的理解和项目实际中用到的你的那些概念
action是表示层的 负责调用业务逻辑的服务组件 service是业务逻辑层的 实际上是调用了DAO的种种方法 DAO类里面就是对于数据库的操作了 .DAO是底层与数据库直接交互的部分,serivce是又对DAO进行了一次封装。而service是暴露给action的部分。
aciton里面调用service,service调用DAO
Person.hbm.xml
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"">
hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"">
jdbc:mysql://localhost:3306/hibernate
root
yanqunqq
com.mysql.jdbc.Driver
org.hibernate.dialect.MySQL5Dialect
true
struts.xml
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"">
/listAll.jsp
/listAll.jsp
/getSinglePerson.jsp
/updatePerson.jsp
/listAll.jsp
PersonAction.java
package com.shengsiyuan.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.hibernate.sql.Update;
import com.opensymphony.xwork2.ActionSupport;
import com.shengsiyuan.model.Person;
import com.shengsiyuan.service.PersonService;
import com.shengsiyuan.service.impl.PersonServiceImpl;
public class PersonAction extends ActionSupport
{
private int id;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
private String username;
private String password;
private int age;
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 int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String savePerson() throws Exception
{
Person person = new Person();
person.setUsername(username);
person.setPassword(password);
person.setAge(age);
java.sql.Date registerDate = new java.sql.Date(
new java.util.Date().getTime());
person.setRegisterDate(registerDate);
PersonService personService = new PersonServiceImpl();
personService.savePerson(person);
List
list = personService.listAllPersons();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("list", list);
return SUCCESS;
}
public String deletePerson() throws Exception
{
PersonService personService = new PersonServiceImpl();
personService.removePerson(id);
List
list = personService.listAllPersons();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("list", list);
return SUCCESS;
}
public String getSinglePerson() throws Exception
{
PersonService personService = new PersonServiceImpl();
Person person = personService.getSinglePersonByid(id);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("person", person);
return SUCCESS;
}
private String updatePerson() throws Exception
{
PersonService personService = new PersonServiceImpl();
Person person = personService.getSinglePersonByid(id);
person.setPassword(password);
person.setAge(age) ;
personService.updatePerson(person) ;
List
list = personService.listAllPersons();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("list", list);
return SUCCESS;
}
}
PersonDAO.java
package com.shengsiyuan.dao;
import java.util.List;
import com.shengsiyuan.model.Person;
public interface PersonDAO
{
public void savePerson(Person person) ;
public List
listAllPersons() ;
public void removePerson(Integer id) ;
public Person getSinglePersonByid(Integer id);
public void updatePerson(Person person) ;
}
PersonDAOImpl.java
package com.shengsiyuan.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.shengsiyuan.dao.PersonDAO;
import com.shengsiyuan.model.Person;
import com.shengsiyuan.util.HibernateUtil;
public class PersonDAOImpl implements PersonDAO
{
public void savePerson(Person person)
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
try
{
session.save(person);
tx.commit();
}
catch (Exception ex)
{
if (null != tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}
}
public List
listAllPersons()
{
// 多查看hibernate帮助文档的session接口
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
List
list = null;
try
{
Query query = session.createQuery("from Person");// HQL语句,select
// *可省略
// 即为默认情况。Person为类名(不是表名),若同名则加上包名
list = (List
) query.list();
tx.commit();
/*
* 上面这行代码相当于: while(rs.next()) { Person p = new Person() ;
* String usrname = rs.getString("username") ; String password =
* rs.getString("password") ; ...... ... p.setUsername(username) ;
* p.setPassword(password) ; .... .. list.add(p) ;
* 通过hibernate.hbm.xml实现一一对应 }
*/
}
catch (Exception ex)
{
if (null != tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}
return list;
}
public void removePerson(Integer id)
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
try
{
Person person = (Person) session.get(Person.class, id);
session.delete(person);
tx.commit();
}
catch (Exception ex)
{
if (null != tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}
}
public Person getSinglePersonByid(Integer id)
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
Person person = null;
try
{
person = (Person) session.get(Person.class, id);
tx.commit();
}
catch (Exception ex)
{
if (null != tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}
return person;
}
public void updatePerson(Person person)
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
try
{
session.update(person) ;
tx.commit();
}
catch (Exception ex)
{
if (null != tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}
}
}
Person.java
package com.shengsiyuan.model;
import java.sql.Date;
public class Person
{
private Integer id ;//int也可以
private String username ;
private String password ;
private Integer age ;
private Date registerDate ;
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;
}
public Date getRegisterDate()
{
return registerDate;
}
public void setRegisterDate(Date registerDate)
{
this.registerDate = registerDate;
}
}
PersonService.java
package com.shengsiyuan.service;
import java.util.List;
import com.shengsiyuan.model.Person;
public interface PersonService
{
public void savePerson(Person person);
public List
listAllPersons() ;
public void removePerson(Integer id) ;
public Person getSinglePersonByid(Integer id);
public void updatePerson(Person person) ;
}
PersonServiceImpl.java
package com.shengsiyuan.service.impl;
import java.util.List;
import com.shengsiyuan.dao.PersonDAO;
import com.shengsiyuan.dao.impl.PersonDAOImpl;
import com.shengsiyuan.model.Person;
import com.shengsiyuan.service.PersonService;
public class PersonServiceImpl implements PersonService
{
public void savePerson(Person person)
{
PersonDAO personDAO = new PersonDAOImpl() ;
personDAO.savePerson(person) ;
}
public List
listAllPersons()
{
PersonDAO personDAO = new PersonDAOImpl() ;
return personDAO.listAllPersons() ;
}
public void removePerson(Integer id)
{
PersonDAO personDAO = new PersonDAOImpl() ;
personDAO.removePerson(id) ;
}
public Person getSinglePersonByid(Integer id)
{
PersonDAO personDAO = new PersonDAOImpl() ;
return personDAO.getSinglePersonByid(id) ;
}
public void updatePerson(Person person)
{
PersonDAO personDAO = new PersonDAOImpl() ;
personDAO.updatePerson(person) ;
}
}
HibernateUtil.java
package com.shengsiyuan.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static SessionFactory sessionFactory ;
static
{
try
{
sessionFactory =new Configuration().configure().buildSessionFactory() ;
}
catch(Exception ex)
{
ex.printStackTrace() ;
}
}
public static Session openSession()
{
Session session = sessionFactory.openSession();
return session ;
}
public static void close(Session session)
{
if(null != session)
{
session.close();
}
}
}