`
laodaobazi
  • 浏览: 272855 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate 使用 Annotation 1(测试)

阅读更多

Hello World 程序

 

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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">100</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
		
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
		
        <!-- Drop and re-create the database schema on startup -->
        <!-- <property name="hbm2ddl.auto">create</property>-->
              <mapping class="com.jlee01.helloworld.Login"/>
    </session-factory>

</hibernate-configuration>

 

Login.java 代码:

package com.jlee01.helloworld;

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

@Entity
@Table(name="T_Login")
public class Login {

	private long id ;
	private String username ;
	private String password ;
	private String address ;
	private String email ;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}
	
	@Column(name="USERNAME" , length=60)
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(name="PASSWORD" , length=60)
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(name="ADDRESS" , length=120)
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	@Column(name="EMAIL" , length=30)
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
}

 

 

LoginTest.java 代码:

package com.jlee01.helloworld;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public class LoginTest extends TestCase {
	
	@BeforeClass
	public void begin(){
		
	}
	
	@AfterClass
	public void end(){
		
	}
	
	public void testLogin(){
		AnnotationConfiguration acfg = new AnnotationConfiguration() ;
		SessionFactory sf = acfg.configure().buildSessionFactory() ;
		Session session = sf.getCurrentSession() ;
		session.beginTransaction() ;
		
		Login login = new Login() ;
		login.setUsername("JLee") ;
		login.setPassword("198791") ;
		login.setAddress("北京") ;
		login.setEmail("444823046@qq.com") ;

		session.save(login) ;
		
		session.getTransaction().commit() ;
		
	}
	
	public void testExport(){
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true) ;
	}
	
}

 

分享到:
评论

相关推荐

    hibernate-annotation 所需要的jar包

    本人初学hibernate,这是所需要的hibernate结合Annotation所需要的基本jar包,希望可以帮助大家,已经成功测试

    hibernate运行需要的包(包括测试和annotation和mysql驱动)

    这个是我自己使用的hibernate练习的包,包括hibernate需要的包,annotation的包,log4j,junit4,mysql驱动·!直接引入就可以用了

    使用Annotation并对DAO层封装具有分页功能的S2SH整合实例_好资源0分送

    现在Annotation越来越流行,最近一段时间也学了一些,EJB3.0、Hibernate、Spring等都很好地支持Annotation,而且熟悉了Annotation的话整个项目开发时间会缩短,代码封装比较好,但是封装带来的就是代码阅读就比较...

    OSGi、Spring、Hibernate集成:动态注册实体类

    平台依赖度低的解决方案 ...测试环境:equinox3.5.2。其它用到的Bundle包括hibernate、hibernate-annotation、hsqldb、 spring、spring-osgi等。请到http://www.springsource.com/repository/下载。

    hibernate笔记

    1 下载hibernate-distribution-3.3.2.GA-dist 5 2 下载hibernate-annotations-3[1].4.0.GA 5 3 注意阅读hibernate compatibility matrix(hibernate 网站download) 5 4 下载slf4jl.5.8 6 Hibernate HelloWorld 6 1 ...

    hibernate2

    Hibernate Annotation简单应用 接着上次的程序我们将其改为Annotation版本的 既然用Annotation那就要将其对应的包导入到工程 然后我们将entity包中的Student.hbm.xml文件删除,将hibernate.cfg.xml文件中的 改为...

    Hibernate+中文文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    hibernate3.2中文文档(chm格式)

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    HibernateAPI中文版.chm

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    基于注解Annotation的最新版SSH(Struts2.3.7+Hibernate4.1.9+Spring3.2.0)框架整合开发

    基于注解Annotation的最新版SSH(Struts2.3.7+Hibernate4.1.9+Spring3.2.0)整合开发,真正实现零配置。 最新版本Struts、Spring、Hibernate框架整合: struts-2.3.7 spring-framework-3.2.0.RELEASE hibernate-...

    基于Java的Hibernate MySQL数据持久化示例设计源码

    该示例包含了基于XML和Annotation的Hibernate配置,涵盖了one-to-one、one-to-many、many-to-many映射关系的配置,并提供了独立的测试用例。适合用于学习和实践Java和Hibernate技术,以及开发基于MySQL的数据持久化...

    Hibernate中文详细学习文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    Spring和Hibernate学习笔记

    第二课:测试实体对象的生命周期 第三课:hibernate基本映射 第四课:hibernate多对一关联映射 ...................... Spring: 第一课:构建Spring的环境并建立一个例子 第三课:spring Bean的作用域 第四课:...

    Hibernate 中文 html 帮助文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    hibernate 体系结构与配置 参考文档(html)

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    springMVC+Hibernate3整合实例

    里面整合的是springMVC+hibernate3的配置文件和实例,用的是mysql数据库,使用注解annotation,下载后,只需修改jdbc.properties配置文件和加载对应的数据库驱动jar包就可以运行。亲测通过,有问题可以交流。

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    基于spring,struts(struts2),hibernate的web项目脚手架

    内置一个基于数据库的代码生成器rapid-generator,极易进行二次开发 struts1,struts2的零配置 spring集成及加强,自动搜索hibernate的entity annotation class 集成动态构造sql的工具:rapid-xsqlbuilder 集成...

    模拟hibernate注解功能

    通过自定义注解,反射等技术实现hibernate注解功能。单元测试可以直接跑,代码里的注释写的很清楚,欢迎下载参阅!

    JQuery1.4.2+Struts2.1.8+JSON0.34+Spring2.5.6+Hibernate3.5+XFire1.2.6整合实例

    1、JSON0.34使用的是struts2附带的struts2-json-plugin-2.1.8.1.jar 2、db是mysql,名字为test,用户名root,密码空 3、com.morningstar.utility.WSUtility为调用WebService的客户端代码,在调用这个WebService返回...

Global site tag (gtag.js) - Google Analytics