hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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 -->
<session-factory>
<!--
必须的配置:
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate是一个应用层的框架,会给你生成sql语句,但是你要告诉我你使用什么数据库,
通过方言来告诉我你使用什么数据库
数据库方言:
非必须的选项:
#hibernate.show_sql true 在控制台上显示生成的sql语句
hibernate.format_sql true 打印好看的sql语句
根据映射文件生成DDL建表语句
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update 有表不创建 更新表 没表我给你创建表
#hibernate.hbm2ddl.auto validate
-->
<!-- 必须的配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 非必须的配置项 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- 开启二级 缓存-->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!--配置使用的二级缓存 -->
<property name="cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 开启查询缓存 hql查询缓存到二级缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- 把映射文件引进来 -->
<mapping resource="com/qf/pojo/Users.hbm.xml"/>
<mapping resource="com/qf/pojo/Money.hbm.xml"/>
<mapping resource="com/qf/pojo/Order.hbm.xml"/>
<!-- 配置二级缓存 来缓存哪些类 -->
<class-cache usage="read-only" class="com.qf.pojo.User"/>
<class-cache usage="read-only" class="com.qf.pojo.Order"/>
<!-- 配置缓存哪些集合 -->
<collection-cache usage="read-only" collection="com.qf.pojo.User.orderSet"/>
</session-factory>
</hibernate-configuration>
One-to-many
## user.hbm.xml ##
private Long uid;
private String uname;
private String ugender;
private String upwd;
private Set<Order> orderSet = new HashSet<>();
<?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 >
<!-- 配置javabean和数据库表的映射 class name:表示类的全限定名(包名+类型) table:表名 -->
<class name="com.qf.pojo.Users" table="userinfo" >
<!-- 配置主键 id name:javabean的属性名 column:数据库的字段名 -->
<id name="uid" column="id">
<!-- 主键生成策略 native:采取本地数据库的一个主键生成策略 mysql:自增长 oracle:序列 sequence -->
<generator class="native"></generator>
</id>
<!-- 配置普通属性 -->
<property name="uname" column="userName"></property>
<property name="ugender" column="gender"></property>
<property name="upwd" column="pwd"></property>
<set name="orderSet" cascade="save-update">
<key column="user_order_id"></key>
<one-to-many class="com.qf.pojo.Order"/>
</set>
</class>
</hibernate-mapping>
order.hbm.xml
private long oid;
private String oname;
private Double oprice;
private Users users;
<?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 >
<!-- 配置javabean和数据库表的映射 class name:表示类的全限定名(包名+类型) table:表名 -->
<class name="com.qf.pojo.Order" table="orderinfo">
<!-- 配置主键 id name:javabean的属性名 column:数据库的字段名 -->
<id name="oid" column="id">
<!-- 主键生成策略 native:采取本地数据库的一个主键生成策略 mysql:自增长 oracle:序列 sequence -->
<generator class="native"></generator>
</id>
<!-- 配置普通属性 -->
<property name="oname" column="oName"></property>
<property name="oprice" column="oPrice"></property>
<many-to-one name="users" column="user_order_id" class="com.qf.pojo.Users"/>
</class>
</hibernate-mapping>