hibernate学习二(HelloWorld)

一、建立hibernate配置文件

  在工程Hibernate_01_HelloWorld下的src上建立hibernate.cfg.xml,打开hibernate-release-4.3.11.Final/documentation/manual/en-US/html_single/index.html(hibernate开发文档),在文档中搜索configuration,找到 hibernate.cfg.xml配置文件的模板,复制到自己的工程中;

  1.修改配置文件适用jdbc和mysql数据库

 <?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/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.internal.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> <mapping resource="com/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>

  1. Database connection settings, 数据库连接的配置;

  2. SQL dialect, hibernate的方言选择,支持多种数据库语言,因我用的是MySQL数据库,所有适用 MySQLDialect,其他可以从文档里查;

  3. Echo all executed SQL to stdout, 是否显示sql语句;

  4. Drop and re-create the database schema on startup, hibernate启动时删除或者创建数据库结构的策略;

  5.Orm, pojo映射配置文件;

二、建立实体类

  在src目录下建立实体类包com.model,并创建实体类Student.java

 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.java同意路径,Student.hbm.xml(建议从hibernate文档中查找)

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.hibernate.tutorial.domain">
[...]
</hibernate-mapping>

  修改Student.hbm.xml,适用student.java类与数据库student表的映射;

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 设置到对应实体类所在的路径 -->
<hibernate-mapping package="com.model">
<!-- name对应实体类的名字 -->
<class name="Student">
<!-- 属性id为设置主键,name的值对应实体类里id属性 -->
<id name="id"></id>
<!-- 其他字段的映射 -->
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>

二、在Mysql数据库中创建student表

CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

三、建立hibernate辅助类

  根据hibernate官方文档,最好适用hibernate util辅助类,在src目录下新建com.util包,并在下面建立HibernateUtil.java类(博主学习的时候,这个版本的文档里的util类有错误,下面的代码已经改正)

 package com.util;

 import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().applySettings(new Configuration().configure().getProperties()).build());
}
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;
} }

四、编写测试类

  建立测试类Main,代码如下:

 import org.hibernate.Session;
import org.hibernate.SessionFactory; import com.model.Student;
import com.util.HibernateUtil; public class Main {
public static void main(String[] args) {
//设置要插入的数据
Student s = new Student();
s.setId(1);
s.setName("233");
s.setAge(100);
//通过util类创建sessionFactory
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
//开启事务
session.beginTransaction();
//将student存入数据库
session.save(s);
session.getTransaction().commit();
//关闭资源
session.close();
sessionFactory.close();
}
}

  结果:

  hibernate学习二(HelloWorld)

项目结构:

  hibernate学习二(HelloWorld)

----------------------------------------------------------------------------------------------------

此文为个人学习记录,如有参考转载,请注明出处 黑白熊的博客 http://www.cnblogs.com/xiong233/

上一篇:gpg --verify之"Can't check signature: No public key"


下一篇:JS 获取css transform中的值