您的位置:首页 > 其它

hibernate学习第一天——配置hibernate

2017-05-09 23:54 267 查看

hibernate学习第一天

开发步骤

新建工程

导入jar包

加入配置文件

写pojo类

1)属性,getter,setter,无参构造器,toString()

2)不要用final 修饰类

3)实现序列化接口

4)提供一个无意义的标识

加上注解

entity :持久化类

table:指定当前类对应数据库的表是哪个

id:配置自增长

配置文件

<!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>
<!-- 配置Hibernat的数据库方言,注意mysql5之后是MySQL5InnoDBDialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

<!-- 连接数据库的参数 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>

<!-- 配置DataSource -->
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">200</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">2</property>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 每隔3000秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 每次是否验证连接是否可用 -->
<property name="hibernate.c3p0.validate">false</property>

<!-- sql语句显示在控制台 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>

<!--
create:每次都会重新创建表
update:如果有表,就不管;如果没表,就创建 ,如果有更新,会更新
create-drop:每次都会重新创建表,之后删除,测试
-->
<property name="hibernate.hbm2ddl.auto">create</property>

<!-- 告诉Hibernate自定义的映射文件
如果是xml文件:rosource
如果是类文件:class
-->
<mapping class="org.fkjava.pojo.User"/>

</session-factory>
</hibernate-configuration>


开发步骤

读取配置文件

创建sessionFactory

创建session

开启事务

保存数据

提交事务

关闭各种资源 session关闭,sessionFactory关闭

// 1.读取配置文件
Configuration config = new Configuration().configure();
// 2.获取SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory(
new StandardServiceRegistryBuilder().applySettings(
config.getProperties()).build());

// 3.获取sesion
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction tc = session.beginTransaction();
// 5.操作

//修改数据
saveBook(session);

// 6.关闭事务
tc.commit();
// 7.关闭连接
session.close();
sessionFactory.close()



4000
置hibernate的其他方式

在src/添加hibernate.properties

1.要注意打开生成表策略 hibernate.hbm2ddl.auto update ,然后打开c3p0,mysql的配置

测试类.addAnnotatedClass(Book.class);//不支持自动加载持久化类,如果有多少个持久类,就要多次addAnnotatedClass

2.在代码中直接配置

Configuration config = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url", "jdbc:mysql://127.0.0.1:3306/hibernate")
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "root")
.setProperty("hibernate.c3p0.max_size", "20")
.setProperty("hibernate.c3p0.min_size", "2")
.setProperty("hibernate.c3p0.timeout", "5000")
.setProperty("hibernate.c3p0.max_statements", "100")
.setProperty("hibernate.c3p0.idle_test_period", "3000")
.setProperty("hibernate.c3p0.acquire_increment", "2")
.setProperty("hibernate.c3p0.validate", "false")
.setProperty("hibernate.hbm2ddl.auto", "update")
.setProperty("hibernate.show_sql", "true")
.setProperty("hibernate.format_sql", "true")
.addAnnotatedClass(Book.class);//不支持自动加载持久化类,如果有多少个持久类,就要多次addAnnotatedClass
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate