hibernate 初学配置文件版

1.jar准备:mysql -connection.jar

antlr.jar

commons_collections.jar

dom4j.jar

javassist.GA.jar

jta.jar

slf4j-api-1.5.8.jar

slf4j-nop-1.5.8.jar(这个不要丢)

2.mysql 表准备:

 create table student(
 id int primary key auto_increment,
 name varchar(20),
 age int);

3.hibernate.cfg.xml:别忘了放在src下面。

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- 数据库连接设置Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">8291289</property>

        <!-- jdbc 连接池,hibernate自带  JDBC connection pool (use the built-in) -->
       <!--  <property name="connection.pool_size">1</property>-->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
            <!-- <property name="current_session_context_class">thread</property>-->

        <!-- Disable the second-level cache  -->
     <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- 是否让hibernate自动生成建表语句Drop and re-create the database schema on startup -->
        <!--  <property name="hbm2ddl.auto">update</property>-->

        <mapping resource="beans/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

4.实体类准备

package beans;

public class Student {
private int id;
private String name;
private int 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;
}

}
5.Student.hbm.xml 要和Student实体类放在一个包下面

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="beans">
   <class name="Student" table="student">
        <id name="id" column="id">
            <generator class="identity"/>
        </id>
          <property name="name" type="string" column="name"/>
        <property name="age" type="int" column="age"/>
    </class>
</hibernate-mapping>

6.测试类


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import beans.Student;


public class StudentTest {

 public static void main(String[] args) {
  Student stu=new Student();
  //mysql 对应的表中已经设置id primary key auto_increment 自增长,对象不需要重新进行设置。
  //stu.setId(5);
  stu.setAge(12);
  stu.setName("mingcheng");
  
  Configuration c=new Configuration().configure();
  SessionFactory sf=c.buildSessionFactory();
  Session s=sf.openSession();
  s.save(stu);
     s.beginTransaction().commit(); 
  s.close();
  sf.close();
 }

}

猜你喜欢

转载自blog.csdn.net/wnn654321/article/details/8988169