Hibernate(一)前奏

隔了这么久,终于有时间来学学Hibernate了,话说配置还真繁杂,不过是后面开发的基础。

在开发过程中,经常看到分层现象,主要目的是为了解耦。

B/S最少分三层:

view 表示层 action/sevlet/xx 数据 jsp 模板service 业务层dao 数据访问层

下面是真正的Hibernate入门。

首先是导入一些必须的jar包;数据库采用MySQL,连接数据库当然要数据库驱动包。

工程目录:

调试建的工程是Java Project

实体类User.java

package test.hibernate.domain;public class User {private int id;private String name;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;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn "[User:id="+id+",name:"+name+"]";}}配置文件hibernate.cfg.xml,,其中文件名是hibernate.cfg<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN"""><hibernate-configuration><session-factory name="foo"><!– 配置数据库信息 –><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/hibernate_2015</property><!– 可简写为<property name="connection.url">jdbc:mysql:///hibernate_2015</property> –><property name="connection.username">root</property><property name="connection.password">686175</property><!– 显示生成的sql语句,不写的话默认是false –><property name="show_sql">true</property><mapping resource="test/hibernate/domain/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"""><hibernate-mapping package="test.hibernate.domain"><class name="User" table="t_user"><id name="id" type="integer" column="id"><generator class="native"/></id><property name="name" type="string" column="name"/></class></hibernate-mapping>id的class属性值取为native表示采用数据库中的配置属性

主要测试类App.java

package test.hibernate.domain;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.classic.Session;import org.junit.Test;public class App {private static SessionFactory sessionFactory;static{Configuration cfg=new Configuration();cfg.configure("hibernate.cfg.xml");sessionFactory=cfg.buildSessionFactory();}@Testpublic void testSave() throws Exception {User user=new User();user.setName("张三");Session session=sessionFactory.openSession();Transaction transaction = session.beginTransaction();session.save(user);transaction.commit();session.close();//关闭session,释放资源}@Testpublic void testGet() throws Exception {Session session=sessionFactory.openSession();Transaction transaction=session.beginTransaction();User user=(User) session.get(User.class, 1);System.out.println(user);transaction.commit();session.close();}}

Hibernate主配置文件1,配置的key前面的hibernate.前缀 可以有,也可以没有。如hibernate.dialect或dialect都可以。2,按作用可分为三类:1,数据库信息<property …>方言、JdbcUrl、驱动、用户名、密码2,导入映射文件<mapping …>3,其他配置<property …>show_sql显示生成的SQL语句format_sql格式化生成的SQL语句hbm2ddl.auto 自动生成表结构hibernate.hbm2ddl.auto生成表结构的两种方式:1,hbm2ddl.auto create 没表的时候自动创建,先删除,在创建 update 如果表不存在就创建,不一样就更新,一样就什么都不做 create-drop 初始化时创建表,SessionFactory执行close()时删除表 validate 验证表结构与HBM中的是否一致,如果不一致就抛异常2,使用SchemaExport工具类注意:只能建表,不能建库

package test.hibernate.domain;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;public class CreateSchema {@Testpublic void test() throws Exception {Configuration cfg = new Configuration().configure();SchemaExport schemaExport = new SchemaExport(cfg);/* * 第一个参数:打印ddl到控制台* 第二个参数:导出script到数据库 */schemaExport.create(true, true);}}问:一只小狗在沙漠中旅行,结果死了,问他是怎么死的?

Hibernate(一)前奏

相关文章:

你感兴趣的文章:

标签云: