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!");
    }
  • 相关阅读:
    Eclipse的tomcat插件
    sql优化:
    ecipse theme
    load()和get()的区别
    eclipse手动指定启动的jdk版本
    TOD&FIXME&XXX
    命令式和声明式
    显示器调色温
    jdeveloper优化:
    win7 登录后只能使用“临时配置文件”,原来的配置文件无法启用!
  • 原文地址:https://www.cnblogs.com/lizeyang/p/5815064.html
Copyright © 2011-2022 走看看