Hibernate学习笔记(一)【基础配置篇】

一、首先找hibernate.properties文件

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://192.168.18.184:3306/SAMPLEDB
hibernate.connection.username=root
hibernate.connection.password=
hibernate.show_sql=true

二、其次找类配置文件 例如:Monkey.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">
<hibernate-mapping>
    <class name="mypack.Monkey" table="MONKEYS">
        <id name="id" column="ID" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" column="NAME" type="string" not-null="true"/>
        <property name="age" column="AGE" type="int"/>
        <property name="gender" column="GENDER" type="character"/>
    </class>
</hibernate-mapping>

三、使用Service 备注:经过改造后笔记

public static SessionFactory sessionFactory;
       static {
              try {
                     Configurationconfig = new Configuration();
                     config.addClass(Monkey.class);
                     sessionFactory =config.buildSessionFactory();
              } catch (RuntimeException e) {
                     // TODO: handle exception
                     e.printStackTrace();
                     throw e;
              }
       }
             
public void findAllMonkeys() {
     Session session = sessionFactory.openSession();
              Transaction tx = null;
              try {
                     tx =session.beginTransaction();
                     //
            	     Query query = session
                                   .createQuery("from Monkey as morder by m.id asc")
                     @SuppressWarnings("unchecked")
                     List<Monkey> monkeys= query.list();
                     //
                     s.save(m);
                    //
                     Monkey m = (Monkey)session.get(Monkey.class, monkey_id);
                     m.setAge(age);
                     //
                     session.delete(monkey);                     
                     tx.commit();
              } catch (RuntimeException e) {
                     // TODO: handle exception
                     if (tx != null) {
                            tx.rollback();
                     }
                     throw e;
              } finally {
                     session.close();
              }
       }

四、升级配置文件 Monkey.hbm.xml

1.dynamic-insert="true" dynamic-update="true"  动态版插入更新
2.access="field" 不是要get set直接采用属性
3.formula 无属性也可以直接查数据库组装数据
4.在class中声明mutable=”false” 或 @Immutable
    这意味着对该类的更新将会被忽略,不过不会抛出异常,只允许有增加和删除操作。
     在class中声明mutable=”false”:insert=允许,delete=允许,update=不允许
     在集合中声明mutable=”false” 或 @Immutable
      这意味着在这个集合中插入记录或删除孤行是不允许的,否则会抛出异常。只允许更新操作。
     不过,如果启用级联删除的话,当父类被删除时,其所有子类也将被删除,即使它是mutable的。
     在集合中声明mutable=”false”:insert=不允许,孤行删除=不允许,delete=允许,update=允许
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="mypack.Monkey" table="MONKEYS" dynamic-insert="true" dynamic-update="true"  > <!-- mutable="ture" -->
    <id name="id">
      <generator class="increment"/>
    </id>
    <property name="name" column="NAME" />
    <property name="gender" column="GENDER" access="field" />
    <property name="age" column="AGE" />
    <property name="avgAge"
      formula="(select avg(m.AGE) from MONKEYS m)" />
   <property name="description"  type="text" column="`MONKEY  DESCRIPTION`"/>
 </class>
</hibernate-mapping>

五、升级配置hibernate.properties --》hibernate.cfg.xml 不过需显示调用

Service类

public static SessionFactory sessionFactory;
       static {
              try {
       Configuration config = newConfiguration();
       config.configure();
           sessionFactory = config.buildSessionFactory();
              } catch (RuntimeException e) {
                     // TODO: handle exception
                     e.printStackTrace();
                     throw e;
              }
       }
<?xml version="1.0"encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
 PUBLIC "-//Hibernate/HibernateConfiguration DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/sampledb</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="show_sql">true</property>
<mapping resource="mypack/Monkey.hbm.xml" />
</session-factory>
</hibernate-configuration>

六、高级综合版 Spring中dao-config.xml xml引用sqlResource.properties

//配置文件配置
       <bean id="propertyConfigurer"
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="location">
                     <value>classpath:sqlResource.properties</value>
              </property>
	</bean>
       <bean id="dataSource"
              class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
              <property name="driverClassName">
                     <value>${database.driverclassname}</value></property>      
              <property name="url"><value>${database.url}</value></property>
              <property name="username"><value>${database.username}</value></property>
              <property name="password"><value>${database.password}</value></property>
              <property name="maxActive"><value>10</value></property>
              <property name="maxWait"><value>3000</value></property>      
              <property name="maxIdle"><value>5</value></property>
       </bean>

七、高级综合版 直接配置Spring中dao-config.xml

//直接配置
       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
              <property name="driverClassName" value="com.mysql.jdbc.Driver" />
              <property name="url" value="jdbc:mysql://192.168.18.184:3306/moniter?characterEncoding=UTF-8" />
              <property name="username" value="root" />
              <property name="password" value="" />
       </bean>

相关推荐