hibernate demo入门
来源:互联网 发布:windows 10 redstone 编辑:程序博客网 时间:2024/05/19 16:27
此demo项目采用hibernate3.2
new 一个普通的java项目,引入hibernate所需要的jar包:核心jar hibernate3.jar,其他依赖jar /lib 下 ,mysql 驱动jar
hibernate 核心配置文件 :默认就放到classpath根目录下(src根目录)即可
hibernate.cfg.xml
注意:session-factory 元素中不要配置 name 属性值,否则会报如下异常
Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: Java.naming.factory.initial
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory > <!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect #hibernate.connection.driver_class com.mysql.jdbc.Driver#hibernate.connection.url jdbc:mysql:///test#hibernate.connection.username gavin#hibernate.connection.password --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3316/hibernatedemo?useUnicode=true&characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">winmshl</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="show_sql">true</property><mapping resource="com/jelly/hibernatedemo/entity/User.hbm.xml"/> </session-factory></hibernate-configuration>
User.hbm.xml 映射文件:
<?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="com.jelly.hibernatedemo.entity"><class name="User" ><id name="id"><generator class="native"/></id><property name="name"/> <property name="birthday"/> </class></hibernate-mapping>
实体bean :
package com.jelly.hibernatedemo.entity;import java.util.Date;public class User {private int id;private String name;private Date birthday;public int getId() {return id;}public String getName() {return name;}public Date getBirthday() {return birthday;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", birthday=" + birthday+ "]";}}
一个简单的demo类(测试类)
package com.jelly.hibernatedemo.main;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.jelly.hibernatedemo.entity.User;public class MainTest { public static void main(String[] args) { try { Configuration conf=new Configuration(); SessionFactory sessionFactory= conf.configure().buildSessionFactory(); Session session=sessionFactory.openSession(); Transaction tx=session.beginTransaction(); User user=new User() ; user.setName("张三"); user.setBirthday(new Date()); session.save(user); tx.commit(); session.close();} catch (Exception e) {e.printStackTrace();}}}
0 0
- Hibernate入门DEMO
- hibernate 入门学习demo
- hibernate demo入门
- Hibernate框架入门Demo
- hibernate 入门学习 Demo
- hibernate 入门增删改查demo
- hibernate 入门增删改查demo
- 【SSH系列】-- hibernate基本原理&&入门demo
- hibernate教程____hibernate入门小demo
- 【菜鸟学框架】——hibernate入门Demo
- hibernate demo
- hibernate demo
- Hibernate Demo
- Hibernate Demo
- Hibernate入门级demo 配置连接 Sql Server 2005 (Hibernate in Action)
- 配置hbm的hibernate小demo(入门级小案例ajax+struts2.0+hibernate3.0)
- hibernate dao demo
- demo(Struts2+Spring+Hibernate)
- 【连载】大话系统架构决策 - 易用性
- Linux守护进程的启动方法
- iOS全局设置navigation的样式
- Linux下使用fdisk扩展分区容量
- STM32之GPIO端口输入模式配置举例
- hibernate demo入门
- 防丢App记录(3)--发送短信
- JSP页面获取action传递过来的值
- 用Java理解Spark算子之Transformation算子
- Shell注释+字符串+数组
- Swap Nodes in Pairs
- oracle游标使用
- 总结
- 1 张图秒懂 Nova 16 种操作 - 每天5分钟玩转 OpenStack(44)