Hibernate数据库查询

数据库查询排序 常用

Team.java

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

<?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

<?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>
		
		<!-- order-by 指按数据库排序 属性值是数据库中的字段名 不是属性名 默认是按升序排序-->
		<map name="students" table="student_map" order-by="name desc">
			<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

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

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:

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值