HQL(Hibernate Query Language)

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) {
			
		}
	}
	//获得session
	public static Session getSession () {
		Session session =  sessionFactory.openSession ();
		return session;
	}
	//关闭session
	public 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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值