hibernate annotation 实现 xml配置文件实现


hibernate 是现在比较流行的一个O/R框架主要用于实现有object到relation(对象到关系模型),数据的持久化工作。

1,构建hibernate的运行环境,

需要的jar包

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

<session-factory>    //创建session工厂(hibernate中的session就像和数据库之间的链接)

<!-- Database connection settings -->

<property name="connection.driver_class">  //数据库驱动类

com.mysql.jdbc.Driver

</property>

<property name="connection.url">

jdbc:mysql://localhost/hibernate   //连接数据库的url

</property>

<property name="connection.username">root</property>   //数据库的用户名

<property name="connection.password">123456</property>  //数据库密码

 

<!-- JDBC connection pool (use the built-in) -->

<!--  <property name="connection.pool_size">1</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>   //格式化sql语句

<!-- Drop and re-create the database schema on startup -->

<property name="hbm2ddl.auto">create</property>  //hbm2ddl数据定义语言的配置

<mapping resource="com/msb/hibernate/model/Student.hbm.xml" />   //基于.hmb.xml文件的mapping

<mapping class="com.msb.hibernate.model.Teacher" />  //基于annotation的mapping

</session-factory>

</hibernate-configuration>


简单的.hbm.xml文件的格式

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.msb.hibernate.model">

<class name="Student" table="student"> //和数据库表明对应

<id name="" column=""></id>   //和数据库的主键对应

<property name="age"></property>  //和数据库的普通字段对应

</class>

</hibernate-mapping>

基于.hbm.xml配置文件的hibernate简单实现

要持久化的类

package com.msb.hibernate.model;

public class Student

{

private int id;

private String name;

private int age;

 

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

public void setId(int id)

{

this.id=id;

}

public int getId()

{

return id;

}

public String getName()

{

return name;

}

public void setName()

{

this.name=name;

}

}

测试类;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import com.msb.hibernate.model.Student;

public class StuentTest

{

public static void main(String[] args)

{

Student st = new Student();

st.setAge(23);

st.setId(1);

st.setName("xiaoliu");

 

Configuration cf = new Configuration().configure()   //获取加载hibernate.cfg.xml配置文件

 

SessionFactory sf = cf.buildSessionFactory();   //根据配置文件建立对应的SessionFactory工厂

Session session = sf.openSession();            //获得一个session(就相当于和数据库取的一个连接)

session.beginTransaction();            //session做什么事之前都要先开始事务

session.save(st);                     //该方法执行时会读取.hbm.xml映射配置文件,然后根据该文件将对象持久化到数据库

session.getTransaction().commit();   // 获得事务然后提交事务

session.close();   //关闭session,关闭sessionfactory

}

}

基于annotation的hibernate执行流程

被持久化的类

 package com.msb.hibernate.model;

import javax.persistence.Entity;

import javax.persistence.Id;

@Entity

public class Teacher

{

private int id;

private String name;

private int age;

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

@Id

public int getId()

{

return id;

}

public void setId(int id)

{

this.id = id;

}

public String getName()

{

return name;

}

 public void setName(String name)

{

this.name = name;

}

}


测试类

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

import com.msb.hibernate.model.Teacher;

import com.msb.hibernate.model.TeacherPK;

 

public class TeacherTest

{

@SuppressWarnings("deprecation")

public static void main(String[] args)

{

 

Teacher teacher = new Teacher();

teacher.setId(2);

teacher.setName("zhangsan");

teacher.setAge(23);

SessionFactory sf = new Configuration().configure()

.buildSessionFactory();

Session session = sf.openSession();

session.beginTransaction();

session.save(teacher);

session.getTransaction().commit();

session.close();

sf.close();

}

}

 

看见annotation 实现hibernate比xml配置文件实现方便多了,以后建议使用annotation实现


1,如果类名和表名不同,

解决方法: @Table(name="_xxxx")xxxx为数据库中的表名,指定数据库中的表名

2,字段命不同

解决方法:  @Column(name="xxx")xxx为数据库中的字段名,指定数据库中字段名

3,不想让某个字段持久化:

在该字段的get方法上加上  @Transient

4,字段是时间和日期类型

@Temporal(TemproalTepe.xx)  xx用于指定把日期格式化

5,字段是枚举类型

如果在xml文件中解决会非常麻烦,而用annotation解决非常方便

在字段的get方法上面加上注解   @Enumerated(EnumType.xx)  xx指定枚举中对象的类型

6,字段如果不加注解,则默认加上了 @Basic  表示是在数据库中是普通字段   

注解应该放在get方法上面,一般不要放在字段上面。

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值