Hibernate教程01——建立一个hibernate程序
来源:互联网 发布:淘宝不卖爱奇艺 最早 编辑:程序博客网 时间:2024/06/05 18:39
Hibernate教程01——建立一个hibernate程序
一、使用pom引入hibernate的依赖库
<properties><junit.version>4.12</junit.version><hibernate.version>4.3.9.Final</hibernate.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>${hibernate.version}</version></dependency> <!-- Because this is a web app, we also have a dependency on the servlet api. --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency> <!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.9</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.9</version><classifier>sources</classifier></dependency> <dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.20.0-GA</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version></dependency>
二、创建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:3306/test</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.characterEncoding">utf8</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> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping class="liuht.hibernate.learn01.entity.Event" /><mapping class="liuht.hibernate.learn01.entity.Tree" /><mapping class="liuht.hibernate.learn01.entity.Department" /><mapping class="liuht.hibernate.learn01.entity.Menu" /> <!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> --> </session-factory> </hibernate-configuration>
三、创建实体类
@Entitypublic class Event {@Id@GeneratedValue@Column(name = "id")private Long id; private String title; private Date date; public Long getId() {return id;} public void setId(Long id) {this.id = id;} public String getTitle() {return title;} public void setTitle(String title) {this.title = title;} public Date getDate() {return date;} public void setDate(Date date) {this.date = date;}
四、创建hibernate工具类
public class HibernateUtil {private static final SessionFactory sessionFactory = buildSessionFactory();private static SessionFactory buildSessionFactory() { try { Configuration cfg = new Configuration().configure(); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); return cfg.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); }}public static SessionFactory getSessionfactory() {return sessionFactory;} }
五、建立测试程序
public class TestLearn01 {@Testpublic void test01(){ Session session = HibernateUtil.getSessionfactory().getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle("test"); theEvent.setDate(new Date()); session.save(theEvent); session.getTransaction().commit(); HibernateUtil.getSessionfactory().close();}}
0 0
- Hibernate教程01——建立一个hibernate程序
- Hibernate——建立第一个hibernate项目
- 一个简单的hibernate程序
- 一个简单的Hibernate程序
- Hibernate教程
- Hibernate教程
- 初学Hibernate--(1)基本介绍+一个简单的Hibernate程序
- eclipse 下配置hibernate以及第一个hibernate小程序
- eclipse 下配置hibernate以及第一个hibernate小程序
- 精通Hibernate——建立单向多对一关联
- 精通Hibernate——建立双向一对多关联
- struts+hibernate+spring的一个示例程序
- Hibernate构建一个CURD的程序
- 怎样编写一个简单的hibernate程序
- Hibernate 学习---建立第一个基本的hibaernate运行程序
- Hibernate4学习(1) 建立简单的Hibernate程序
- 【Hibernate】——初识hibernate
- Hibernate总结—hibernate配置文件
- Objective-c语言_使用字符串然后retainCount会出现一长串数字
- ps与kill配合
- 南邮数据结构试验---各种内排序算法实现及比较
- xcode的环境变量,Build Settings参数,workspace及联编设置
- (1) Android Junit 安卓单元测试
- Hibernate教程01——建立一个hibernate程序
- 超级计算器——两个大数相乘
- Linux/Mac osx设置C/C++终端输出字体颜色
- Javascript——DOM编程
- s5pv210 LCD控制器初始化
- 初学 HTML制作网页
- 机器学习十大经典算法支持向量机SVM(Support victor machine)(下篇)
- Python界面设计之Label
- ASM Mirroring and Disk Partnership