Hibernate 学习(1)
来源:互联网 发布:狗听得懂人话吗 知乎 编辑:程序博客网 时间:2024/04/29 18:41
O/R mapping
对象关系映射
把数据库,sql语言f封装成对象,让我们的编程更加对象化
这就是好处
第一步,建立hibernate用户库
第二步,建立实体类student
package com.bjsxt.hibernate;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.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"><!-- 这个是映射文件 指定表里面的字段id 是 id成员变量表里面的字段name 是name成员变量表里面的字段age是 age成员变量数据库表不分大小写--><hibernate-mapping> <class name="com.bjsxt.hibernate.Student" column="student"> <id name="id" column="id"/> <property name="name" column="name" /> <property name="age" column="age"/> </class></hibernate-mapping>
hbm就是hibernate mapping的意思
第三步,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> <property name="connection.username">root</property> <property name="connection.password">1995</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect 数据库方言,每个数据库软件方言不一样,这里是mysql--> <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 二级缓存disable --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout就是执行的过程中把生成的sql语句打印出来 --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup hbm hibernate mapping ddl data define language hibernate自动帮你生成建立表语句 --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/> <mapping class="com.bjsxt.hibernate.Teacher"/> </session-factory></hibernate-configuration>
第四步,写测试程序
package com.bjsxt.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;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(); }}
0 0
- Hibernate学习1-什么是Hibernate
- Hibernate学习手记1
- hibernate学习笔记1
- Hibernate学习摘要(1)
- Hibernate学习笔记(1)
- Hibernate学习日记(1)
- Hibernate 学习笔记 1
- hibernate学习笔记1
- Hibernate学习笔记1
- Hibernate学习笔记1
- Hibernate学习1 -- 概述
- Hibernate快速学习(1)
- Hibernate学习笔记1
- Hibernate学习1
- hibernate学习笔记-1
- hibernate学习笔记(1)
- hibernate学习笔记 1
- Hibernate学习笔记(1)
- spring源代码之bean的加载(一)
- 环境变量
- 树的简谈
- 2010 - 2017, 我在华为的谘询
- 16年公司年会后的感悟
- Hibernate 学习(1)
- Java的异步HttpClient
- 高性能的关键:Spring MVC的异步模式
- 【视频】BSR缺陷引起的上行流量问题
- 春节将至,为何我如此“恐归”
- LeetCode 387. First Unique Character in a String
- Codeforces 758B-Blown Garland
- UVA 11992 Fast Matrix Operations
- hdu1512 Monkey King