數(shù)據(jù)庫(kù)用的是mysql5.0;
腳本如下:
use test;
create table person
(
id int AUTO_INCREMENT primary key,
username varchar(20),
password varchar(20)
);

insert into person values(null,'ts','ts');

實(shí)體類(lèi)用Annotation映射,代替hbm.
package com.vo;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@SuppressWarnings(
{ "unchecked", "serial" })
@Entity //標(biāo)識(shí)是一個(gè)實(shí)體
@Table(name="person") //映射表
public class Person implements Serializable


{
//主鍵映射
@Id
//主鍵自增
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
//@Column(name="username"),對(duì)于列,可映射也可以不映射.注意保持列名和屬性名一致就行
private String username;
private String password;

public Integer getId()

{
return id;
}

public void setId(Integer id)

{
this.id = id;
}

public String getUsername()

{
return username;
}

public void setUsername(String username)

{
this.username = username;
}

public String getPassword()

{
return password;
}

public void setPassword(String password)

{
this.password = password;
}
}

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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="show_sql">true</property>
<!-- 實(shí)體類(lèi)映射 -->
<mapping class="com.vo.Person"/>
</session-factory>
</hibernate-configuration>
測(cè)試類(lèi):
package com.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import com.vo.Person;

public class PersonTest


{
private Session session;
private Transaction tx;

@Before
public void before()

{
session = new AnnotationConfiguration().configure()
.buildSessionFactory().openSession();
tx = session.getTransaction();
}

@After
public void after()

{
session.close();
}

@Test
public void testSave()

{
tx.begin();
Person person = new Person();
person.setUsername("zdw");
person.setPassword("admin");
session.save(person);
tx.commit();
}
@Test
public void testUpdate()

{
tx.begin();
Person person = (Person) session.load(Person.class, 1);
person.setPassword("test");
session.update(person);
tx.commit();
}
@SuppressWarnings("unchecked")
@Test
public void testQueryAll()

{
List<Person> persons = session.createCriteria(Person.class).list();
assertNotNull(persons);
}
@Test
public void testDelete()

{
Person person = (Person) session.load(Person.class, 1);
session.delete(person);
}
}

經(jīng)測(cè)試,增刪改查全部正常.
這樣的確很方便了.
源碼可以在我的網(wǎng)盤(pán)下載.
點(diǎn)此下載