Hibernate配置
来源:互联网 发布:rmvb格式软件 编辑:程序博客网 时间:2024/06/06 08:36
Hibernate配置
1、JDBC的优点
直接底层操作,提供了很简单、便捷的访问数据库的方法,跨平台性比较强。灵活性比较强,可以写很复杂的SQL语句。
2、JDBC的缺点
因为JAVA是面向对象的,JDBC没有做到使数据能够面向对象的编程,使程序员的思考仍停留在SQL语句上。
操作比较繁琐,很多代码需要重复写很多次。
如果遇到批量操作,频繁与数据库进行交互,容易造成效率的下降。
3、配置文件hibernate.cfg.xml和hibernate.properties,XML和properties两种,这两个文件的作用一样,提供一个即可,推荐XML格式,下载目录/etc下是示例配置文件。
可以在配置文件指定:
数据库的URL、用户名、密码、JDBC驱动类、方言等。
启动时Hibernate会在CLASSPATH里找这个配置文件。
4.、对象模型与关系模型阻抗不匹配之处。
Java面向对象语言,对象模型,其主要概念有:继承、关联、多态等;数据库是关系模型,其主要概念有:表、主键、外键等。对象模型当中特有的“继承”,“组合”,“聚合”,“依赖”的概念在关系模型当中是不存在的。
5、orm的作用:把关系型数据库映射成对象模型
6、 Eg、配置:
(1)User.java
public class User {
private int id;
private String name;
private Date birthday;
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date date) {
this.birthday = date;
}
}
(2)User.hbsi.xml
<hibernate-mapping
package="com.hbsi.domain">
<class name="User" table="user">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
<property name="birthday"/>
</class>
</hibernate-mapping>
(3)hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///demo</property>
<property name="connection.username">root</property>
<property name="connection.password">32956</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com/hbsi/domain/User.hbsi.xml"/>
</session-factory>
(4)测试
public class TestHibernate {
//使用hibernate完成将对象存入表中
public static void main(String[] args){
Configuration cfg=new Configuration();
cfg.configure();//完成hibhernate的初始化--读取配置文件
//sessionFactory对象---DriverManager
SessionFactory sf=cfg.buildSessionFactory();
//得Session对象
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
User user =new User();
user.setName("Tom");
user.setBirthday(new Date());
s.save(user);
s.close();
System.out.println("end");
}
}
- hibernate-hibernate的配置
- 【Hibernate】2.Hibernate配置
- Hibernate 配置!
- Hibernate配置
- Hibernate配置
- Hibernate 配置
- Hibernate配置
- Hibernate配置
- Hibernate配置
- Hibernate配置
- hibernate 配置
- Hibernate 配置
- Hibernate 配置
- hibernate配置
- 配置HIbernate
- Hibernate 配置
- Hibernate配置
- Hibernate配置
- android 图片解码 质量问题
- Android开发入门之--Eclipse/Android配置
- teset
- 百度UEditor使用详解
- goto的限制
- Hibernate配置
- Bitmap 与 Drawable 之间的转换
- Hibernate 3.2.5 annotation介绍及其简单例子
- android Tasks and Back Stack
- 软件工程概述
- log4j在weblogic中配置
- A1600各个版本经典刷机包汇集一起
- linux解决中文乱码(小方框)
- Java正则表达式