hibernate实现有两种配置xml配置与注释配置.docx
- 文档编号:10754640
- 上传时间:2023-02-22
- 格式:DOCX
- 页数:7
- 大小:16.83KB
hibernate实现有两种配置xml配置与注释配置.docx
《hibernate实现有两种配置xml配置与注释配置.docx》由会员分享,可在线阅读,更多相关《hibernate实现有两种配置xml配置与注释配置.docx(7页珍藏版)》请在冰豆网上搜索。
hibernate实现有两种配置xml配置与注释配置
Hibernate实现有两种配置,xml配置与注释配置
hibernate实现有两种配置,xml配置与注释配置。
(1):
xml配置:
hibernate.cfg.xml (放到src目录下)和实体配置类:
xxx.hbm.xml(与实体为同一目录中)
xml version='1.0' encoding='utf-8'?
>
DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http:
//www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
--Databaseconnectionsettings-->
com.mysql.jdbc.Driver
jdbc:
mysql:
//localhost:
3306/hxj
--JDBCconnectionpool(usethebuilt-in)-->
--SQLdialect-->
org.hibernate.dialect.MySQLDialect
--EnableHibernate'sautomaticsessioncontextmanagement-->
--Disablethesecond-levelcache -->
org.hibernate.cache.NoCacheProvider
--EchoallexecutedSQLtostdout-->
--Dropandre-createthedatabaseschemaonstartup-->
—update也可以用create/create-drop/update/validate代替,create 表示可以根据实体配置文件来自动生成表(只能生成表).
-->
// 实体配置类
(2):
实体配置类:
xxx.hbm.xml
xml version="1.0"?
>
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/HibernateMappingDTD3.0//EN"
"
(3):
测试类(包括获取SessionFactory类和实体测试类)
SessionFactory类:
HibernateUtil
public class HibernateUtil{
private static final SessionFactory sessionFactory;
static {
try {
//CreatetheSessionFactoryfromhibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwableex){
//Makesureyoulogtheexception,asitmightbeswallowed
System.err.println("InitialSessionFactorycreationfailed." +ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactorygetSessionFactory(){
return sessionFactory;
}
}
实体测试类:
PersonManager
-----------------------------------------------------------------------------------
public class PersonManager{
public static void main(String[]args){
createAndStorePerson();
HibernateUtil.getSessionFactory().close();
}
private static void createAndStorePerson(){
Sessionsession= // 通过Session工厂获取Session对象
HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction(); //开始事务
Personperson= new Person();
person.setUsername("何小景");
person.setAge(26);
session.save(person);
session.getTransaction().commit(); // 提交事务
}
}
(4):
注解方式:
注解的方式与xml很很多类似:
首先是需要加入4个jar包:
hibernate-commons-annotations.jar 、 hibernate-annotations.jar
ejb3-persistence.jar 、 hibernate-jpa-2.0-api-1.0.1.Final.jar
下面是不同的地方:
(1):
hibernate.hbm.xml 文件中把引用:
xxx.hbm.xml改为引用实体类:
即把:
改为:
(2):
获取SessionFactory方式发生了变化:
即:
由SessionFactorysf=newConfiguration().configure().buildSessionFactory()
改为:
SessionFactorysf=newAnnotationConfiguration().configure().buildSessionFactory()
(3):
注解方式不需要在xxx.hbm.xml把实体类与表进行映射。
而采用在实体类中进行注解。
注意:
(1):
如果实体类属性名与表字段名不一致的时候,要么都注解在属性前,要么都注解在get方法前。
不能部分注解在属性前,部分注解在方法前。
(2):
如果实体类属性名与表字段名一致的时候,可以部分注解在属性前,部分注解在方法前。
(3):
如果在实体类中某些属性不注解:
(属性和get都不写注解),默认为表字段名与实体类属性名一致。
(4):
如果实体类的某个成员属性不需要存入数据库中,使用@Transient 进行注解就可以了。
即类似于:
(xxx.hbm.Xml配置中的某些字段不写(就是不需要对这个成员属性进行映射))
(5):
表名称可以在实体类前进行注解。
(6):
所有这些注解在:
javax.persistence包下。
而不是在hibernate包中。
---------------------------------------------------------------------------------------------------------------------
@Entity // 表示为实体类
@Table(name="t_teacher") // 表名注解
public class Teacher implements Serializable{
private int id;
private String username;
private int age;
@Id // 表示主键
@GenericGenerator(name= "generator",strategy= "increment") @GeneratedValue(generator= "generator") // 自增长
@Column(name= "id") // 类属性对应着表字段
public int getId(){
return id;
}
public void setId(int id){
this.id =id;
}
@Column(name="t_username") // 类属性对应着表字段
public StringgetUsername(){
return username;
}
public void setUsername(Stringusername){
this.username =username;
}
@Column(name="t_age") // 在实体类属性进行注解,类属性对应着表字段
public int getAge(){
return age;
}
public void setAge(int age){
this.age =age;
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- hibernate 实现 有两种 配置 xml 注释
![提示](https://static.bdocx.com/images/bang_tan.gif)