想飛就別怕摔

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

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

          導航

          統計

          常用鏈接

          留言簿(5)

          隨筆分類(94)

          隨筆檔案(93)

          文章分類(5)

          文章檔案(5)

          相冊

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 洱源县| 延安市| 吴川市| 特克斯县| 邳州市| 兴和县| 东台市| 黄龙县| 固安县| 新化县| 山阴县| 隆安县| 陵川县| 新巴尔虎左旗| 修文县| 宁强县| 保定市| 湖口县| 六盘水市| 中西区| 腾冲县| 松潘县| 揭西县| 湖口县| 稷山县| 唐山市| 吕梁市| 冕宁县| 太康县| 徐州市| 噶尔县| 崇阳县| 桦甸市| 汉沽区| 江陵县| 贵州省| 敖汉旗| 滁州市| 定结县| 宁安市| 鹿邑县|