Java自学笔记之Hibernate-单表hql查询语句

说明:连接的是mysql数据库 hibernate的hql语句进行单表查询。
1.实体化类

package com.web0816;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="psd")
public class Psd implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private int id;
    private String name;
    private String username;
    private String password;
    private String belong;
    private String description;

    @Id
    @Column(name="id")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Column(name="username")
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    @Column(name="password")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Column(name="belong")
    public String getBelong() {
        return belong;
    }
    public void setBelong(String belong) {
        this.belong = belong;
    }
    @Column(name="description")
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Psd() {
    }
    public Psd(int id, String name, String username, String password,
            String belong, String description) {
        this.id = id;
        this.name = name;
        this.username = username;
        this.password = password;
        this.belong = belong;
        this.description = description;
    }
    @Override
    public String toString() {
        return "Psd [id=" + id + ", name=" + name + ", username=" + username
                + ", password=" + password + ", belong=" + belong
                + ", description=" + description + "]";
    }



}

2.HIbernateUtils.java 获得SessionFactory的工具类

package com.web0816;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtils {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;
    public static SessionFactory getSessionFactory() {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(
                configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

}

3.HibernateTest.java 进行测试hql的类

package com.web0816;

import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;

public class HibernateTest {

    @Test
    public void test1() {
        // 测试SessionFactory 和Session配置是否成功
        Session s = HibernateUtils.getSessionFactory().openSession();
        System.out.println(s);
    }

    @Test
    public void test2() {
        // 直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 from后面加的是类名
        String hql = "from Psd";
        Session session = HibernateUtils.getSessionFactory().openSession();
        Query query = session.createQuery(hql);
        List<Psd> list = query.list();
        for (Psd psd : list) {
            System.out.println(psd);
        }
    }

    @Test
    public void test3() {
        // 查询其中几个字段
        String hql = "select username,password from Psd";
        Session session = HibernateUtils.getSessionFactory().openSession();
        Query query = session.createQuery(hql);
        // 默认查询出来的list里存放的是一个Object数组
        List<Object[]> list = query.list();
        for (Object[] objects : list) {
            System.out.println(objects[0] + "=" + objects[1]);
        }
    }

    @Test
    public void test4() {
        // 查询其中几个字段,添加new
        // list(),注意list里的l是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是List集合了
        String hql = " select new list(username,password) from Psd";
        Session session = HibernateUtils.getSessionFactory().openSession();
        Query query = session.createQuery(hql);
        // 默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是List集合了
        List<List> list = query.list();
        for (List user : list) {
            String name = (String) user.get(0);
            String passwd = (String) user.get(1);

            System.out.println(name + " : " + passwd);
        }
    }

    @Test
    public void test5() {
        // 查询其中几个字段,添加new map(),注意map里的m是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是map集合了
        String hql = " select new map(username,password) from Psd";
        Session session = HibernateUtils.getSessionFactory().openSession();
        Query query = session.createQuery(hql);
        // 默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是Map集合了
        List<Map> list = query.list();
        for (Map user : list) {
            // 一条记录里所有的字段值都是map里的一个元素,key是字符串0,1,2,3....,value是字段值
            // 如果将hql改为:String hql = " select new map(name as username,passwd as password) from Users";,那么key将不是字符串0,1,2...了,而是"username","password"了
            String name = (String) user.get("0");// get("0");是get(key),注意:0,1,2...是字符串,而不是整形
            String passwd = (String) user.get("1");

            System.out.println(name + " : " + passwd);
        }

        hql = "select new map(username as username,password as password) from Psd";
        query = session.createQuery(hql);
        list = query.list();
        for (Map user : list) {
            String name = (String) user.get("username");
            String passwd = (String) user.get("password");

            System.out.println(name + " : " + passwd);
        }
    }
    @Test
    public void test6(){
        //条件查询,参数索引值从0开始,索引位置。通过setString,setParameter设置参数   
        String hql = "from Psd where id=? and belong=?";   
        Session session = HibernateUtils.getSessionFactory().openSession();
        Query query = session.createQuery(hql);   
        //第1种方式   
//      query.setInteger(0, q);   
//      query.setString(1, "a");   
        //第2种方式   
        query.setParameter(0, 1);   
        query.setParameter(1, "l");   
        List<Psd> list = query.list();   
        for(Psd psd : list){   
            System.out.println(psd);   
        }   
    }
    @Test
    public void test7(){
        //条件查询,自定义索引名(参数名):id,:belong.通过setParameter设置参数   
        String hql = "from Psd where id=:id and belong=:belong";   
        Session session = HibernateUtils.getSessionFactory().openSession(); 
        Query query = session.createQuery(hql); 
        // 此时在用query.setParameter(0,1) 会报错org.hibernate.QueryParameterException
        // 此时如果索引名写错的话也会报错org.hibernate.QueryParameterException
        query.setParameter("id", 1);
        query.setParameter("belong", "l");   
        List<Psd> list = query.list();   
        for(Psd psd : list){   
            System.out.println(psd);   
        }  
    }

}

学习来源:http://www.cnblogs.com/focusChen/articles/2401892.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值