hibernate hql 使用实例

    xiaoxiao2022-06-28  29

    1. 导入hibernate3   包

    2.编写:  hibernate.cfg.xml

    <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"           "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools.                   --> <hibernate-configuration>     <session-factory>         <property name="dialect">             org.hibernate.dialect.MySQLDialect         </property>         <property name="connection.url">             jdbc:mysql://localhost:3306/student         </property>         <property name="connection.username">root</property>         <property name="connection.password">root</property>         <property name="connection.driver_class">             com.mysql.jdbc.Driver         </property>         <property name="myeclipse.connection.profile">student</property>         <mapping resource="entyti/Student.hbm.xml" />         <mapping resource="entyti/Scj.hbm.xml" />         <mapping resource="entyti/Chengji.hbm.xml" />     </session-factory> </hibernate-configuration>

    3.编写实体类映射文件

    <hibernate-mapping>     <class name="entyti.Student" table="student" catalog="student">         <id name="id" type="java.lang.Integer">             <column name="id" />             <generator class="identity" />         </id>         <property name="name" type="java.lang.String">             <column name="name" />         </property>         <property name="sex" type="java.lang.Short">             <column name="sex" />         </property>         <property name="score" type="java.lang.Integer">             <column name="score" />         </property>         <property name="dept" type="java.lang.String">             <column name="dept" />         </property>         <property name="chengji" type="java.lang.String">             <column name="chengji" />         </property>         <property name="classname" type="java.lang.String">             <column name="classname" />         </property>     </class> </hibernate-mapping>

    4.  编写实体类

    5.  编写

    public class BaseDaoTest {     static  SessionFactory   sf  =null;     static{       Configuration  cf = new Configuration().configure();         sf  = cf.buildSessionFactory();       }    public Session getSession(){      return  sf.openSession();     } }

    6.查询

    public class HibernateTest  extends  BaseDaoTest {     @Test     public  void select(){          //得到session               Session  session  = this.getSession();           String  sql ="from Student";           //查询出数据           List<Student>list  = session.createQuery(sql).list();           for(Student stu:list){            System.out.println(stu.getId());            System.out.println(stu.getName());         }     } } 7.添加

        @Test     public  void add(){           //得到session           Session  session  = this.getSession();           //得到事务           Transaction ts = session.beginTransaction();           try {             //实例化对象               Student stu  = new Student();               stu.setName("yuntaoss");               stu.setClassname("hhhhhhh");               //保存添加               session.save(stu);               //提交事务               ts.commit();         } catch (Exception e) {             ts.rollback();             }     } //修改     @Test     public void xiugai(){                   //得到session           Session  session  = this.getSession();           //得到事务           Transaction ts = session.beginTransaction();           try {             //实例化对象              Student stu =  (Student)session.get(Student.class,1);              stu.setName("ssssssddd");               //保存添加修改               session.update(stu);               //提交事务               ts.commit();         } catch (Exception e) {             ts.rollback();             }       } //删除

    @Test     public   void deleteTest(){         //得到session           Session  session  = this.getSession();           //得到事务           Transaction ts = session.beginTransaction();           try {             //实例化对象              Student stu =  (Student)session.get(Student.class,13);               //删除               session. delete(stu);               //提交事务               ts.commit();         } catch (Exception e) {             ts.rollback();             }              }

    转载请注明原文地址: https://ju.6miu.com/read-1124365.html

    最新回复(0)