想飛就別怕摔

          大爺的并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 生命的綻放 閱讀(708) 評論(0)  編輯  收藏 所屬分類: Hibernate

          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統計

          常用鏈接

          留言簿(5)

          隨筆分類(94)

          隨筆檔案(93)

          文章分類(5)

          文章檔案(5)

          相冊

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 平乡县| 绥芬河市| 和平县| 扬中市| 滁州市| 灵石县| 广州市| 海城市| 阜宁县| 宿松县| 荥经县| 体育| 满城县| 温州市| 宁海县| 怀远县| 静乐县| 武强县| 腾冲县| 汝阳县| 廊坊市| 商水县| 辽阳县| 长岛县| 新竹市| 湖南省| 佛学| 莱西市| 台山市| 承德市| 舟山市| 甘洛县| 延长县| 乌什县| 柳林县| 巨鹿县| 霍城县| 吉木萨尔县| 雅安市| 新郑市| 周宁县|