我转载的文章,全都补齐含库源代码: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