这是我第一次写博客,作为一直萌新先谈谈我的感想,之前接触博客的时候感觉没必要自己写出来,后来学了的东西有时候忘的差不多,然后自己在找的时候又下了不少功夫挺浪费时间,最主要的是之前不太会操作,有点嫌麻烦qaq,emmmmm。这里记录我的编程学习历程,以供以后参阅。
hibernate是一个持久层的框架技术,简单来说就是做了对象和关系的自动转换现在常用的持久层技术:
(1)通过会话bean和实体bean来实现数据持久化(EJB)
业务层---会话bean---实体bean--数据库
优点:稳定
缺点:重量级,内存消耗很大
(2)通过jdbc和dao来实现数据持久化(之前用的)
业务层---dao---jdbc--数据库
优点:内存消耗最小
缺点:简单,重复,耗时,各个数据库sql不太一样
(3)通过dao和orm组件来实现数据持久化(hibernate)
业务层---dao--orm--数据库
优点:内存消耗一般
缺点:还要学习该框架
1,搭建hibernate框架
(1)添加jar
暂时用的是hibernate3.jar自行选择
slf4j-api-1.6.1.jar(是接口标准)
slf4j-nop-1.6.1.jar(实现包)
(2)src下添加配置文件hibernate.cfg.xml,个人设置自行修改(资料hibernate-distribution-3.6.10.Final\project\etc下有模版)
<!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>
<!-- 基本连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hhh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库连接池配置 -->
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- 数据库方言,hibernate-distribution-3.6.10.Final\project\etc\下的hibernate.properties文件中有 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否需要根据配置自动生成数据表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 是否显示发送的sql语句 -->
<property name="show_sql">true</property>
<!-- 是否sql格式化后输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 需要hibernate管理的配置文件 -->
<mapping resource="entity/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(3)实体类映射(例)
public class Customer {
private int id;
private String name;
private String info;
private Date createTime;
}
(4)提供实体类映射文件,建议与类同名,如Customer.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>
<!--
name,类的位置,table,对应的数据库表
-->
<class name="entity.Customer" table="t_Customer">
<!-- 主键,对象/表的id配置,很重要
mysql---increment
oracle---sequence
mssqlserver---identity
uuid
select,根据查询生成主键
-->
<id name="id">
<!-- 主键声称策略 -->
<generator class="increment"/>
</id>
<!-- 其他属性
column,重定义列名,默认与属性名一致
type,更改列类型,建议默认
length,字符串长度,建议默认
-->
<property name="name"/>
<property name="info"/>
<property name="createTime"/>
</class>
</hibernate-mapping>
(5)将该实体类交给hibernate管理,在hibernate.cfg.xml中添加
<mapping resource="entity/Customer.hbm.xml"/>
(6)测试
public class ExportDB {
public static void main(String[] args) {
//读取配置文件,configure()该方法默认读取src下名为hibernate.cfg.xml
Configuration cfg=new Configuration().configure();
//将类导成表
SchemaExport se=new SchemaExport(cfg);
//参数,显示一些脚本等信息,true即可
se.create(true, true);
}
}
这样就可以把相应的表自动导入数据库了,虽然配置有点麻烦,但是一劳永逸。