hibernate中的分组查询,建立sql查询,删除与更新,条件查询

package ff;

public class T_user {

private int id;

private String name;

private int age;

private String sex;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}
}





package ff;

import org.hibernate.*;
import java.util.*;
public class T_userTest {


public static void main(String[] args) {

Session session = HibernateSessionFactory.getSession();
Query query=session.createQuery("select sex,avg(age) from T_user  group by sex");
List list=query.list();
Iterator it=list.iterator();
while(it.hasNext()){
Object []user=(Object [])it.next();
System.out.println(user[0].equals("0")?"男员工的平均工资:"+user[1]:"女员工的平均工资:"+user[1]);
}
}

}




<?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="ff.T_user" table="T_USER">
      <id name="id" column="id">
         <generator class="increment"/>
      </id>
     
      <property name="name" column="name"/>
      <property name="sex" column="sex"/>
      <property name="age" column="age"/>
    </class>

</hibernate-mapping>




<?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.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@192.168.2.9:1521:ora9
</property>
<property name="connection.username">lxf</property>
<property name="connection.password">lxf</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property> 

<property name="hbm2ddl.auto">update</property>
<mapping resource="ff/User.hbm.xml" />

</session-factory>

</hibernate-configuration>












package ff;

import org.hibernate.*;
import java.util.*;
public class T_userTest {


public static void main(String[] args) {

Session session = HibernateSessionFactory.getSession();
Query query=session.createQuery("select count(name) from T_user ");
List list=query.list();
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println("记录数为:"+it.next());
}
}

}



package ff;
import org.hibernate.*;
import java.util.*;
public class T_userTest {
public static void main(String[] args) {
 
Session session = HibernateSessionFactory.getSession();

SQLQuery sqlquery=session.createSQLQuery("select * from T_user where age>20");
sqlquery.addEntity("T_user",T_user.class);
Iterator it=sqlquery.list().iterator();
while(it.hasNext()){
T_user u=(T_user)it.next();
System.out.println("名字:"+u.getName()+"\t"+"年龄"+"\t"+u.getAge());
}
session.close();
}

}





package ff;

import org.hibernate.*;
import java.util.*;
public class T_userTest {


public static void main(String[] args) {
  /*    //更新操作
Session session = HibernateSessionFactory.getSession();
Query query=session.createQuery("update T_user set name='momor'where id=1 ");
query.executeUpdate();
Transaction tx=session.beginTransaction();
tx.commit();
session.close();*/
//删除操作
Session session = HibernateSessionFactory.getSession();
Query query=session.createQuery("delete T_user where id=1 ");
query.executeUpdate();
Transaction tx=session.beginTransaction();
tx.commit();
session.close();
}

}




<?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="ff.T_user" table="T_USER">
      <id name="id" column="id">
         <generator class="increment"/>
      </id>
     
      <property name="name" column="name"/>
      <property name="sex" column="sex"/>
      <property name="age" column="age"/>
    </class>

</hibernate-mapping>




<?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.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@192.168.2.9:1521:ora9
</property>
<property name="connection.username">lxf</property>
<property name="connection.password">lxf</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property> 

<property name="hbm2ddl.auto">update</property>
<mapping resource="ff/User.hbm.xml" />

</session-factory>

</hibernate-configuration>







package ff;

public class T_user {

private int id;

private String name;

private int age;



public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}







<?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="ff.T_user" table="T_USER">
      <id name="id" column="id">
         <generator class="increment"/>
      </id>
     
      <property name="name" column="name"/>
      <property name="age" column="age"/>
    </class>

</hibernate-mapping>





package ff;

import org.hibernate.*;
import java.util.*;
public class T_userTest {


public static void main(String[] args) {
// TODO Auto-generated method stub
/*Session session = HibernateSessionFactory.getSession();
List list=session.createQuery("from T_user user where user.age>28").list();
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println("客户名:"+((T_user)it.next()).getName());
}
session.close();*/

/* Session session = HibernateSessionFactory.getSession();
List list=session.createQuery("from T_user user where user.age>35 and user.id>2").list();
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println("客户名:"+((T_user)it.next()).getName());
}
session.close();*/

/* Session session = HibernateSessionFactory.getSession();
List list=session.createQuery("from T_user user where (user.name is not null)").list();
Iterator it=list.iterator();
while(it.hasNext()){
T_user u=(T_user)it.next();
System.out.println("客户名:"+u.getName()+"年龄"+u.getAge());
}
session.close();*/

/*Session session = HibernateSessionFactory.getSession();
List list=session.createQuery("from T_user user where (user.name like 'a%')").list();
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println("客户名:"+((T_user)it.next()).getName());
}
session.close();*/


/* Session session = HibernateSessionFactory.getSession();
Query query=session.createQuery("from T_user user order by user.age");
List list=query.list();
Iterator it=list.iterator();
while(it.hasNext()){
T_user u=(T_user)it.next();
System.out.println("客户名:"+u.getName()+"年龄"+u.getAge());
}*/
Session session = HibernateSessionFactory.getSession();
Query query=session.createQuery("from T_user user order by user.age");
List list=query.list();
Iterator it=list.iterator();
while(it.hasNext()){
T_user u=(T_user)it.next();
System.out.println("客户名:"+u.getName()+"年龄"+u.getAge());
}
}

}

你可能感兴趣的:(oracle,sql,Hibernate,MyEclipse,jdbc)