zoukankan      html  css  js  c++  java
  • HIbernate实现增、删、改、查。

    //大配置
    <?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">
    
    <<hibernate-configuration>
       <session-factory>
            <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
            <property name="connection.username">sb</property>
            <property name="connection.password">sb</property>
            
            <!-- 输出所有 SQL 语句到控制台。 -->
            <property name="hibernate.show_sql">true</property>
            
            <!-- 在 log 和 console 中打印出 SQL。 -->
            <property name="hibernate.format_sql">true</property>
            <!-- 方言 -->
            <property name="hibernate.dialect">    org.hibernate.dialect.Oracle10gDialect</property>
        
        
           <!-- 关联小配置 -->
    <property name="hbm2ddl.auto">update</property>  
        
    //与小配置进行关联     
    <mapping  resource="entity/Student.hbm.xml"/>
       </session-factory>
    
    </hibernate-configuration>
    
    
    //小配置
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     
     <hibernate-mapping package="entity">
         <class name="Student" table="Student">
             <id name="id" type="int" column="id">
             </id>
             <property name="name" type="string" column="name"/>
             <property name="age" type="int" column="age"/>
         </class>    
     </hibernate-mapping>
    
    
    
    package entity;
    //学生实体类
    public class Student {
     private Integer age;
     private String name;
     private Integer id;
    @Override
    public String toString() {
        return "Student [age=" + age + ", name=" + name + ", id=" + id + "]";
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    }
    
    
    
    //工具类 方便调用 更简洁
    package Util;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtil {
        private static Configuration cf=new Configuration().configure();
        private static SessionFactory sf=cf.buildSessionFactory();
        public static  Session getSession(){
            return sf.openSession();
        }
        public static void  CloseSession(){
        getSession().close();
        }
    }

    1
    public static void main(String[]args){ 2 Class clazz=Student.class(); 3 System.out.println(clazz); 4 }
    Session session;
    Transaction tx;
    @Before   //方法执行前
    public void beffore(){
      session=HibernateUtil.getSession();
      tx=Session.beginTransaction;
    }
    @After     //方法之前后
    public void after(){
    tx.commit();
    HibernateUtil.CloseSession;
    }

    1.增加

    public void addtest(){
    //创建学生对象
     Student stu=new Studetn();
     stu.setId(1);
     stu.setName("李泽阳");
     stu.sestage(23);
     
    //找到和数据库的接口 Session---SessionFactory---configur.buidSessionFactory();
     Configuration cf=new Configurration().Configur("Hibernate.cfg.xml");
     SessionFactory sf=cf.buildSessionFactory();
     session=sf.openSession();
     tx=Session.beginTransaction();
    //保存
    Session.save(stu);
    }

    2.删除

    public void deltest(){
     Student stu=new Student();
     stu.setId(1);
     Session.delect(stu);
     System.out.println("delect OK!");
    }

    3.修改

    public void updatetest(){
     Student stu=(Student)Session.get(Student.class,1);
     stu.setName("阳阳阳");
     Session.update(stu);
     System.out.println("update OK!");
    }

    4.查询

    public void selecttest(){
     Student stu=(Student)Session.get(Student.class,1);
     System.out.println("select OK!");
    }
  • 相关阅读:
    (转)LINUX CENTOS7下安装PYTHON
    (转)python的paramiko模块
    (转)Db2数据库一次生产故障详细记录---数据库坏页
    (转)性能调优和问题诊断最佳实践,第 1 部分
    (转)性能调优和问题诊断最佳实践,第 2 部分
    字符串
    html框架
    数组
    生成伪随机数
    控制语句和循环
  • 原文地址:https://www.cnblogs.com/lizeyang/p/5815064.html
Copyright © 2011-2022 走看看