我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

基于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.class1);
        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.class1);
        session.delete(person);
    }

}

经测试,增删改查全部正常.
这样的确很方便了.
源码可以在我的网盘下载. 点此下载

posted on 2007-11-29 09:08 々上善若水々 阅读(1864) 评论(0)  编辑  收藏 所属分类: Hibernate


只有注册用户登录后才能发表评论。


网站导航: