Hibernate使用注解配置持久化类

接上篇文章(点击打开链接),给出Hibernate注解配置持久化类的步骤:

一、同样配置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 >  
        <!-- mysql数据库驱动 -->  
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
        <!-- mysql数据库名称 -->  
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/world</property>  
        <!-- 数据库的登陆用户名 -->  
        <property name="hibernate.connection.username">root</property>  
        <!-- 数据库的登陆密码 -->  
        <property name="hibernate.connection.password">1988419</property>  
        <!-- 配置方言为每一种数据库提供特殊sql适配-->  
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  

		<!-- c3p0连接池配置 -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!-- 连接池最大连接 -->
		<property name="hibernate.c3p0.max_size">10</property>
		<!-- 连接池最小连接 -->
		<property name="hibernate.c3p0.min_size">1</property>
        <!-- 连接池超时时长(毫秒) -->  
        <property name="hibernate.c3p0.timeout">5000</property>
        <!-- 指定连接池最大缓存多少Statment对象 -->
        <property name="hibernate.c3p0.max_statment">50</property>
        <!-- 空闲检测周期(毫秒) -->
        <property name="hibernate.c3p0.idle_test_period">3000</property>
        <!-- 连接池一次增加的连接数 -->
        <property name="hibernate.c3p0.acquire_increment">5</property>
        <!-- 每次都验证连接是否可用 -->
        <property name="hibernate.c3p0.validate">true</property>
        
        <!-- 是否在控制台输出sql语句 -->
        <property name="hibernate.show_sql">true</property>
        
        <!-- 使用xml配置时,指定持久化类xml的路径 -->
<!--         <mapping resource="org/lcwben/Student.hbm.xml"></mapping> -->
        
        <!-- 使用注解持久化类配置时,指定持久化类.java文件配置 -->
			<mapping class="org.lcwben.Student" />
    </session-factory>  
</hibernate-configuration>  


二、建立持久化类:

import java.util.Date;

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

import org.hibernate.annotations.GenericGenerator;

//注意:所有的注解均使用jpa规范
@Entity
@Table(name="test_stu")
public class Student {

	@Id //uid作为主键
	@GenericGenerator(name = "generator", strategy = "increment") //自增选项
	@GeneratedValue(generator="generator")
	private Integer uid;
	private String uname;
	private Date regtime;
	private Integer status;
	private Integer score;
	private Integer classes;
	
	
//所有成员变量的getter与setter
	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public Date getRegtime() {
		return regtime;
	}
	public void setRegtime(Date regtime) {
		this.regtime = regtime;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public Integer getScore() {
		return score;
	}
	public void setScore(Integer score) {
		this.score = score;
	}
	public Integer getClasses() {
		return classes;
	}
	public void setClasses(Integer classes) {
		this.classes = classes;
	}
	
}

三、JUnit测试:

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;


public class Client {

	private static SessionFactory sf;
	
	static {
		Configuration cfg = new Configuration().configure();
		ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
		sf = cfg.buildSessionFactory(sr);
	}
	
	@Test
	public void testSelectAll() {
		
		Configuration cfg = new Configuration().configure();
		ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
		SessionFactory sf = cfg.buildSessionFactory(sr); 
		
		Session sess = null;
		try {
			sess = sf.openSession();
			String hql = "from Student";
			Query qy = sess.createQuery(hql);
			List<Student> stus = qy.list();
			for (Student stu : stus) {
				System.out.println(stu.getUid()+" "+stu.getUname()+" "+stu.getRegtime()+" "+stu.getStatus()+" "+stu.getScore()+" "+stu.getClasses());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(sess!=null) {
				sess.close();
			}
		}
	}
}

结果如下:

三月 08, 2017 3:05:22 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
三月 08, 2017 3:05:22 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select student0_.uid as uid1_0_, student0_.classes as classes2_0_, student0_.regtime as regtime3_0_, student0_.score as score4_0_, student0_.status as status5_0_, student0_.uname as uname6_0_ from test_stu student0_
1 lcwben1 2017-01-18 0 60 1
2 lcwben2 2017-01-18 1 67 1
3 lcwben3 2017-01-19 1 51 3
4 benhbm2 2017-01-19 0 78 3
5 lcwben5 2017-01-19 0 80 3
6 lcwben6 2017-01-22 1 97 2
7 lcwben7 2017-01-23 0 73 1
8 lcwben8 2017-01-25 1 48 2
9 lcwben9 2017-01-28 1 100 3
10 lcwben10 2017-01-31 0 92 3




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值