hibernate 入门学习demo
来源:互联网 发布:mysql rowid用法 编辑:程序博客网 时间:2024/05/02 21:39
2014年,开始学习java ,边学习css+html+servlet+jsp, 做了一个简单的学校精品课程网站,最近在做一个SSH 商业网站,也是边学边做,但是很多东西都是别人搭建了。虽然会用,但是还是一知半解,还是写东西记录下,自己开始学着搭建框架。
Hibernate用于管理JAVA类到数据库表的映射(包括Java数据类型到SQL数据类型的映射),提供数据库查询和获取数据的方法,可以大幅度缩短使用JDBC处理数持久化的时间
Hibernatede 的使用让开发者 完全采用面向对象的方式来开发应用程序。
ORM(Object/Relation Mapping),对象/关系数据库映射。
第一个hibernate 入门小例子:
第一步 进去网站下载hibernate相应的jar文件 www.hibernate.org
需要的包:hibernate3.jar 已经lib下required目录下所有的包,jpa目录下的包,以及一些其他包。还有mysql驱动包。
第二步,写hibernate.cfg.xml
<?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> <!-- hibernate 为数据库名--> <property name="connection.username">root</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.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> <!-- 设置乱码--> <property name="connection.useUnicode">true</property> <property name="connection.characterEncoding">UTF-8</property> <mapping resource="model/Student.hbm.xml"/><mapping class="model.Student"/> </session-factory></hibernate-configuration>
第三步,新建数据库(hibernate), 建表(student id name age)
第四步,写Student类。
package model;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;}}第五步,写相应的Student的映射文件, Student.hbm.xml.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="model.Student" table="student"> <id name="id" column="id" type="java.lang.Integer"> <generator class="increment"/> </id> <property name="name" type="java.lang.String" column="name"></property> <property name="age" type="java.lang.Integer" column="age"></property></class></hibernate-mapping>说明 class 对应的名字是包名加类名, 后面的table 对应的数据库的中的表面
id 中name对应的是 student中的id,column中id对应的是数据库表中的id,可以根据数据库的字段来改成对应的,如"_id"。property用于其他字段,对应的name为student 中的属性, column对应的是数据库表中的属性。
第六步写Student操作类,这里为studenttext
package hibernate;import model.Student;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;@SuppressWarnings("deprecation")public class studenttest { public static void main(String[] args){ Student s = new Student();s.setName("l我i");s.setAge(9); Configuration conf = new Configuration().configure();//实例化Configuration 并加载hibernate.cfg.xml文件<span style="white-space:pre"></span> SessionFactory sf = conf.buildSessionFactory(); <span style="white-space:pre"></span>//创建sessionfactory<span style="white-space:pre"></span> Session sess = sf.openSession(); //创建session<span style="white-space:pre"></span> Transaction tx = sess.beginTransaction(); //开始事务<span style="white-space:pre"></span> sess.save(s);<span style="white-space:pre"></span> tx.commit();<span style="white-space:pre"></span> sess.close();<span style="white-space:pre"></span> sf.close(); }}
运行之后可以插入数据到数据库里面。总结:jar一定要找对,插入中文乱码需要在配置文件中加一段配置代码。
实践操作类步骤: 获取 (1)获取Configuration (2) 获取SessionFactory (3)获取Session,打开事务 (5)用面向对象的方式操作数据库 (6)关闭事务,关闭Seesion
第一篇文章,以后把自己学的东西,写出来,加深印象,已经总结能力。加油 2015.7.26
- hibernate 入门学习demo
- hibernate 入门学习 Demo
- Hibernate入门DEMO
- hibernate demo入门
- Hibernate框架入门Demo
- jsrender 学习 入门 demo
- hibernate学习笔记之demo
- hibernate 入门增删改查demo
- hibernate 入门增删改查demo
- 【SSH系列】-- hibernate基本原理&&入门demo
- hibernate教程____hibernate入门小demo
- 【Hibernate学习笔记】hibernate入门
- hibernate 入门学习
- hibernate validate入门学习
- java学习:Hibernate入门
- hibernate学习笔记--入门
- Hibernate入门学习
- Hibernate 学习入门1
- 【剑指Offer面试题】 九度OJ1385:重建二叉树
- shell 脚本统计文件梳理及获取磁盘占有率
- 8.3 Qemu启动参数管理
- php函数base64_encode后的参数包含加号解析出错的解决方案
- u3d面试题11
- hibernate 入门学习demo
- Hdu 2289
- ACdream DP专题训练
- Python 异常处理
- 9.1 libvirt与openstack
- [FOJ 2123] 数字的孔数
- 安卓自定义EditText样式
- u3d面试题2
- Tair-淘宝自主开发的一个分布式key/value存储系统