想飞就别怕摔

大爷的并TM骂人

Hibernate简单例子(面试题)

试题:在hibernate中根据姓名查询出表中符合条件的员工分别用 HQL 和SQL

package zzn.hibernate.model;

import java.util.Date;

public class Employ {
    
private int id;
    
private Integer empId;  
    
private String empName;  
    
private String empEname;  
    
private String gender;
    
private String empNo;
    
private Date birthday;
    
private String idCard;
    
private String address;
    
    
public Integer getEmpId() {
        
return empId;
    }
    
public void setEmpId(Integer empId) {
        
this.empId = empId;
    }
    
public String getEmpName() {
        
return empName;
    }
    
public void setEmpName(String empName) {
        
this.empName = empName;
    }
    
public String getEmpEname() {
        
return empEname;
    }
    
public void setEmpEname(String empEname) {
        
this.empEname = empEname;
    }
    
public String getGender() {
        
return gender;
    }
    
public void setGender(String gender) {
        
this.gender = gender;
    }
    
public String getEmpNo() {
        
return empNo;
    }
    
public void setEmpNo(String empNo) {
        
this.empNo = empNo;
    }
    
public Date getBirthday() {
        
return birthday;
    }
    
public void setBirthday(Date birthday) {
        
this.birthday = birthday;
    }
    
public String getIdCard() {
        
return idCard;
    }
    
public void setIdCard(String idCard) {
        
this.idCard = idCard;
    }
    
public String getAddress() {
        
return address;
    }
    
public void setAddress(String address) {
        
this.address = address;
    }
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    
    
}

<?xml version="1.0" encoding='gb2312'?>
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>


<hibernate-mapping package="zzn.hibernate.model">
    
<class name="Employ" table="employ">
        
<id name="id" column="id">
            
<generator class="identity" />
        
</id>
        
<property name="empName" column="emp_name" type="string" />
        
<property name="empEname" column="emp_ename" type="string" />
        
<property name="gender" column="gender" type="string" />
        
<property name="empNo" column="emp_phone" type="string" />
        
<property name="idCard" column="id_card" type="string" />
        
<property name="address" column="adress" type="string" />
        
<property name="empId" column="emp_id" type="integer" />
        
<property name="birthday" column="birthday" type="date" />
    
</class>
</hibernate-mapping>


package zzn.hibernate.test;

import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import zzn.hibernate.model.Employ;

public class EmployTest {
    @SuppressWarnings(
"unchecked")
    
public static void main(String[] args) {
        employAdd();
        @SuppressWarnings(
"unused")
        List
<Employ> list = employHibernateQuery("zhaozhaonan");
        
for(Employ emp : list){
            System.out.println(
"EmployId:~~~~" + emp.getEmpId()
                    
+ "   EmployeeName:~~~~" + emp.getEmpName()
                    
+ "   EmployeeEname:~~~~" + emp.getEmpEname()
                    
+ "   EmployeePhone:~~~~" + emp.getEmpNo()
                    
+ "   EmploueeBirthday:~~~~" + emp.getBirthday()
                    
+ "   EmploueeAddress:~~~~" + emp.getAddress()
                    
+ "   EmploueeGender:~~~~" + emp.getGender()
                    );
        }
    }
    
    @SuppressWarnings(
"unchecked")
    
public static List employHibernateQuery(String name){
        Configuration configuration 
= null;
        SessionFactory sessionFactory 
= null;
        Session session 
= null;
        
try {
            configuration 
= new Configuration();
            sessionFactory 
= configuration.configure().buildSessionFactory();
            session 
= sessionFactory.openSession();
            String sql 
= "from Employ where emp_name=? ";
            Query query 
= session.createQuery(sql);
            query.setString(
0, name);
            List employList 
= query.list();
            
return employList;
        }
finally{
            
if(session != null){
                session.close();
                }
        }
    }
    
    
public static void employAdd(){
        Configuration configuration 
= null;
        SessionFactory sessionFactory 
= null;
        Session session 
= null;
        Transaction transaction 
= null;
        Employ emp 
= new Employ();
        
try{
            configuration 
= new Configuration();
            sessionFactory 
= configuration.configure().buildSessionFactory();
            session 
= sessionFactory.openSession();
            transaction 
= session.beginTransaction();
            emp.setEmpId(
123);
            emp.setEmpName(
"zhaozhaonan");
            emp.setEmpEname(
"shanshan");
            emp.setEmpNo(
"13581937677");
            emp.setBirthday(
new Date());
            emp.setAddress(
"huoying");
            emp.setGender(
"3");
            emp.setIdCard(
"152105198506120316");
            session.save(emp);
            
        }
finally{
            
if(session!=null){
                transaction.commit();
                session.close();
            }
        }
    }
}

posted on 2009-06-27 10:17 生命的绽放 阅读(697) 评论(0)  编辑  收藏 所属分类: Hibernate


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜