您的位置:首页 > 编程语言 > Java开发

Spring 整合 Hibernate5 时的方式、引入关系映射文件的多种方式

2017-03-06 20:37 387 查看
相依为命 — 陈小春

方式一

spring配置文件 + hibernate.cfg.xml + 实体.hbm.xml


Spring 配置文件

...
<!-- 配置hibernate 相关属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载hibernate配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
...


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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/shop</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<mapping resource="classpath:Category.hbm.xml"/>
</session-factory>
</hibernate-configuration>


实体.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-3-6 16:04:32 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.jxust.model.Category" table="CATEGORY">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="type" type="java.lang.String">
<column name="TYPE" />
</property>
<property name="hot" type="boolean">
<column name="HOT" />
</property>
</class>
</hibernate-mapping>


方式二

spring 配置文件 + hibernate.cfg.xml + 实体类注解


spring 配置文件

...
<!-- 配置hibernate 相关属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载hibernate配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
...


Category.java


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* 商品实体类
* 使用全注解的方式,可以不用配置Category.hbm.xml
* @author Peng
* @Date2017年3月6日下午5:37:31
*/
@Entity
public class Category {
/**
* 主键
*/
private int id;
/**
* 类型名称
*/
private String type;
/**
* 类型是否为热点类型
*/
private boolean hot;

@Id
@GeneratedValue
@Column(name="id",unique = true,nullable= false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="type")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Column(name="hot")
public boolean getHot() {
return hot;
}
public void setHot(boolean hot) {
this.hot = hot;
}
@Override
public String toString() {
return "Category [id=" + id + ", type=" + type + ", hot=" + hot + "]";
}
}


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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/shop</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<mapping class="com.jxust.model.Category"/>
</session-factory>
</hibernate-configuration>


方式三

spring 配置文件 + 实体类注解


spring 配置文件

<!-- 配置hibernate 相关属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>

<property name="hibernateProperties" >
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true </prop>

<!--它包含4个属性:
* create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变
* create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除
* update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行
* validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值  -->

<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<!-- hibernate 映射文件 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/jxust/model/</value>
</list>
</property>
<!--
mappingResources:指定classpath下具体映射文件名
mappingLocations:可以指定任何文件路径
mappingDirectoryLocations:指定映射的文件路径
mappingJarLocations:指定加载的映射文件在jar文件中
-->

</bean>


引入关系映射文件的多种方式

...
<!-- hibernate 映射文件 -->
<property name="mappingResources">
<value>classpath:Stock.hbm.xml</value>
</property>
....


...
<property name="mappingResources">
<list>
<value>/hibernate/Stock.hbm.xml</value>
<value>/hibernate/Stock.hbm.xml</value>
...
</list>
</property>
....


...
<property name="mappingDirectoryLocations">
<list>
<!--实体类-->
<value>classpath:com/jxust/model/</value>
</list>
</property>
....


<property name="mappingLocations">
<value>/WEB-INF/petclinic.hbm.xml </value>
</property>


可以使用通配符

<property name="mappingLocations">
<value>classpath:/com/company/domainmaps/*.hbm.xml </value>
</property>


对应注解的实体类创建表时,可以使用包扫描

<property name="packagesToScan">
<value>com/jxust/model</value>
</property>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate spring