this.getSession().save(person); 为什么不存

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="org.gjt.mm.mysql.Driver">
        </property>
        <property name="url" value="jdbc:mysql://localhost:3306/zxw"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/zxw/vo/Person.hbm.xml</value></list>
        </property>
    </bean>
    
    <bean id="personDao" class="com.zxw.dao.PersonDao" abstract="true"></bean>
    
    <bean id="personDaoImpl" class="com.zxw.dao.impl.PersonDaoImpl" parent="personDao">
        <property name="hibernateTemplate">
            <ref bean="hibernateTemplate"/>
        </property>
    </bean>
    
    
    <bean id="hibernateTemplate"
        class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    
</beans>

 

package com.zxw.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zxw.dao.PersonDao;
import com.zxw.vo.Person;

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {

    public void insert(Person person) {
//      this.getHibernateTemplate().save(person);
        this.getSession().save(person);
        this.getSession().beginTransaction().commit();
    }

    public List query() {
        List all=null;
        String hql="FROM com.zxw.vo.Person AS p";
        Query q=this.getSession().createQuery(hql);
        all=q.list();
        return all;
    }

    public List queryByName(String name) {
        List all=null;
        String hql="FROM com.zxw.vo.Person AS p WHERE p.name=?";
        Query q=this.getSession().createQuery(hql);
        q.setString(0,name);
        all=q.list();
        return all;
    }

}

用 this.getHibernateTemplate().save(person);
能存入数据库

用this.getSession().save(person);
运行没反应 也不搞错

  1. 你自己配置的使用hibernatetemplate。
  2. 使用session,就把dao注入sessionFactory。 两种只需要一种就行了。 使用 hibernate template 是不需要继承 Spring提供的 dao 接口的。