HQL(Hibernate Query Language)
来源:互联网 发布:三维动画的软件 编辑:程序博客网 时间:2024/05/18 03:46
1:HQL(Hibernate Query Language):面向的是对象而不是数据库中的表,
这是与SQL(Structured Query Language)之间的差别
2:Session的get与load方法都可以获取相应的持久化对象,如果该对象存在,那么这二个方法的
行为是一样的;如果改对象不存在,那么get方法会返回null而load方法会抛出异常。
2:用hibernate实现一张表的增删改查
hibernate整合struts2的员工增删改查
dao包:
package dao;import java.util.List;import model.Person;public interface PersonDao {public void savePerson (Person person);public List<Person> listAllPersons();public void removePerson (String id);public Person getSinglePersonById(String id);public void updatePerson (Person person);}dao.impl包:
package dao.impl;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import util.HibernateUtil;import model.Person;import dao.PersonDao;public class PersonDaoImpl implements PersonDao {public void savePerson(Person person) {Session session = HibernateUtil.getSession();/** * */Transaction tx = session.beginTransaction();try {session.save(person);tx.commit();}catch(Exception ex){ex.printStackTrace();if (null != tx) {tx.rollback();}}finally{HibernateUtil.close(session);}}@SuppressWarnings("unchecked")public List<Person> listAllPersons() {List<Person> persons = null;Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();try {//Person是类的名字而不是表的名字,既然是类的名字就需要严格区分大小写Query query = session.createQuery("from Person");//类的名字persons = (List<Person>)query.list();tx.commit();}catch (Exception ex) {if (tx != null) {tx.rollback();}}finally {HibernateUtil.close(session);}return persons;}public void removePerson(String id) {Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();try {Person person = (Person)session.get(Person.class, id);session.delete(person);tx.commit();}catch(Exception ex) {ex.printStackTrace();if (null != tx) {tx.rollback();}}finally {HibernateUtil.close(session);}}public Person getSinglePersonById(String id) {Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();Person person = null;try {person = (Person)session.get(Person.class, id);}catch(Exception ex) {ex.printStackTrace();if (null != tx) {tx.rollback();}}finally{HibernateUtil.close(session);}return person;}public void updatePerson(Person person) {Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();try {session.update(person);tx.commit();}catch(Exception ex) {ex.printStackTrace();if (null != tx) {tx.rollback();}}finally{HibernateUtil.close(session);}}}
util包:
package 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 e) {}}//获得sessionpublic static Session getSession () {Session session = sessionFactory.openSession ();return session;}//关闭sessionpublic static void close (Session session) {if (null != session) {session.close();}}}
service包:
package service;import java.util.List;import model.Person;public interface PersonService {public void savePerson (Person person);public List<Person> listAllPersons ();public void removePerson (String id);public Person getSinglePersonById(String id);public void updatePerson (Person person);}
service.impl包:
package service.imp;import java.util.List;import service.PersonService;import dao.PersonDao;import dao.impl.PersonDaoImpl;import model.Person;public class PersonServieImpl implements PersonService{public void savePerson (Person person) {PersonDao pd = new PersonDaoImpl();pd.savePerson(person);}public List<Person> listAllPersons() {PersonDao pd = new PersonDaoImpl();return pd.listAllPersons();}public void removePerson(String id) {PersonDao pd = new PersonDaoImpl();pd.removePerson(id);}public Person getSinglePersonById(String id) {PersonDao pd = new PersonDaoImpl();return pd.getSinglePersonById(id);}public void updatePerson(Person person) {PersonDao pd = new PersonDaoImpl ();pd.updatePerson(person);}}
action.person包中:
package action.person;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import service.PersonService;import service.imp.PersonServieImpl;import model.Person;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class PersonAction extends ActionSupport{private String id;private String username;private String password;private int age;private java.sql.Date registerDate;public java.sql.Date getRegisterDate() {return registerDate;}public void setRegisterDate(java.sql.Date registerDate) {this.registerDate = registerDate;}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 listAllPerson () throws Exception {PersonService personService = new PersonServieImpl();List<Person> list = personService.listAllPersons();HttpServletRequest request = ServletActionContext.getRequest();request.setAttribute("list", list);return Action.SUCCESS;}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 PersonServieImpl();personService.savePerson(person);return Action.SUCCESS;}public String deletePerson ()throws Exception {PersonService personService = new PersonServieImpl();personService.removePerson(id);return Action.SUCCESS;}public String getSinglePerson () throws Exception{PersonService personService = new PersonServieImpl ();Person person = personService.getSinglePersonById(id);HttpServletRequest request = ServletActionContext.getRequest();request.setAttribute("person", person);return "success";}public String updatePerson () throws Exception {PersonService personService = new PersonServieImpl ();Person person = new Person ();person.setId(id);person.setAge(age);person.setPassword(password);person.setRegisterDate(registerDate);person.setUsername(username);personService.updatePerson(person);return Action.SUCCESS;}public String getId() {return id;}public void setId(String id) {this.id = id;}}hibernate.cfg.xml主配置文件:
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name = "connection.username">root</property> <property name = "connection.password">baother520</property> <property name = "connection.driver_class">com.mysql.jdbc.Driver</property> <property name = "dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name = "show_sql">true</property> <mapping resource="Person.hbm.xml" /> </session-factory></hibernate-configuration>
Person.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"><hibernate-mapping><class name = "model.Person" table = "person"><id name = "id" column = "id" type = "string"><generator class="uuid"></generator></id><property name = "username" column = "username" type = "string"></property><property name="age" column = "age" type = "int"></property><property name = "registerDate" column = "registerDate" type = "date"></property></class></hibernate-mapping>
- HQL(Hibernate Query Language)
- Hibernate Query Language (HQL)
- HQL(Hibernate Query Language)
- Hibernate Query Language(HQL)
- About Hibernate Query Language(HQL)
- HQL(Hibernate Query Language)
- Hibernate查询语言(Query Language), 即HQL
- HQL(Hibernate Query Language)使用详解
- hibernate-HQL(query language)-16
- hibernate参考手册之---Hibernate查询语言(Query Language), 即HQL
- Hibernate学习笔记-HQL(Hibernate Query Language)
- Hibernate学习笔记-HQL(Hibernate Query Language)
- Hibernate检索方式之HQL(Hibernate Query Language)
- Hibernate查询(Query Language,hql,ejbql,nativeql,qbc,qbe)
- 关于HQL(Hibernate Query Language)的查询方法
- 转发:Using HQL (Hibernate Query language) http://www.castleproject.org/activerecord/documentation/v1rc1/usersguide/hql.html
- HQL:Hibernate Qusery Language
- Hibernate Query Language查询
- iTween visual Editor 的使用技巧
- camshift---反向投影图
- 马化腾对话李连杰:移动互联网的能量有多大?
- int取值
- 分布式搜索Elasticsearch——创建客户端(六)
- HQL(Hibernate Query Language)
- Paypal 支付(3)
- ftp两种工作模式
- Java命名规范
- android 改变导入project的名称
- 通过自定义文件启动程序并读取
- Android:自定义ListView
- 三年了
- 实习生面试心得