<?xml version="1.0" encoding="UTF-8"?>
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="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/db_xyd?useUnicode=true&amp;amp;amp;amp;amp;characterEncoding=utf-8-">
</property>
<property name="username" value="root"></property>
<property name="password" value="1234"></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.SQLServerDialect-->
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/model/TDictionary.hbm.xml</value>
</list>
</property>
</bean>
<bean id="TDictionaryDAO" class="com.dao.TDictionaryDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="dictionaryAction" class="com.action.dictionaryAction" scope="prototype">
<property name="dictionaryDAO">
<ref bean="TDictionaryDAO" />
</property>
</bean>
package com.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.model.TDictionary;
public class TDictionaryDAO extends HibernateDaoSupport
{
private static final Log log = LogFactory.getLog(TDictionaryDAO.class);
public void save(TDictionary transientInstance)
{
log.debug("saving TDictionary instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public static TDictionaryDAO getFromApplicationContext(ApplicationContext ctx) {
return (TDictionaryDAO) ctx.getBean("TDictionaryDAO");
}
}
package com.action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.dao.TDictionaryDAO;
import com.model.TDictionary;
import com.opensymphony.xwork2.ActionSupport;
public class dictionaryAction extends ActionSupport
{
private int OID;
private String willtake;
private TDictionaryDAO dictionaryDAO;
public String dictionaryAdd()
{
TDictionary dict=new TDictionary();
dict.setOID(OID);
dict.setL_key(100000);
dict.setVc_value(willtake);
dict.setVc_caption("vc_caption");
//这里报空指针异常,是dictionaryDAO空
dictionaryDAO.save(dict);
return "success";
}
public TDictionaryDAO getDictionaryDAO()
{
return dictionaryDAO;
}
public void setDictionaryDAO(TDictionaryDAO dictionaryDAO)
{
this.dictionaryDAO = dictionaryDAO;
}
}