zoukankan      html  css  js  c++  java
  • Hibernate基于【XML】和【注解】——完整实例

    Eclipse中新建Java Project工程:

    工程结构  和  需要的Jar包:

     我用的SqlServer数据库,所以连接数据库的Jar包是sqljdbc4.jar

    一、基于XML配置

    1、实体类(对应数据表中的字段)

    package Entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    public class Student {
    private int id;
        private String name;
        private int age;
        
        public Student(){}
        
        public Student(int id,String name,int age){
            this.id=id;
            this.name=name;
            this.age=age;
        }
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
        
        
    }

    2、Student.hbm.xml(XML文件 将实体类 对应 数据表 和 表中字段)

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD//EN"
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
    
    <hibernate-mapping>
       <class name="Entity.Student" table="Student">
          <meta attribute="class-description">
             This class contains the employee detail. 
          </meta>
          <id name="id" type="int" column="id">
             <generator class="native"/>
          </id>
          <property name="name" column="name" type="string"/>
          <property name="age" column="age" type="int"/>
       </class>
    </hibernate-mapping>

    3、hibernate.cfg.xml(配置连接数据库、映射Student.hbm.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">
    
    <hibernate-configuration>
    
        <session-factory>
    
            <!-- Database connection settings 数据库连接配置-->
            <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
            <property name="connection.url">jdbc:sqlserver://localhost:1433;database=test</property>
            <property name="connection.username">sa</property>
            <property name="connection.password">Zhutou0216</property>
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            <!-- SQL dialect 方言-->
            <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    
    
            <!-- Echo all executed SQL to stdout 在控制台打印后台sql语句-->
            <property name="show_sql">true</property>
            <!-- 格式化语句 -->
           <property name="format_sql">true</property>
            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>
            
            <!-- 映射数据库对应实体类: xml配置文件生效方式 --> 
            <mapping resource="entity/Student.hbm.xml" />
            
    
    
        </session-factory>
    
    </hibernate-configuration>

    4、MainApp(运行类)

    package Entity;
    
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class MainApp {
    
        
        public static void main(String[] args) {
            Configuration cfg = new Configuration().configure();
            SessionFactory factory = cfg.buildSessionFactory();
            
            Session session = null;
            try{
                session = factory.openSession();
                
                session.beginTransaction();
                
                Student student = new Student();
                student.setName("en");
                student.setAge(88);
                session.save(student);
                session.getTransaction().commit();
            }catch(Exception e){
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally{
                if(session != null){  
                    if(session.isOpen()){  
                        //关闭session  
                        session.close();  
                    }  
                }
            }
            
        }
        
        
    }

    二、基于注解配置

    1、实体类(在实体类中用注解方式对应数据表和表中字段,因此用不着Student.hbm.xml了)

    package Entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="Student")
    public class Student {
    
        @Id @GeneratedValue
        @Column(name="id")
        private int id;
        
        @Column(name="name")
        private String name;
        
        @Column(name="age")
        private int age;
        
        public Student(){}
        
        public Student(int id,String name,int age){
            this.id=id;
            this.name=name;
            this.age=age;
        }
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
        
        
    }

    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">
    
    <hibernate-configuration>
    
        <session-factory>
    
            <!-- Database connection settings 数据库连接配置-->
            <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
            <property name="connection.url">jdbc:sqlserver://localhost:1433;database=test</property>
            <property name="connection.username">sa</property>
            <property name="connection.password">Zhutou0216</property>
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            <!-- SQL dialect 方言-->
            <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    
    
            <!-- Echo all executed SQL to stdout 在控制台打印后台sql语句-->
            <property name="show_sql">true</property>
            <!-- 格式化语句 -->
           <property name="format_sql">true</property>
            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>
            
            <!-- 映射数据库对应实体类: xml配置文件生效方式 --> 
            <!-- <mapping resource="entity/Student.hbm.xml" /> -->
            
            <!-- 映射数据库对应实体类:注解生效方式生效方式 --> 
            <mapping class="Entity.Student"/>
    
        </session-factory>
    
    </hibernate-configuration>

    3、MainApp(这里没有改变)

    package Entity;
    
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class MainApp {
    
        
        public static void main(String[] args) {
            Configuration cfg = new Configuration().configure();
            SessionFactory factory = cfg.buildSessionFactory();
            
            Session session = null;
            try{
                session = factory.openSession();
                
                session.beginTransaction();
                
                Student student = new Student();
                student.setName("en");
                student.setAge(88);
                session.save(student);
                session.getTransaction().commit();
            }catch(Exception e){
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally{
                if(session != null){  
                    if(session.isOpen()){  
                        //关闭session  
                        session.close();  
                    }  
                }
            }
            
        }
        
        
    }
  • 相关阅读:
    JAVA假期第五天2020年7月10日
    JAVA假期第四天2020年7月9日
    JAVA假期第三天2020年7月8日
    JAVA假期第二天2020年7月7日
    JAVA假期第一天2020年7月6日
    CTF-sql-group by报错注入
    CTF-sql-order by盲注
    CTF-sql-sql约束注入
    CTF-sql-万能密码
    X-Forwarded-for漏洞解析
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/7216216.html
Copyright © 2011-2022 走看看