<?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.username" >root </property>
<!-- 密码 -->
<property name="connection.password" >root </property>
<!-- url -->
<property name= "connection.url">
jdbc:mysql:// localhost:3306/hibernate512
</property>
<!-- 方言 告诉hibernate,要操作的数据库是mysql -->
<property name="dialect" >org.hibernate.dialect.MySQLDialect </property>
<!-- 导入驱动 -->
<property name= "connection.driver_class" >
com.mysql.jdbc.Driver
</property>
<!-- validate
只检查结构 update 检查结构,更新或者创建表 create 每次启动 hibernate时,都要创建表 create-drop
启动 hibernate时创建表,当 hibernate关闭时,删除表
-->
<property name= "hbm2ddl.auto">update</property >
<!--显示sql语句的输出
-->
<property name= "show_sql"> true</ property >
<property name= "format_sql"> true</ property >
<!--关联映射文件,手动添加,然后自动生成!
-->
<mapping resource="com/oteman/hibernate/domain/Person.hbm.xml" />
</session-factory >
</hibernate-configuration>
|
package com.oteman.hibernate.domain;
public class Person
{
Long pid;
String pname;
String pgender;
public Long
getPid() {
return pid ;
}
public void setPid(Long
pid) {
this.pid =
pid;
}
public String
getPname() {
return pname ;
}
public void setPname(String
pname) {
this.pname =
pname;
}
public String
getPgender() {
return pgender ;
}
public void setPgender(String
pgender) {
this.pgender =
pgender;
}
}
|
<?xml version= "1.0" encoding ="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- 数据库表和类之间的映射关系 字段和表列名之间的关系 -->
<hibernate-mapping>
<class name="com.oteman.hibernate.domain.Person" table="Person" >
<id name= "pid" column ="pid" length="5" type="java.lang.Long" >
<generator class= "increment"></generator >
</id>
<property name= "pgender" column ="gender" length="10" type="string"></ property>
<property name= "pname" column ="name" length="20" ></property>
</class >
</hibernate-mapping>
|
@Test
public void testCreateTable(){
Configuration configuration= new Configuration();
configuration.configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
}
|
/**
* 向数据库中增加数据
*/
@Test
public void add(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person p= new Person();
p.setPgender( "男");
p.setPname( "张三");
session.save(p);
transaction.commit();
session.close();
}
/**
* 更新数据, hibernate具有快照机制,会将对象前后的值进行比较,如果发现对象的属性没有发生改变,那么就不进行update操作,可以观察 sql语句;
*/
@Test
public void update(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person=(Person) session.get(Person. class,
2l);
person.setPname( "哈哈");
/*//第二种形式:
Person p=new Person();
p.setPgender("男");
p.setPid(2L);
p.setPname("李四");*/
session.update(person);
transaction.commit();
session.close();
}
/**
* 删除数据
*/
@Test
public void delete(){
Session session= sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person=(Person) session.get(Person. class,
1l);
session.delete(person);
transaction.commit();
session.close();
}
/**
* 查询所有!
*/
@Test
public void query(){
Session session= sessionFactory.openSession();
List list = session.createQuery("from
Person").list();
System. out.println(list.size());
session.close();
}
|
public class GeneratroTest extends HibernateUtils{
/**
*
* 1.<id name=" pid" column="pid"
type="java.lang.Long">
<generator class="increment"> </generator>
</id>
* increament形式产生的 sql语句
* Hibernate:
select
max( pid)
from
person
Hibernate:
insert
into
person
( pname, psex , pid)
values
(?, ?, ?)
说明:
1、先得到主键的最大值
2、在最大值的基础上加1
3、上述的两步过程是由 hibernate内部完成的
*/
@Test
public void testIncrement(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
//person.setPid(11L);为increament时,自行设置的id没有用, hibernate会帮我们处理id;
person.setPname( "云三班长" );
person.setPsex( "不详");
session.save(person);
transaction.commit();
session.close();
}
/**
*
* 2.<generator class="identity"> </generator>
*
* Hibernate:
insert
into
person
( pname, psex )
values
(?, ?)
说明:
主键的生成交给数据库来做,数据库必须支持自动增长机制,会出现id不连续的情况,效率比increment要高一些;
*/
@Test
public void testIdentity(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
//person.setPid(11L);
person.setPname( "云三班长" );
person.setPsex( "不详");
session.save(person);
transaction.commit();
session.close();
}
/**
* 3.<generator class=" uuid"></generator>
* 主键的生成是由 hibernate内部完成的
*/
@Test
public void testUUID(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
person.setPname( "云三班长" );
person.setPsex( "不详");
session.save(person);
transaction.commit();
session.close();
}
/**
* 4.<generator class="assign"> </generator>
* 主键的生成由自己指定。
*/
@Test
public void testAssigned(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
person.setPid(11L);
person.setPname( "云三班长" );
person.setPsex( "不详");
session.save(person);
transaction.commit();
session.close();
}
}
|
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/damogu_arthur/article/details/46914109