导航:首页 > 数据分析 > 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如何获取数据库相关的资料

热点内容
登录韩国id后如何在app付费 浏览:811
bp神经网络预测matlab代码实现 浏览:194
linux基本分区 浏览:274
如何给电脑重要文件设置路径 浏览:387
中国发布要素市场化配置文件 浏览:984
狸窝全能视频转换器密码 浏览:341
哪里下载原始数据 浏览:2
javaclass关键字 浏览:651
零基础学网页UI设计PDF文件 浏览:303
1月6日买苹果送耳机 浏览:520
access代码生成器 浏览:915
柱状图如何更改表格左侧数据 浏览:223
嘉定区常用网络服务有哪些 浏览:271
两份word文件怎么压缩 浏览:471
股票池压缩文件 浏览:656
word2007如何画正方形 浏览:244
vivo最好用什么数据线 浏览:417
二维腿长是哪个数据 浏览:126
红头文件在哪里订书 浏览:175
iphone4s运行ios9 浏览:688

友情链接