HIbernate笔记01全局配置

来源:互联网 发布:淘宝论坛 买家 编辑:程序博客网 时间:2024/05/24 15:38

上面的图片是hibernate开发包解压后的lib文件夹下面的文件夹,一般我们开发只需要讲require文件家下面的jar包和jpa的文件夹下面的jar包引入即可

 

hibernate.cfg.xml配置文件详解

<!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><!-- 一、数据库信息:数据库方言(是一个类的全名)与数据库连接信息 --><property name="dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="connection.url">jdbc:mysql:///hibernate_day01</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.username">root</property><property name="connection.password">root</property><!-- 二、其他配置 --><property name="show_sql">true</property><property name="format_sql">false</property><!-- create: 先删表,再建表。create-drop: 启动时建表,退出前删表。update: 如果表结构不一致,就创建或更新。validate: 启动时验证表结构,如果不致就抛异常。 --><property name="hibernate.hbm2ddl.auto">update</property><!-- 三、导入映射配置文件<mapping resource="cn/yht/a_helloworld/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"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="cn.itcast.a_helloworld.User" table="user"><id name="id" type="int" column="id">            <generator class="native"/></id><property name="name" type="string" not-null="true" column="name"/></class></hibernate-mapping>

实体类User.java文件

package cn.yht.a_helloworld;/** * 用户对象 *  * @author yht *  */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() {return "[User: id=" + id + ", name=" + name + "]";}}


测试程序App,java

package cn.yht.a_helloworld;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App {private static SessionFactory sessionFactory;static {// 读取配置文件并生成Session工厂对象Configuration cfg = new Configuration();// cfg.configure("hibernate.cfg.xml"); // 加载指定的配置文件// cfg.configure(); // 读取默认的配置文件(hibernate.cfg.xml)// cfg.addResource("cn/itcast/a_helloworld/User.hbm.xml");// cfg.addClass(User.class); //// sessionFactory = cfg.buildSessionFactory();sessionFactory = new Configuration()//.configure()//.addClass(User.class)// 加载指定类对应的映射文件(以类名为前缀,后缀为.hbm.xml的同一个包下的文件).buildSessionFactory();}// 保存对象到数据库@Testpublic void testSave() throws Exception {// 准备对象User user = new User();user.setName("张三");// 保存到数据库中Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();session.save(user); // 保存tx.commit();session.close();}// 从数据库中获取一条数据@Testpublic void testGet() throws Exception {Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();User user = (User) session.get(User.class, 1); // 从数据库中获取id为1的User数据tx.commit();session.close();System.out.println(user); // 显示信息}}