初めてhibernateを使います.

19673 ワード

今日初めてhibernateフレームを使って成功しました.データベースにデータを挿入したいです.初めて使うのは面倒くさいかもしれませんが、ウェブ開発ではオリジナルのSQL文を使う方法と比べて、確かに便利です.
   hibernateを使って、私はhibernate-annotations-3.3.4.GA.zipをダウンロードしました.hibernate-distriction-3.3.3.2.GA-dist.zipとsf 4 j-1.5.zipの3つの圧縮パッケージは全部公式サイトでダウンロードしました.もしこの3つのバージョンを使用していないなら、公式サイトで調べてください.コンパイルの間に思いがけない問題が起こるかもしれません.
これらの3つの圧縮パッケージを解凍した後、それぞれhibernati-distributionn-3.3.2.GAフォルダの下にあるhibernate 3.jarとlibの下にあるrequiredの中のすべてのjarパッケージを、hibernate-annotations-3.4.GAフォルダの下にあるhibernate-annotations.jarとslaf 4 j-15.8フォルダの中にあるsf 4 j-nop-1.5.jarはMyEclipseに導入します.もちろんmysql-connectore-java-java-5.8-bin.jarが必要です.では、hibernate-annotations-3.4.GAフォルダのlibディレクトリの下にあるhibernate-commons-annotations.jarとejb 3-persistence.jarの二つのjarパッケージを導入しなければなりません.全部で必要なのは12 jarパッケージです.hibera-distributions-3.3.2.GA\documentation\mann.singleのindex.はhibernateの使用マニュアルです.
今回は初めてhibernateを使います.
最初にデータベースを作成:
create database hibernate;



use hibernate;



create table hibernate(id int primary key,name varchar(20),age int);
 
Javaプロジェクトを創立して、Studentクラスを創立して、id、name、ageの3つの属性を持って、そしてsetとget方法を書きます.コードは以下の通りです.
public class Student {

    private int id;

    private String name;

    private int age;

    private String phoneNumber;

    

    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;

    }

    public String getPhoneNumber() {

        return phoneNumber;

    }

    public void setPhoneNumber(String phoneNumber) {

        this.phoneNumber = phoneNumber;

    }

}
ほとんどの人の習慣に従って、Studentクラスを作る同クラスのディレクトリにStudent.hbm.xmlファイルを作成します.コードは以下の通りです.
 1 <?xml version="1.0"?>

 2 <!DOCTYPE hibernate-mapping PUBLIC

 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 5 

 6 <hibernate-mapping package="com.nenu.hibernate">

 7     <class name="Student" table="student">    <!--             ,               ,     table="  "-->

 8         <id name="id" column="id"></id><!--      -->

 9         <property name="name" column="name"></property>    <!--        ,          ,     column="  "-->

10         <property name="age"></property>

11     </class>

12 </hibernate-mapping>
次に、ヒベルナの配置ファイルを書きます.プロジェクトのSrcディレクトリの下で、ヒベルナ.cfg.xmlを新規作成します.使用マニュアルの中でcopy(手書きではなく、直接copyを推奨します.)の配置ファイル:
 1 <?xml version='1.0' encoding='utf-8'?>

 2 <!DOCTYPE hibernate-configuration PUBLIC

 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 5 

 6 <hibernate-configuration>

 7 

 8     <session-factory>

 9 

10         <!-- Database connection settings -->

11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

12         <property name="hibernate.connection.url">

13                <![CDATA[jdbc:mysql://localhost:3306/daycode?useUnicode=true&characterEncoding=utf8]]>        

14         </property>

15         <!--       -->

16         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>

17         <property name="connection.username">root</property>

18         <property name="connection.password">root</property>

19 

20         <!--        -->

21         <!-- JDBC connection pool (use the built-in) -->

22         <!-- <property name="connection.pool_size">1</property> -->

23 

24         <!-- SQL dialect SQL  ,                ,     -->

25         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

26 

27         <!-- Enable Hibernate's automatic session context management -->

28         <!-- <property name="current_session_context_class">thread</property>       -->

29 

30         <!-- Disable the second-level cache                hibernate   -->

31         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

32 

33         <!-- Echo all executed SQL to stdout -->

34         <property name="show_sql">true</property>

35 

36         <!-- Drop and re-create the database schema on startup hibernate        -->

37         <property name="hbm2ddl.auto">update</property>

38 

39         <mapping resource="com/nenu/hibernate/Student.hbm.xml"/>

40         

41 

42     </session-factory>

43 

44 </hibernate-configuration>
これでほとんどの仕事が完了しました.今はStudent Testクラスを新たに作ってテストすればいいです.コードは以下の通りです.
 1 import org.hibernate.Session;

 2 import org.hibernate.SessionFactory;

 3 import org.hibernate.cfg.Configuration;

 4 

 5 public class studentTest {

 6     public static void main(String[] args) {

 7         Student student = new Student();

 8         

 9         student.setId(1);

10         student.setAge(22);

11         student.setName("hibernate");

12         

13         Configuration cfg = new Configuration();

14         SessionFactory sf = cfg.configure().buildSessionFactory();

15         Session session = sf.openSession();

16 

17         session.beginTransaction();//          

18         session.save(student);

19         session.getTransaction().commit();

20     }

21 }
student Test.javaを実行して、コンソールがsql文を出力しているのが見えます.ハイバーナ:insert into Techer(age、name、title、id)values(?、?、?、?);データベースを見ればデータが見えます.
hibernateを使用するのに便利なのは、オブジェクトDBMSに向かないオブジェクトに一つのオブジェクトを挿入することができるということです.