基于JPA的Hibernate->CRUD(簡單應用)(原創)
數據庫用的是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');
實體類用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 //標識是一個實體
@Table(name="person") //映射表
public class Person implements Serializable
{
//主鍵映射
@Id
//主鍵自增
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
//@Column(name="username"),對于列,可映射也可以不映射.注意保持列名和屬性名一致就行
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>
<!-- 實體類映射 -->
<mapping class="com.vo.Person"/>
</session-factory>
</hibernate-configuration>
測試類:
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);
}
}
經測試,增刪改查全部正常.
這樣的確很方便了.
源碼可以在我的網盤下載. 點此下載
腳本如下:

































































hibernate.cfg.xml配置文件:




















































































這樣的確很方便了.
源碼可以在我的網盤下載. 點此下載
posted on 2007-11-29 09:08 々上善若水々 閱讀(1893) 評論(0) 編輯 收藏 所屬分類: Hibernate