基于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);
    }

}

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

々上善若水々 2007-11-29 09:08 发表评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值