hibernate入门(一)
来源:互联网 发布:全国二手车评估软件 编辑:程序博客网 时间:2024/05/19 01:14
1:User
package com.itcast.hibernate.domain;
import java.util.Date;
public class User {
private int id; //自然主键(非业务属性)
private String name;
private Date birthday;
public User() {
}
public User(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birthday=" + birthday
+ "]";
}
}
2:hibernate.cfg.xml
<?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>
<!-- 配置连接数据库的基本信息 -->
<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">123456</property>
<!-- 配置 hibernate 的基本信息 -->
<!-- hibernate 所使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property>
<!-- 是否对 SQL 进行格式化 -->
<property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定关联的 .hbm.xml 文件 -->
<mapping resource="com/itcast/hibernate/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3: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 package="com.itcast.hibernate.domain">
<class name="User" table="user" dynamic-insert="true">
<id name="id" type="int">
<column name="id" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id>
<property name="name" length="30" type="java.lang.String">
<column name="name"/>
</property>
<property name="birthday" type="java.util.Date">
<column name="birthday"/>
</property>
</class>
</hibernate-mapping>
4:Test
package com.itcast.hibernate.test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
import com.itcast.hibernate.domain.User;
public class TestHibernate {
@Test
public void testUser() {
//1. 创建一个 SessionFactory 对象
SessionFactory sessionFactory = null;
//1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
//4.0 之前这样创建
// sessionFactory = configuration.buildSessionFactory();
//2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象
//hibernate 的任何配置和服务都需要在该对象中注册后才能有效.
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
//3).
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2. 创建一个 Session 对象
Session session = sessionFactory.openSession();
//3. 开启事务
Transaction transaction = session.beginTransaction();
//4. 执行保存操作
User user=new User();
user.setId(2);
user.setName("小月");
user.setBirthday(new java.sql.Date(new Date().getTime()));
session.save(user);//insert语句
//5. 提交事务
transaction.commit();
//6. 关闭 Session
session.close();
//7. 关闭 SessionFactory 对象
sessionFactory.close();
}
}
- Hibernate (一)hibernate入门
- Hibernate入门系列(一)
- 一、Hibernate基础入门
- Hibernate--入门(一)
- Hibernate框架入门一
- Hibernate入门(一)
- hibernate入门(一)
- Hibernate框架入门(一)
- Hibernate入门详解(一)
- Hibernate (一) 入门
- hibernate入门一
- hibernate入门(一)
- Hibernate入门(一)
- Hibernate入门(一)
- Hibernate 入门一
- Hibernate入门(一)
- Hibernate入门学习(一)
- hibernate 笔记一 简单入门
- 排成一条线的硬币
- 1-4:定位、z-index
- 异常的解决
- csv文件转化为数组及数组的切片
- 给你的电脑降降温!!!!!!
- hibernate入门(一)
- 【转】(DT系列一)DTS结构及其编译方法----不错
- HDU-3549 Flow Problem (最大流模板)
- Cow Contest POJ
- hdu 6033 Add More Zero
- LightOJ
- Servlet
- 水题。。。810A
- 欢迎使用CSDN-markdown编辑器