分享

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

 浅殇图书馆 2018-06-09

相依为命 — 陈小春


方式一

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

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>
    ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www./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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

实体.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate./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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

方式二

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

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>
    ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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 + "]";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www./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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

方式三

spring 配置文件 + 实体类注解
  • 1

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

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

        ...
        <!-- hibernate 映射文件 -->
        <property name="mappingResources">
                <value>classpath:Stock.hbm.xml</value>
        </property>
        ....
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
        ...
        <property name="mappingResources">
            <list>
                <value>/hibernate/Stock.hbm.xml</value>
                <value>/hibernate/Stock.hbm.xml</value>
                ...
            </list>
        </property>
        ....
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

        ...
        <property name="mappingDirectoryLocations">
            <list>
            <!--实体类-->
                <value>classpath:com/jxust/model/</value>
            </list>
        </property>
        ....
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8


        <property name="mappingLocations"> 
            <value>/WEB-INF/petclinic.hbm.xml </value> 
        </property> 
  • 1
  • 2
  • 3
  • 4

可以使用通配符

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

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

    <property name="packagesToScan">    
                <value>com/jxust/model</value>  
        </property>
  • 1
  • 2
  • 3

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多