day_01hibernateHelloWorld

星期一, 十二月 28, 2015 23:01:37

课程内容

1.helloworld

   a)xml

   b)annotation

2.hibernate原理模拟

  ----什么是O/R mapping以及为什么要有O/R mapping

3.

client-------app server(tomcat)---------struct(分发)-----------action-------------service(业务逻辑处理)-------------DAO------------model(实体类)

原来的:

client----------1.new()------------------>Student

2.jdbc

面向对象的替代面向关系的这层的逻辑。

用hibernate编程用面向对象的方法。

hibernate:

client 

 1.new()   Student

 2. configuration

 3.buildsessionFactory()               SessionFactory

 4.openSesiion()    ----createMessage()-----session

5.save(Student s)()   pesist()DB

hibernate帮我们屏蔽了关系型,直接使用面向对象的方法。

面向对象的写法---hibernate---面向关系的数据库

风格

1.先脉络,后细节

2.先操作,后原理

3.重Annotation,轻xml配置文件

    JPA   java持久化api

    hibernate-extension

资源:

1.http:www.hibernate.org

hibernate-annotation-3.4

hibernate-distribution-3.3

slf4j-1.5.8

一、new一个java project,名为hibernate_0100_HelloWorld

2.学习建立user library -hibernate,并加入相应的jar包

    2.1在windows-->preference-->java-->user library

         new自己包,名称为hibernate-->add jars

    2.2在该libraries中加入hibernate所需jar包

        1.hibernate core    

        2./required

        3.slf-nop.jar

3.引入mysql的jdbc驱动包

4.在mysql中建立对应的数据库以及表

     1.create database hibernate

     2.user hibernate

     3.create table Student(id int primary key,name varchar(20),age int);

5.建立hibernate配置文件hibernate.cfg.xml

    1.从参考文档中copy  

    2.修改赌赢的数据库连接

     3.注释掉暂时用不上的内容

6.建立Student类

7.建立Student映射文件Student.hbm.xml

  参考文档

8.将映射文件加入到hibernate.cfg.xml

  参考文档

9.写测试类Main,在Main中对Student对象进行直接的存储测试

    参考文档

二、在mysql中创建数据库和表

create database hibernate;

use hibernate;

create table student(id int primary key,name varchar(20),age int);

hibernate-annotations-3.4.0 GA

hibernate-distribution-3.3.2.GA-dist

slf4j-1.5.8

原理

object relatation的sql语句

从sql里拿出来再进行组装成object

建立java project

window-->preference-->java-->user libraries

需要jar   在distribution中的hibernate3.jar

G:\asiainfo\web_hp2\Hibernate3.3.2\jar\hibernate-distribution-3.3.2.GA\lib\required  所以的jar

G:\asiainfo\web_hp2\Hibernate3.3.2\jar\slf4j-1.5.8  slf4j-nop-1.5.8.jar

在src中新建file

hibernate.cfg.xml

数据库配置连接

com.mysql.jdbc.Driver

jdbc:mysql://localhost/hibernate

root

root

package com.zhuhw.hibernate;

public class Student {
		private int id;
		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;
		}
		private String name;
		private int age;
}
<?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">root</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>-->
		<!-- hibernate去哪里找这个配置文件 -->
        <mapping resource="com/zhuhw/hibernate/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 找不到entity,是因为这个类没有改包名 -->
<hibernate-mapping package="com.zhuhw.hibernate">
	<class name="Student" table="student" >
	<!-- id主键;name=id对应的是Student中的getid() -->
		<id name="id" ></id>
		<property name="name" />
		<property name="age" />
		<!-- hibernater知道了怎么将class与表中的字段对应到一起了 -->
	</class>
</hibernate-mapping>
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.zhuhw.hibernate.Student;


public class StudentTest {
	public static void main(String[] args){
		Student s = new Student();
		s.setId(1);
		s.setAge(23);
		s.setName("ll");
		
		Configuration cf = new Configuration();
		SessionFactory sf = cf.configure().buildSessionFactory();
		Session session = sf.openSession();
		//在hibernate中执行操作要在一个事务里面
		session.beginTransaction();
		session.save(s);
		session.getTransaction().commit();
		session.close();
		sf.close();
		
	}
}

运行结果:

Hibernate: insert into student (name, age, id) values (?, ?, ?)

猜你喜欢

转载自yuzhouxiner.iteye.com/blog/2267306
今日推荐