简略的Hibernate实例

简单的Hibernate实例

1.编写POJO/javabean/domain类

 

public class Employee implements Serializable{

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String email;
	private java.util.Date hiredate;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public java.util.Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(java.util.Date hiredate) {
		this.hiredate = hiredate;
	}
}

 2.编写mapping文件

<?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 package="POJO">
	<!-- name : 表示类名 table 表示该类和哪个表映射 -->
	<class name="Employee" table="employee">
		<!-- id元素专门用于指定主键是如何生成,hibernate设计者认为,我们每一个表都应该有一个主键 -->
		<!-- name:表示类的哪个属性是主键 -->
		<id name="id" type="java.lang.Integer">
			<!-- 指定主键生成策略 -->
			<generator class="native">
				<param name="native">emp_seq</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" not-null="true" />
		</property>
		<property name="email" type="java.lang.String">
			<column name="email" not-null="true" />
		</property>
		<property name="hiredate" type="java.util.Date">
			<column name="hiredate" not-null="true" />
		</property>
	</class>
</hibernate-mapping>
 

 3.配置Hibernate.cfg.xml

设置自动创建数据库表,选择对应的数据库方言

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate- configuration是连接配置文件的根元素 -->
<hibernate-configuration>
	<session-factory>
		<!-- 指定连接数据库所用的驱动 -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 指定连接数据库的url,hibernate连接的数据库名 -->
		<property name="connection.url">jdbc:mysql://localhost:3306/jk</property>
		<!-- 指定连接数据库的用户名 -->
		<property name="connection.username">root</property>
		<!-- 指定连接数据库的密码 -->
		<property name="connection.password">root</property>
		<!-- 指定连接池里最大连接数 -->
		<property name="hibernate.c3p0.max_size">20</property>
		<!-- 指定连接池里最小连接数 -->
		<property name="hibernate.c3p0.min_size">1</property>
		<!-- 指定连接池里连接的超时时长 -->
		<property name="hibernate.c3p0.timeout">5000</property>
		<!-- 指定连接池里最大缓存多少个Statement对象 -->
		<property name="hibernate.c3p0.max_statements">100</property>
		<property name="hibernate.c3p0.idle_test_period">3000</property>
		<property name="hibernate.c3p0.acquire_increment">2</property>
		<property name="hibernate.c3p0.validate">true</property>
		<!-- 指定数据库方言 -->
		<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
		<!-- 根据需要自动创建数据表 -->
		<property name="hbm2ddl.auto">update</property>
		<!-- 显示Hibernate持久化操作所生成的SQL -->
		<property name="show_sql">true</property>
		<!-- 将SQL脚本进行格式化后再输出 -->
		<property name="hibernate.format_sql">true</property>
		<!-- 罗列所有的映射文件 -->
		<mapping resource="POJO/Employee.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
 

 4.编写测试类来操作Hibernate,简单的增删改操作!

修改和删除操作需要获得对象对应的ID

 

import org.hibernate.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import POJO.Employee;


public class Test {

	
	public static void main(String[] args){
		
//		add();
//		delete();
//		update();
//		select("jk");
		search("k");
	}
	private static void update() {
		Configuration con =  new Configuration().configure();
		SessionFactory sf = con.buildSessionFactory();
		Session session = sf.openSession();
		Transaction trans = session.beginTransaction();
		Employee emp = new Employee();
		emp = (Employee) session.load(emp.getClass(), 1);
		emp.setEmail("jk@12@12.com");
		session.update(emp);
		trans.commit();
		session.close();
		sf.close();
	}
	public static void add(){
		Configuration con =  new Configuration().configure();
		SessionFactory sf = con.buildSessionFactory();
		Session session = sf.openSession();
		Transaction trans = session.beginTransaction();
		Employee emp = new Employee();
		emp.setName("jk");
		emp.setEmail("123@123.123");
		emp.setHiredate(new java.util.Date());
		session.save(emp);
		trans.commit();
		session.close();
		sf.close();
	}
	public static void delete(){
		Configuration con =  new Configuration().configure();
		SessionFactory sf = con.buildSessionFactory();
		Session session = sf.openSession();
		Transaction trans = session.beginTransaction();
		Employee emp = new Employee();
		emp = (Employee) session.load(emp.getClass(), 3);
		session.delete(emp);
		trans.commit();
		session.close();
		sf.close();
	}
	public static void select(String name)
	{
		Configuration config = new Configuration().configure();
		SessionFactory sf = config.buildSessionFactory();
		Session session = sf.openSession();
		Transaction trans = session.beginTransaction();
		Employee emp = null;
		String hql = "from Employee where name=:name";
		Query query = session.createQuery(hql);
		query.setParameter("name", name);
		List list = query.list();
		for(int i=0; i<list.size(); i++){
			Employee e = (Employee) list.get(i);
			System.out.println(e.getId());
		}
	}
	public static void search(String name){

		Configuration config = new Configuration().configure();
		SessionFactory sf = config.buildSessionFactory();
		Session session = sf.openSession();
		Transaction trans = session.beginTransaction();
		Employee emp = null;
		List list = session.createCriteria(Employee.class)
					.add(Restrictions.like("name", "%"+name))
					.addOrder(Order.asc("id")).list();
		for(int i=0; i<list.size(); i++){
			emp = (Employee) list.get(i);
			System.out.println(emp.getName());
		}
	}
}