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.properties1.要注意打开生成表策略 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学习第一天 配置环境和helloworld
- Hibernate学习手记——SQL server MiddleGen配置问题
- Java学习之hibernate配置要点详谈
- {传智播客} (学习笔记)--hibernate的使用和配置
- Hibernate学习文档_02_一些配置选项和接口
- Hibernate学习笔记 3 - 关系映射配置(基于注解)
- 学习hibernate日记(第一天)
- Android 下arcgis开发学习第一天——环境配置及常见错误
- JSF2 学习日志,第一天,环境配置&hello world
- Hibernate 学习第一天
- Hibernate学习笔记之EHCache的配置
- Hibernate 3学习笔记 - 简单的Hibernate配置文件
- SSH学习笔记之关于struts-spring-hibernate整合配置问
- 传智播客hibernate学习,开发环境配置
- Hibernate学习一:配置及简单实例的创建过程
- Struts+hibernate学习第一天
- hibernate3 学习笔记(三) hibernate 日志配置文件 log4j.properties
- Hibernate学习小结(一)――Hibernate配置
- Hibernate学习笔记之EHCache的配置
- Hibernate学习之hibernate.cfg.xml配置