当前位置: 移动技术网 > IT编程>开发语言>Java > Hibernate的Annotation版Hello world实例

Hibernate的Annotation版Hello world实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了hibernate的annotation版hello world实现方法。分享给大家供大家参考,具体如下:

需要引入的包:hibernate-commons-annotations-4.0.4.final.jar
由于我使用的是:hibernate-release-4.3.5.final,在required目录下已经有了。

bean:

import javax.persistence.column;
import javax.persistence.entity;
import javax.persistence.id;
import javax.persistence.table;
@entity
@table(name="teacher")
public class teacher {
  private int id;
  private string name;
  private string title;
  @id
  public int getid() {
    return id;
  }
  public void setid(int id) {
    this.id = id;
  }
  @column(name="name")
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  @column(name="title")
  public string gettitle() {
    return title;
  }
  public void settitle(string title) {
    this.title = title;
  }
}

对应的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.mysql.jdbc.driver</property>
    <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
    <property name="connection.username"></property>
    <property name="connection.password"></property>
    <!-- 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.internal.nocacheprovider</property>
    <!-- echo all executed sql to stdout -->
    <property name="show_sql">true</property>
    <!-- drop and re-create the database schema on startup -->
    <!--
    <property name="hbm2ddl.auto">update</property>
    -->
    <mapping resource="com/hibernate/model/student.hbm.xml"/>
    <mapping class="com.hibernate.model.teacher"/>
  </session-factory>
</hibernate-configuration>

测试类:

import org.hibernate.session;
import org.hibernate.sessionfactory;
import org.hibernate.cfg.annotationconfiguration;
import org.hibernate.cfg.configuration;
import com.huxing.hibernate.model.student;
import com.huxing.hibernate.model.teacher;
public class studenttest {
  public static void main(string[] args) {
    student a = new student();
    a.setid(123);
    a.setage(32);
    a.setname("hello hibernate!");
    teacher tea = new teacher();
    tea.setid(4);
    tea.setname("mysql");
    tea.settitle("high");
    configuration cfg = new annotationconfiguration();
    sessionfactory cf = cfg.configure().buildsessionfactory();
    session session = cf.opensession();
    session.begintransaction();
    session.save(tea);
    session.gettransaction().commit();
    session.close();
    cf.close();
  }
}

注意:代码省略了包路径。

其他方面:

1.注解可以加在属性上,也可以加在get方法上。
2.注解的mapping和xml配置的xml的不同!一个是resource,一个是class。

希望本文所述对大家hibernate框架程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网