日期:2014-05-16 浏览次数:20943 次
<?xml version="1.0" encoding="UTF-8"?>
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/xuexiao?characterEncoding=UTF8" />
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="minPoolSize">
<value>1</value>
</property>
<property name="maxPoolSize">
<value>20</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/bean/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="myAction" class="com.hibernate.servlet.MyAction"></bean>
</beans>
public class Userdao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public boolean addUser(User user){
Session session=null;
try {
session = sessionFactory.openSession();
session.getTransaction().begin();
session.save(user);
session.getTransaction().commit();
} catch (Exception e) {
// TODO: handle exception
return false;
}finally{
session.close();
}
return true;
}
}
<bean id="usrDao" class="xx.xx.Userdao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>