刚学hibernate

J2EE 码拜 8年前 (2016-09-15) 1259次浏览
先说说想达到目的,hibernate流程基本是下面,
//1.读取hibernate.cfg.xml文件
//3. 打开Sesson
//4. 开启事务
//5. 执行持久化操作
//6. 提交事物
//7. 关闭Session
//8. 关闭SessionFectory
但是要是每次增删改查都写这些,感觉是不是需要进行管理一下,就用了SSH整合那套,没有弄struts,想要做的也只是在控制台输出输出验证。
项目结构如下:
刚学hibernate
test.java

package test.bean;
public class test {
private String  username;
private  int age;
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
}

对应映射文件test.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 
	package="test.bean">
	<class name="test" table="test">
		<id name="id" >  
	<generator class="native" />
		</id>
		<property name="name" />
		<property name="age"/>
	</class>
</hibernate-mapping>

dao文件

package test.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import test.bean.test;
public class testdaoimp {  
    private SessionFactory sessionFactory;
	public void add(test t) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
	    session.save(t);
	}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  <!-- 配置自动扫描的包 -->
  <!-- <context:component-scan base-package="bean"></context:component-scan>--> 
  <!-- 配置数据源 -->
  <!-- 导入资源文件 -->
  <context:property-placeholder location="classpath:db.properties"/>
  <!-- 配置数据源 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">   
              <property name="driverClassName">   
                     <value>oracle.jdbc.driver.OracleDriver</value>   
              </property>   
              <property name="url">   
                     <value>jdbc:oracle:thin:@192.168.1.163:1521:ORCL</value>   
              </property>   
              <property name="username">   
                     <value>test</value>   
              </property>   
              <property name="password">   
                     <value>123456</value>   
              </property>      
		      <property name="maxIdle" value="10"></property>
		      <property name="maxActive" value="20"></property>
       </bean>   
  <!-- 配置 Hibernate 的 SessionFactory 实例 : 通过Spring 提供的LocalSessionFactoryBean配置-->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <!-- 配置数据源属性 -->
    <property name="dataSource" ref="dataSource"></property>
    <!-- 配置 hibernate 映射文件的位置及名称 , 可以使用通配符-->
    <property name="mappingLocations">
     <list>
          <value>classpath:test/bean/test.hbm.xml</value>
     </list>
    </property>
    <!-- 使用hibernateProperties属性来配置Hibernate原生的属性 -->
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
      </props>
    </property>  
  </bean>
  <!-- Spring对hibernate的事务管理-->
  <bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<!-- 依赖注入上面定义的sessionFactory -->
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 配置事务的传播特性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
	    <tx:method name="get*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->
        <tx:method name="find*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="remove*" propagation="REQUIRED"/>
        <tx:method name="add*" propagation="REQUIRED"/>
      <!--默认其他方法都是REQUIRED-->
        <tx:method name="*"/>
		</tx:attributes>
	</tx:advice>
  <!-- 那些类的哪些方法参与事务 -->
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* daoimp..*.*(..))"/>
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
	</aop:config>
</beans>

测试类

package test5;
import test.bean.test;
import test.dao.testdaoimp;
public class test51 {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        test t=new test();
        t.setAge(15);
        t.setUsername("张三");
        testdaoimp tdi=new testdaoimp();
		tdi.add(t);

	}
}

结果:
Exception in thread “main” java.lang.NullPointerException
at test.dao.testdaoimp.add(testdaoimp.java:13)
at test5.test51.main(test51.java:16)
对hibernate持久化操作语句不是很熟。

解决方案

50

SessionFactory  假如不用注解  是需要new的  否则就会报空指针

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明刚学hibernate
喜欢 (0)
[1034331897@qq.com]
分享 (0)