梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
<?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="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        
<property name="driverClass" value="com.mysql.jdbc.Driver" />
        
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/hwpok" />
        
<property name="user" value="mydata" />
        
<property name="password" value="root" />
        
<property name="autoCommitOnClose" value="true"/>
        
<property name="checkoutTimeout" value="5000"/>
        
<property name="initialPoolSize" value="2"/>
        
<property name="minPoolSize" value="2"/>
        
<property name="maxPoolSize" value="4"/>
        
<property name="maxIdleTime" value="25200"/>
        
<property name="acquireIncrement" value="1800"/>
        
<property name="maxIdleTimeExcessConnections" value="5"/>
    
</bean>
    
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<property name="dataSource" ref="dataSource" />
        
<property name="mappingDirectoryLocations">
            
<list>
                
<value>classpath:/hvp.spring.hibernate.orm</value>
            
</list>
        
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                
<prop key="hibernate.show_sql">true</prop>
                
<prop key="hibernate.generate_statistics">true</prop>
            
</props>
        
</property>
    
</bean>
    
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>
    
<bean id="userDAO" class="hvp.spring.hibernate.orm.UserDAO">
        
<property name="hibernateTemplate" ref="hibernateTemplate" />
    
</bean>
</beans>
hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="hvp.spring.hibernate.orm">
    
<class name="User" table="le_users">
        
<id name="userId" column="user_id">
            
<generator class="increment" />
        
</id>
        
<property name="userName" column="user_name" />
        
<property name="sex" column="sex" />
    
</class>
</hibernate-mapping>
dao
package hvp.spring.hibernate.orm;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class UserDAO extends HibernateDaoSupport
{   
    
public void addUser(User user){
        
this.getHibernateTemplate().save(user);
    }

    
    
public static void main(String[] a){
        User user 
= new User();
        user.setUserName(
"hwpokay");
        user.setSex(
0);
        
        String configPath 
= "hvp/spring/aspectj/anno/beans.xml";
        ApplicationContext ctx 
= new ClassPathXmlApplicationContext(configPath);
        
        UserDAO userDAO 
= (UserDAO)ctx.getBean("userDAO");
        userDAO.addUser(user);
    }

}

posted on 2008-08-14 08:01 HUIKK 阅读(486) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: