日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Hibernate3.3复习知识点二】 - 配置hibernate环境(annotations)

發布時間:2023/11/30 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Hibernate3.3复习知识点二】 - 配置hibernate环境(annotations) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

配置文件hibernate.cfg.xml中引入:<mapping class="com.bjsxt.hibernate.Teacher"/>

<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">bjsxt</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.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/bjsxt/hibernate/Student.hbm.xml"/><mapping class="com.bjsxt.hibernate.Teacher"/></session-factory></hibernate-configuration> View Code

?

實體類:

package com.bjsxt.hibernate;import javax.persistence.Entity; import javax.persistence.Id;@Entity public class Teacher {private int id;private String name;private String title;@Idpublic 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 String getTitle() {return title;}public void setTitle(String title) {this.title = title;} } View Code

注解所在包:

import javax.persistence.Entity;
import javax.persistence.Id;

類與主鍵:@Entity? @ Id

?

測試類:

public class TeacherTest {public static void main(String[] args) {Teacher t = new Teacher();t.setId(1);t.setName("t1");t.setTitle("middle");SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();Session session = sessionFactory.getCurrentSession();session.beginTransaction();session.save(t);session.getTransaction().commit();} } View Code

注解形式新建sessionFactory 時用new AnnotationConfiguration()。

?

轉載于:https://www.cnblogs.com/surge/p/3210614.html

總結

以上是生活随笔為你收集整理的【Hibernate3.3复习知识点二】 - 配置hibernate环境(annotations)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。