Ⅰ hibernate中,用HQL查詢如何獲取資料庫中年齡欄位最大的那條記錄呢HQL語句怎麼寫持久化類為User
子查詢吧,下面是我寫的
hql語句
"from User where age=(select max(age) from User) "
相應的sql語句是
SELECT * FROM USER WHERE AGE=(SELECT MAX(AGE) FROM USER);
樓上回答的不完整,回那樣只會返回答最大的年齡
Ⅱ hibernate中,用HQL查詢如何獲取資料庫中年齡欄位最大的那條記錄HQL語句怎麼寫
可以用子查詢:
hql語句
"from User where age=(select max(age) from User) "
相應的sql語句是
SELECT * FROM USER WHERE AGE=(SELECT MAX(AGE) FROM USER);
Ⅲ hibernate怎麼查詢資料庫
資料庫查詢排序 常用
Team.java
[java] view plain
package com.fgh.hibernate;
import java.util.HashMap;
import java.util.Map;
public class Team {
private String id;
private String name;
private Map students = new HashMap();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map getStudents() {
return students;
}
public void setStudents(Map students) {
this.students = students;
}
}
hibernate.cfg.xml
[html] view plain
<?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="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<mapping resource="Team.hbm.xml" />
</session-factory>
</hibernate-configuration>
Team.hbm.xml
[html] view plain
<?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="com.fgh.hibernate.Team" table="team_map">
<id name="id" column="id" type="string">
<generator class="uuid"></generator>
</id>
<property name="name" column="name" type="string"></property>
<strong><span style="font-size:18px;color:#ff6666;"><!-- order-by 指按資料庫排序 屬性值是資料庫中的欄位名 不是屬性名 默認是按升序排序--></span></strong>
<map name="students" table="student_map"<strong><span style="color:#ff0000;"> <span style="font-size:18px;">order-by="name desc"</span></span></strong>>
<key column="team_id"></key>
<index column="name" type="java.lang.String"></index><!-- 指定的是map中的key -->
<element column="value" type="java.lang.String"></element><!-- 指定的是map中的value -->
</map>
</class>
</hibernate-mapping>
建表類:CreateTable.java
[java] view plain
package com.fgh.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class CreateTable {
public static void main(String[] args) {
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true, true);
}
}
測試類:
InsertTest.java
[java] view plain
package com.fgh.hibernate;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class InsertTest {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
// 保存操作
// Team team = new Team();
// team.setName("team1");
//
// HashMap map = (HashMap) team.getStudents();
// map.put("aa", "zhangsan");
// map.put("bb", "lisi");
// map.put("ccc", "wangwu");
tx = session.beginTransaction();
// 查詢操作 這里使用uniqueResult()方法返回一個唯一的對象
// 而不是返回list 方便 Team 和 name 都是指類裡面的屬性
Team team = (Team) session.createQuery(
"from Team t where t.name = 'team1'").uniqueResult();
Map map = team.getStudents();
Collection collection = map.values();
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (null != tx) {
tx.rollback();
}
} finally {
session.close();
}
}
}
後台列印sql:
[sql] view plain
Hibernate: select team0_.id as id0_, team0_.name as name0_ from team_map team0_ where team0_.name='team1'
Hibernate: select students0_.team_id as team1_0_, students0_.value as value0_, students0_.name as name0_ from student_map students0_ where students0_.team_id=? order by students0_.name desc
wangwu
lisi
zhangsan
Ⅳ Hibernate如何動態鏈接資料庫
一.導包 mysql
二.在默認src下創建hibernate.cfg.xml
1.創建xml文件,命名為hibernate.cfg.xml
2.添加約束
(在org.hibernate/hibernate-configuration-3.0.dtd中)
1 <!DOCTYPE hibernate-configuration PUBLIC2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url">jdbc:mysql://localhost:3306/houserentsys</property> <!-- houserentsys是資料庫名稱 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">123456</property>
<property name="show_sql">true</property> <property name="format_sql">false</property> <!-- 設置為false就會不換行 --> <property name="hbm2ddl.auto">update</property> <!-- 進行操作時不會刪除重建-->
<!--hbm2ddl.auto屬性:
create:表示啟動的時候先drop,再create
c
reate-drop: 也表示創建,只不過再系統關閉前執行一下drop
update: 這個操作啟動的時候會去檢查schema是否一致,如果不一致會做scheme更新
validate: 啟動時驗證現有schema與你配置的hibernate是否一致,如果不一致就拋出異常,並不做更新
-->
<mapping resource="e/tsinghua/entity/mapping/district.xml"/> <mapping resource="e/tsinghua/entity/mapping/street.xml"/>
</session-factory></hibernate-configuration>
hbm2ddl.auto屬性:
create:表示啟動的時候先drop,再create
create-drop: 也表示創建,只不過再系統關閉前執行一下drop
update: 這個操作啟動的時候會去檢查schema是否一致,如果不一致會做scheme更新
validate: 啟動時驗證現有schema與你配置的hibernate是否一致,如果不一致就拋出異常,並不做更新
三.實體 實現序列化介面 封裝屬性和構造方法 實體.xml 位置隨意
(在org.hibernate/hibernate-mapping-3.0.dtd中)
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
在hibernate.cfg.xml 添加 映射文件的引用
<mapping resource="e.tsinghua.entity.mapping.district"/>
七個步驟(在新建的執行文件Test.java中)
//1.載入配置文件
Configuration cfg=new Configuration().configure();
//2.獲得sessionfactory
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf=cfg.buildSessionFactory(serviceRegistry);
//3.創建session
Session session=sf.openSession();
//4.創建事務
Transaction tx=session.beginTransaction();
//5.操作
District dis=new District(100,"海淀區");
session.save(dis);
//6.提交 回滾
tx.commit();//tx.rollback();
//7.釋放資源