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); // 显示信息}}
- HIbernate笔记01全局配置
- Hibernate二级缓存的全局配置
- hibernate配置笔记
- hibernate配置笔记
- hibernate配置笔记
- hibernate配置详解笔记
- 全局配置
- hibernate学习笔记(1):hibernate配置实例
- Hibernate学习笔记:配置hibernate日志
- Hibernate学习笔记:配置hibernate日志
- hibernate笔记(一)hibernate基本配置
- Hibernate学习笔记(十)Hibernate 高级配置
- Spring中Hibernate配置笔记
- springboot学习笔记3(全局错误页面配置)
- struts2笔记之常量配置,全局视图配置,默认配置,属性注入配置
- Hibernate实战_笔记20(全局XML映射元数据、全局的注解元数据、占位符)
- hibernate笔记01 Hibernate介绍
- Hibernate笔记=>连接池配置
- struts2 package 配置说明
- 查看LoadRunner脚本请求日志和服务器返回值方法
- mysql-Front连接本机mysql-sqlserver:
- JavaScript学习篇之——面向对象
- oracle11g导入dmp文件命令
- HIbernate笔记01全局配置
- android ro.hardware 属性修改。
- 中断线程Interrupt与Interrupted的用处
- TOJ 4365 ZOJ 3623 Battle Ships / 完全背包
- yii General error: 2014 Cannot execute queries while other unbuffered queries are active
- Linux操作系统文件系统基础知识详解
- dos界面下的sql语句
- less 命令
- sa--网卡自动重启