码迷,mamicode.com
首页 > Web开发 > 详细

hibernate HQL

时间:2015-05-20 23:59:01      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

package com.h3c.zgc.user.po;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
    @Id
    @Column(name="id")
    private int id;
    @Column(name="student_name")
    private String studentName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
}
package com.h3c.zgc.base;

import java.util.Map;

public interface BaseDao<T> {

    long findCount(String hql,Map<String, Object> params);
}
package com.h3c.zgc.base;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
@Repository
public class BaseDaoHibernate4<T> implements BaseDao<T>{
    @Resource
    private SessionFactory sessionFactory;
    
    public Session getSession(){
        return this.sessionFactory.getCurrentSession();
    }
    @Override
    public long findCount(String hql, Map<String, Object> params) {
        Query query = this.getSession()
                      .createQuery(hql);
        Set<Entry<String, Object>> ens = params.entrySet();              
        for(Entry<String,Object> ent:ens){
            query.setParameter(ent.getKey(), ent.getValue());
        }
        return (long) query.list().get(0);
    }

}
package com.h3c.zgc.user.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.h3c.zgc.user.dao.StudentDao;
import com.h3c.zgc.user.po.Student;
@Service
public class StudentService {
    @Resource
    private StudentDao studentDao;
    @Transactional
    public long getCount(Student s){
        String hql ="select count(id) from Student where studentName =:name";
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("name", s.getStudentName());
        return this.studentDao.getTotalCount(hql, params);
    }
}
package com.h3c.zgc.user.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.h3c.zgc.user.po.Student;
import com.h3c.zgc.user.service.StudentService;

public class TestSpring {
    @Test
    public void test1(){
        ApplicationContext wac = new ClassPathXmlApplicationContext("spring/mvc/action-servlet.xml");
        StudentService ss = (StudentService) wac.getBean("studentService");
        Student s = new Student();
        s.setStudentName("name");
        System.out.println(ss.getCount(s));
        
        
    }
    
    
}

 

hibernate HQL

标签:

原文地址:http://www.cnblogs.com/qq931399960/p/4518335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!