Hibernate总结-----Hibernate简介,基本配置
来源:互联网 发布:redis同步到mysql 编辑:程序博客网 时间:2024/05/01 21:33
Hibernate简介:
Hibernate 是一个开放源代码的对象关系映射框架(ORM),对jdbc进行了非常轻量级的对象封装,将pojo与数据库表建立映射关系,是一个全自动的ORM框架,可以自动生成SQL语句,自动执行。
ORM对象关系映射:
创建一个HelloWord:
1、导入需要的jar包
1.1使用maven方式引入。。。。只需在网页上搜索maven仓库,搜索自己所需要的jar包,将代码复制进自己的工程下的pom.xml中
2、配置Hibernate.cfg.xml(配置数据库连接的相关参数)
2.1 <?xmlversion="1.0" encoding="utf-8"?> <!DOCTYPEhibernate-configuration PUBLIC
"-//Hibernate/HibernateConfiguration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
配置session-factory
<session-factory>:SessionFactory是Hibernate中的一个类,保存数据库实例相关的配置信息
<!-- 数据库连接配置 -->
<propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
<propertyname="connection.url">jdbc:mysql://ip:port/hibernate1506</property>
<propertyname="connection.username">username</property>
<propertyname="connection.password">password</property>
<!-- 数据库连接池配置 -->
<propertyname="connection.pool_size">1</property>
<!--数据库方言指定使用数据库,使Hibernate知道在底层如何拼接生成sql语句-->
<propertyname="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--根据类生成表的策略validate | update | create |create-dropcreate:每次加hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表。
create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory 一关闭,表就自动删除
upupdate:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。-->
<propertyname="hbm2ddl.auto">update</property>
<!--是否后台打印sql语句 -->
<propertyname="hibernate.show_sql" value="true" />
<!--引入映射配置文件 -->
<mappingresource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
3、配置*.hbm.xml(建立bean对象与table之间的映射关系{xml、Annotation})
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.huanxin.maven.hibernatebase"><class name="UserInfoBean" table="userInfo"><id name="uId" type="java.lang.Long"><generator class="increment"></generator></id><property name="username"></property><property name="password"></property></class></hibernate-mapping>
创建UserInfoBean 类
public class UserInfoBean implements Serializable {long uId;String username;String password;public long getuId() {return uId;}public void setuId(long uId) {this.uId = uId;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public UserInfoBean(String username, String password) {super();this.username = username;this.password = password;}public UserInfoBean() {super();// TODO Auto-generated constructor stub}@Overridepublic String toString() {return "UserInfoBean [uId=" + uId + ", username=" + username + ", password=" + password + "]";}
4、加载配置文件,创建SessionFactory
Configuration configuration = new Configuration();Configuration configure = configuration.configure();// SessionFactory sessionFactory=configure.buildSessionFactory();StandardServiceRegistryBuilder applySettings = new StandardServiceRegistryBuilder().applySettings(configure.getProperties());StandardServiceRegistry build = applySettings.build();SessionFactory sessionFactory = configure.buildSessionFactory(build);session = sessionFactory.openSession();
5、使用SessionFactory进行CRUD
5.1SessionSectory是一个重量级对象,不仅包含配置,还起到一个缓冲作用,缓冲了Hibernate自动生成的SQL语句、一些其他映射数据和将来有可能重复利用的数据
5.2Session:实例化的Session是一个轻量级的类,创建和销毁它都不会占用太多的资源,‘一个Session相当于打开一次数据库连接,Heribrnate中的CRUD操作都是由Session完成的
6、连接流程
7、SQL拼接
Annotation方式
@Entity:映射实体 @Id:主键 @GeneratedValue(strategy =GenerationType.AUTO):自动增长 @Column:列
0 0
- Hibernate总结-----Hibernate简介,基本配置
- hibernate简介及配置
- Hibernate 基本配置
- hibernate的基本配置
- Hibernate 的基本配置
- hibernate基本配置
- Spring+hibernate基本配置
- SpringMVC+Hibernate基本配置
- hibernate的基本配置
- Hibernate基本配置
- Hibernate基本配置
- hibernate 基本配置
- hibernate-1-基本配置
- hibernate基本配置
- Hibernate基本配置
- hibernate基本配置
- Hibernate基本配置
- 才高行厚的hibernate(1)---hibernate基本配置
- jQuery操作checkbox选择
- (超高性价比)有了PLC上网神器,PLC远程监视,PLC远程监控,手机远程监控PLC很简单
- Python中给多进程文件加锁
- 用SpannableString打造绚丽多彩的文本显示效果
- Git errors
- Hibernate总结-----Hibernate简介,基本配置
- Android之旅---AppWidget
- 关于Ruby的ARGV与gets语句同时使用的问题
- XOR Zero
- APK安装释放文件的过程
- CFBundleDocumentTypes app store 上架
- 《记录问题》spring注解如何读取.properties文件数据,以及spring静态变量读取.properties文件错误
- 利用java序列化进行对象深Clone
- Android酷炫实用的开源框架(UI框架)