[hibernate] hibernate 基础操作
1.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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.OracleDialect </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <property name="connection.username">dataprocess</property> <property name="connection.password">dataprocess</property> <property name="connection.driver_class"> oracle.jdbc.OracleDriver </property> <property name="myeclipse.connection.profile"> hibernate </property> <property name="format_sql">true</property> <property name="show_sql">true</property> <mapping resource="com/wa/hibernate/ch1/model/News.hbm.xml" /> <mapping resource="com/wa/hibernate/ch1/model/SysUsers.hbm.xml" /> </session-factory> </hibernate-configuration>
2. SysUsers.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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.wa.hibernate.ch1.model.SysUsers" table="SYS_USERS" schema="DATAPROCESS"> <comment>用户信息</comment> <id name="userId" type="java.lang.Long"> <column name="USER_ID" precision="15" scale="0" /> <generator class="sequence" > <param name="SEQ_SYS_USERS"></param> </generator> </id> <property name="uname" type="java.lang.String"> <column name="UNAME" length="100" not-null="true"> <comment>用户名</comment> </column> </property> <property name="pwd" type="java.lang.String"> <column name="PWD" length="60" not-null="true"> <comment>密码</comment> </column> </property> <property name="cardNo" type="java.lang.String"> <column name="CARD_NO" length="20"> <comment>身份证号</comment> </column> </property> <property name="birth" type="java.util.Date"> <column name="BIRTH" length="7"> <comment>出生日期</comment> </column> </property> <property name="salary" type="java.lang.Double"> <column name="SALARY" precision="15"> <comment>薪水</comment> </column> </property> <property name="UStatus" type="java.lang.String"> <column name="U_STATUS" length="2" not-null="true"> <comment>状态</comment> </column> </property> </class> </hibernate-mapping>
测试方法 :
package com.wa.hibernate.ch1.test;
import java.net.URL;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.wa.hibernate.ch1.model.SysUsers;
public class TestUser {
private static Session session ;
@Before
public void before(){
Configuration configuration = new Configuration();
URL url =TestUser.class.getResource("/hibernate.cfg.xml");
configuration.configure(url);
SessionFactory sessionFactory=configuration.buildSessionFactory();
session=sessionFactory.openSession();
System.out.println("session 开启.....");
}
@Test
public void test1(){
SysUsers users = new SysUsers();
users.setBirth(new Date());
users.setCardNo("4119012343242131");
users.setSalary(2321.2132);
users.setUname("哎呀,我去测试");
users.setPwd("234rtgfd");
users.setUStatus("0");
//保存
session.save(users);
//提交事务
Transaction transaction = session.beginTransaction();
transaction.commit();
}
@Test
public void test2(){
//延迟加载
SysUsers sysUsers =(SysUsers) session.load(SysUsers.class,Long.valueOf(1));
System.out.println("-------------------");
System.out.println(sysUsers);
//立即查询
SysUsers users = (SysUsers) session.get(SysUsers.class, Long.valueOf(2));
System.out.println("&&&&&&&&&&&&&");
System.out.println(users);
}
/**
* 查询
*/
@Test
public void test3(){
Criteria criteria =session.createCriteria(SysUsers.class);
//等于
//criteria.add(Restrictions.eq("UStatus", "0"));
//模糊查询
//criteria.add(Restrictions.like("uname","哎呀",MatchMode.ANYWHERE));
//between and
//criteria.add(Restrictions.between("userId", Long.valueOf(21),Long.valueOf(23)));
/** in 和 order 排序 lt(<) gt(>)**/
//criteria.add(Restrictions.in("userId",new Long[]{1l,2l}))
//.addOrder(Order.asc("birth")).add(Restrictions.gt("salary", 500d));
/**
* 分页查询
*/
criteria.setProjection(Projections.rowCount());
//总页数
int total =Integer.valueOf(criteria.uniqueResult().toString()) ;
//重置为null
/**
* 不重置NULL的SQL
* select
*
from
( select
count(*) as y0_
from
DATAPROCESS.SYS_USERS this_ )
where
rownum <= ?
重置null后的SQL:
select
*
from
( select
this_.USER_ID as USER1_1_0_,
this_.UNAME as UNAME1_0_,
this_.PWD as PWD1_0_,
this_.CARD_NO as CARD4_1_0_,
this_.BIRTH as BIRTH1_0_,
this_.SALARY as SALARY1_0_,
this_.U_STATUS as U7_1_0_
from
DATAPROCESS.SYS_USERS this_ )
where
rownum <= ?
添加分页后的sql:
select
*
from
( select
row_.*,
rownum rownum_
from
( select
this_.USER_ID as USER1_1_0_,
this_.UNAME as UNAME1_0_,
this_.PWD as PWD1_0_,
this_.CARD_NO as CARD4_1_0_,
this_.BIRTH as BIRTH1_0_,
this_.SALARY as SALARY1_0_,
this_.U_STATUS as U7_1_0_
from
DATAPROCESS.SYS_USERS this_ ) row_ )
where
rownum_ <= ?
and rownum_ > ?
*/
criteria.setProjection(null);
criteria.setFirstResult(2*(2-1));
criteria.setMaxResults(2);
List<SysUsers> list = criteria.list();
for (SysUsers users:list){
System.out.println(users);
}
}
@After
public void after(){
if(session!=null){
session.close();
System.out.println("session 关闭");
}
}
}
2.分页语句:
oracle分页:
select
*
from
( select
row_.*,
rownum rownum_
from
( select
this_.USER_ID as USER1_1_0_,
this_.UNAME as UNAME1_0_,
this_.PWD as PWD1_0_,
this_.CARD_NO as CARD4_1_0_,
this_.BIRTH as BIRTH1_0_,
this_.SALARY as SALARY1_0_,
this_.U_STATUS as U7_1_0_
from
DATAPROCESS.SYS_USERS this_ ) row_ )
where
rownum_ <= ?
and rownum_ > ?
mysql 分页:
select
this_.USER_ID as USER1_1_0_,
this_.UNAME as UNAME1_0_,
this_.PWD as PWD1_0_,
this_.CARD_NO as CARD4_1_0_,
this_.BIRTH as BIRTH1_0_,
this_.SALARY as SALARY1_0_,
this_.U_STATUS as U7_1_0_
from
DATAPROCESS.SYS_USERS this_ ) row_
limit 1,10;

浙公网安备 33010602011771号