hibernate实现有两种配置,xml配置与注释配置。<转>
《注意:在配置时hibernate的下载的版本一定确保正确,因为不同版本导入的jar包可能不一样,所以会导致出现一些错误》
hibernate实现有两种配置,xml配置与注释配置。
(1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.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>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hxj
</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">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>
<!-- Drop and re-create the database schema on startup -->
<!—update也可以用create/create-drop/update/validate代替, create 表示可以根据实体配置文件来自动生成表(只能生成表).
-->
<property name="hbm2ddl.auto">update</property>
// 实体配置类
<mapping resource="com/wsw/struts/model/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(2): 实体配置类:xxx.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package=”com.wsw.struts.model”>
<class name="Person" table="per">
<id name="id" column="id">
<generator class="native"/> //字段自增
</id>
<property name="username" column="p_username"/>
<property name="age" column="p_age"/>
</class>
</hibernate-mapping>
(3):测试类(包括获取SessionFactory类和实体测试类)
SessionFactory类:HibernateUtil
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
实体测试类:PersonManager
-----------------------------------------------------------------------------------
public class PersonManager {
public static void main(String[] args) {
createAndStorePerson();
HibernateUtil.getSessionFactory().close();
}
private static void createAndStorePerson() {
Session session = // 通过Session工厂获取Session对象
HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction(); //开始事务
Person person = 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改为引用实体类:
即把:<mapping resource="com/wsw/hibernate/model/Person.hbm.xml"/>
改为:<mapping class="com.wsw.hibernate.model.Teacher" />
(2):获取SessionFactory方式发生了变化:
即:由SessionFactory sf = new Configuration().configure().buildSessionFactory()
改为:SessionFactory sf = new AnnotationConfiguration().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 String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="t_age") // 在实体类属性进行注解,类属性对应着表字段
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
《转自:http://www.blogjava.net/yxhxj2006/archive/2012/06/30/381861.html》
hibernate实现有两种配置,xml配置与注释配置。<转>的更多相关文章
- Hibernate实现有两种配置,xml配置与注释配置
hibernate实现有两种配置,xml配置与注释配置. (1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) < ...
- 【JPA】两种不同的实现jpa的配置方法
两种不同的实现jpa的配置方法 第一种: com.mchange.v2.c3p0.ComboPooledDataSource datasource.connection.driver_class=co ...
- Django---Http协议简述和原理,HTTP请求码,HTTP请求格式和响应格式(重点),Django的安装与使用,Django项目的创建和运行(cmd和pycharm两种模式),Django的基础文件配置,Web框架的本质,服务器程序和应用程序(wsgiref服务端模块,jinja2模板渲染模块)的使用
Django---Http协议简述和原理,HTTP请求码,HTTP请求格式和响应格式(重点),Django的安装与使用,Django项目的创建和运行(cmd和pycharm两种模式),Django的基 ...
- Hibernate多对多两种情况
Hibernate在做多对多映射的时候,除了原先的两张表外,会多出一个中间表做关联,根据中间表的会有两种不同的配置情况: 1.中间表不需要加入额外数据. 2.中间表有其他字段,需记录额外数据. 下面, ...
- Spring中IoC - 两种ApplicationContext加载Bean的配置
说明:Spring IoC其实就是在Service的实现中定义了一些以来的策略类,这些策略类不是通过 初始化.Setter.工厂方法来确定的.而是通过一个叫做上下文的(ApplicationConte ...
- spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法
spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...
- gitlab两种连接方式:ssh和http配置介绍
gitlab环境部署好后,创建project工程,在本地或远程下载gitlab代码,有两种方式:ssh和http (1)ssh方式:这是一种相对安全的方式 这要求将本地的公钥上传到gitlab中,如下 ...
- JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理
java发送http get请求,有两种方式. 第一种用URLConnection: public static String get(String url) throws IOException { ...
- gitlab两种连接方式:ssh和http配置介绍 --转自 散尽浮华
gitlab环境部署好后,创建project工程,在本地或远程下载gitlab代码,有两种方式:ssh和http 1)ssh方式:这是一种相对安全的方式 这要求将本地的公钥上传到gitlab中,如下图 ...
随机推荐
- 数据库——SQL中EXISTS怎么用3(转)
有一个查询如下: 1 SELECT c.CustomerId, CompanyName 2 FROM Customers c 3 WHERE EXISTS( 4 SELECT Or ...
- zookeeper连接 org.apache.curator.framework.imps.CuratorFrameworkImpl Background exception was not retry-able or retry gave up [main-EventThread]
ERROR org.apache.curator.framework.imps.CuratorFrameworkImpl Background exception was not retry-able ...
- [转]RosBridge小结
1.rosbridge介绍 rosbridge(rosbridge_suite)是ros官方为开发者提供的一个用于非ros系统和ros系统进行交互通信的功能包.rosbridge主要包含两个部分,Ro ...
- 关于那些常见的坑爹的小bug(会持续更新)
当我学了矩阵分析的时候我知道什么是麻烦,当我学了傅里叶级数的时候我知道什么是相当麻烦. 然而,当我刚刚接触前端,我才明确什么叫做坑爹的ie6.这个分享对于经验丰富的前端基本都遇过.对于刚入行的新手,也 ...
- PHP不能不看的50个细节!
1. 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的”函数”(译注:PHP ...
- Qt 反射
简介 本文主要讲解Qt是如何实现反射,以及一点点反射使用的小心得. 文章概览 Qt反射内幕小窥 详细内容 反射前期准备 得到注册的类成员变量 得到注册的类成员函数 访问类成员属性(get,set) 调 ...
- USB2.0学习笔记连载(二):USB基础知识简介
USB接口分为USB A型.USB B型.USBmini型.USBmicro型.USB3.0其中每种都有相应的插座和插头. 图1 图2 上图是USBA型接口,图1为插座,图2为插头.插座指向下行方向, ...
- C struct的内存对齐
说明:如果你看到了这篇,请略过. struct是复合类型. 其中的成员在内存中的分布都是对齐的. 这个对齐的意思是,struct的sizeof运算结果必定是其最大类型长度的整数倍. --注意,如果st ...
- mysql中,now()函数和sysdate()函数有什么区别?
问题描述: 今天在看mysql的时间函数,now()和sysdate(),记录下两者之间有什么不同. 实验过程: 1.执行以下的两个语句: mysql),now(); +--------------- ...
- 高级类特性----static关键字
static 关键字 当我们编写一个类时,其实就是在描述其对象的属性和行为,而并没有产生实质上的对象,只有通过new关键字才会产生出对象,这时系统才会分配内存空间给对象,其方法才可以供外部调用. 我们 ...