hibernate怎么查询数据库
1个回答
展开全部
数据库查询排序 常用
Team.java
[java] view plain copy
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 copy
<?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 copy
<?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 copy
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 copy
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 copy
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
Team.java
[java] view plain copy
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 copy
<?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 copy
<?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 copy
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 copy
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 copy
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询