【SSH】spring 整合 hibernate

微微撒 2020-02-18

spring-hibernate-1.2.9.jar

applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- //加载实体类的映射文件位置及名称 -->
        <property name="mappingLocations" value="classpath:k/bean/*.hbm.xml"></property>
        <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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 配置Hibernate的基本属性 -->
        <!-- 1.数据源配置到IOC容器中 -->
        <!-- 2.关联的.hbm.xml也在IOC容器配置SessionFactory实例 -->
        <!-- 3.配置Hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="cache.use_second_level_cache">true</property>
        <property name="ache.provider_class">org.hibernate.cache.internal.DefaultCacheKeysFactory</property>
        <property name="generate_statistics">true</property>
    </session-factory>
</hibernate-configuration>

Employee.hbm.xml

<?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>
    <class name="k.domain.Employee" table="t_employee">
        <id name="id" column="id" type="java.lang.Integer">
            <!-- 设置数据库表id增长策略  native:生成表id值就是主键自动增长  uuid -->
            <generator class="native"></generator>
        </id>
        <!-- 配置其他属性和表字段对应  name属性:实体类属性名称 column属性:生成表字段名称 -->
        <property name="name" column="name" type="java.lang.String" length="64"></property>
        <property name="email" column="email" type="java.lang.String" length="64"></property>
        <property name="hireDate" column="hireDate" type="java.util.Date"></property>
        <property name="salary" column="salary" type="java.lang.Float"></property>
        <property name="password" column="password" type="java.lang.String" length="64"></property>
        <property name="grade" column="grade" type="java.lang.Integer" length="3"></property>
        <many-to-one name="department" class="k.domain.Department" column="department_id"></many-to-one>
    </class>
</hibernate-mapping>

相关推荐

LetonLIU / 0评论 2020-05-29
东方咖啡屋 / 0评论 2020-01-06