hibernate criteria的例子

我转载的文章,全都补齐含库源代码:http://pan.baidu.com/share/link?shareid=404568&uk=3878681452

Criteria类对sql进行了封装,可以用来进行各种查询,它以面向对象的方式来完成sql的部分功能.

创建数据库:

DROP TABLE IF EXISTS `EMPLOYEE`;

CREATE TABLE `EMPLOYEE` (
  `EMP_ID` int(11) NOT NULL AUTO_INCREMENT,
  `EMP_NAME` text ,
  `EMP_ADDR` text,
  
  `EMP_PHONE` varchar(50),
  `EMP_EMAIL` varchar(100),
  `EMP_DOB` datetime,
  `EXP_IN_MONTH` int(11),
  `EMP_SALARY` double,
  PRIMARY KEY (`EMP_ID`)
) 

插入数据

package roseindia;

import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import roseindia.bean.User;
import roseindia.util.HibernateUtil;



public class DataInsert {

	
	public static void main(String[] args) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		User user=null;
		
		try {
			transaction = session.beginTransaction();
			
			for(int i=0; i<10; i++)
			{
			user=new User("Gyan "+i,"NEW DELHI"+i,"68784654654"+i,"gyan@gmail.com"+i,new Date(),12*i,15398.00*i);
			session.save(user);
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	

	}

}

映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="roseindia.bean.User" table="EMPLOYEE">
        <id name="empId" type="long" column="EMP_ID">
            <generator class="native" />
        </id>
        <property name="empName" type="java.lang.String" length="100" not-null="false" column="EMP_NAME" />
        <property name="empAddr" type="java.lang.String" length="100" not-null="false" column="EMP_ADDR" />
        <property name="empPhone" type="java.lang.String" length="100" not-null="false" column="EMP_PHONE" />
        <property name="empEmail" type="java.lang.String" length="100" not-null="false" column="EMP_EMAIL" />
        <property name="empDOB" type="java.util.Date" not-null="false" column="EMP_DOB" />
        <property name="expInMonth" type="int" not-null="false" column="EXP_IN_MONTH" />
        <property name="empSalary" type="double" not-null="false" column="EMP_SALARY" />
          
       </class>
</hibernate-mapping>

原文:

http://www.roseindia.net/tutorial/hibernate/hibernate-criteria-RestrictionExample.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值