hibernate---HQL之引用查询小例子

     今天在读<>一书时,突然发现一种和MyBatis框架持久层查询类似的查询方式,半自动的查询方式,需要自己写SQL语句,然后参数绑定进行查询.不过这种方式在实际开发中很少使用,但它的轻巧灵活,还是值得学学的.不得不说Hibernate框架在数据持久层中的重量级地位.现在很多轻量级的持久层框架都是或多或少地参考沿用了它的封装方式.


                    hibernate---HQL之引用查询小例子_第1张图片



书中原描述是这样的:

     

      hibernate---HQL之引用查询小例子_第2张图片

     hibernate---HQL之引用查询小例子_第3张图片

&照猫画狗实践下:


#数据访问层:

package com.sunline.entity;



/**
 * User entity. @author MyEclipse Persistence Tools
 */

public class User  implements java.io.Serializable {


    // Fields    

     private Integer id;
     private String userName;
     private String userPassword;
     private String userInfo;


    // Constructors

    /** default constructor */
    public User() {
    }

    
    /** full constructor */
    public User(String userName, String userPassword, String userInfo) {
        this.userName = userName;
        this.userPassword = userPassword;
        this.userInfo = userInfo;
    }

   
    // Property accessors

    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return this.userName;
    }
    
    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return this.userPassword;
    }
    
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getUserInfo() {
        return this.userInfo;
    }
    
    public void setUserInfo(String userInfo) {
        this.userInfo = userInfo;
    }
}





    
        
            
            
        
        
            
        
        
            
        
        
            
        
    
    
    
    
        from User where userName=:userName and userPassword=:userPassword   
    
    
    
        from User  
    


#数据访问层

package com.sunline.dao;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.sunline.entity.User;
import com.sunline.sessionFactory.HibernateSessionFactory;

@Repository(value="userDao")
public class UserDao extends HibernateDaoSupport {
    private Session session;
    HibernateSessionFactory getSession;
	/*
	 * 使用注解必须添加以下方式
	 */
    @Resource  
    public void setSessionFacotry(SessionFactory sessionFacotry) {  
        super.setSessionFactory(sessionFacotry);  
    }  
    
    /*
     * 根据用户名和密码查询用户
     */
	public List findByInfo(){
    	getSession = new HibernateSessionFactory();
    	session = getSession.getSession();
    	Query query = session.getNamedQuery("findByInfo");    //引用在xml定义好的查询语句
    	query.setString("userName", "haige");
    	query.setString("userPassword", "123456");
        List list = query.list();
		return list;
    }
	
	/*
	 * 查询所有用户
	 */
	public List findAll(){
    	getSession = new HibernateSessionFactory();
    	session = getSession.getSession();
    	Query query = session.getNamedQuery("findAll");       //引用在xml定义好的查询语句
        List list = query.list();
		return list;
    }
}

#业务逻辑层

package com.sunline.biz;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.sunline.dao.UserDao;
import com.sunline.entity.User;

@Service(value="userBiz")
public class UserBiz {
	@Autowired
	@Qualifier("userDao")            //使用@Qualifier注解来说明使用哪一个实现类
    UserDao userDao;
	
    /*
     * 根据用户名和密码查询用户
     */
	public List findByInfo(){
		return userDao.findByInfo();
	}
	
	/*
	 * 查询所有用户
	 */
	public List findAll(){
		return userDao.findAll();
	}
}

#测试类

package com.sunline.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sunline.biz.UserBiz;
import com.sunline.entity.User;

public class UserTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	    UserBiz userBiz = (UserBiz) ctx.getBean("userBiz");
        
	    /*
	     * 根据用户名和密码查询用户
	     */
	    List list = userBiz.findByInfo();
	    for(User user : list){
	    	System.out.println("你的个人信息为: "+user.getUserName()+" "+user.getUserPassword()+" "+user.getUserInfo());
	    }
	    
	    /*
	     * 查询所有用户
	     */
	    List list = userBiz.findAll();
	    for(User user : list){
	    	System.out.println("你的个人信息为: "+user.getUserName()+" "+user.getUserPassword()+" "+user.getUserInfo());
	    }
	}

}


你可能感兴趣的:(Hibernate,框架学习笔记)