導航:首頁 > 數據分析 > hibernate如何獲取資料庫

hibernate如何獲取資料庫

發布時間:2023-03-07 17:12:37

Ⅰ 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.釋放資源

閱讀全文

與hibernate如何獲取資料庫相關的資料

熱點內容
保證能貸款的app 瀏覽:105
adb文件夾大小 瀏覽:78
資料庫browser 瀏覽:458
愛麗絲夢遊仙境x級版本 瀏覽:636
windows登陸linux 瀏覽:851
如何用excel摳文件印章 瀏覽:24
蘋果4s的div設置 瀏覽:638
網路管理員考試教材 瀏覽:201
c配置文件在c盤哪裡 瀏覽:694
可配置文件翻譯 瀏覽:879
如何把紙文件掃描成pdf 瀏覽:514
plc博途編程中除法取整怎麼取 瀏覽:717
950客服代碼 瀏覽:463
ghost支持linux嗎 瀏覽:927
文件生命周期理論有哪些內容 瀏覽:278
百度雲盤批量轉存工具 瀏覽:949
qq上說購買q幣真的假的 瀏覽:589
linuxrmrf找回 瀏覽:887
打開u盤隱藏文件夾 瀏覽:43
win7怎麼安裝西門子plc編程軟體 瀏覽:314

友情鏈接