需要的.jar文件:
spring框架下的包:
hibernate框架下的包:
配置文件beans.xml:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="cn.com"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/websystem?useUnicode=true&charaterEncoding=UTF-8" />
<property name="user" value="root"/>
<property name="password" value="yaoweiwei"/>
<!-- 初始化时连接池的最小连接 -->
<property name="initialPoolSize" value="1"/>
<!-- 连接池里最小连接数 -->
<property name="minPoolSize" value="1"/>
<!-- 连接池里最大连接数 default :15 -->
<property name="maxPoolSize" value="300"/>
<!-- 最大空闲时间,60秒未使用则抛弃,default:0 永不抛弃 -->
<property name="maxIdleTime" value="60"/>
<!-- 连接池里连接耗尽时每次增加的数目 default:3-->
<property name="acquireIncrement" value="6"/>
<!-- 设置每60秒检查 所有连接池里的链接-->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/com/bean/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
用来测试生成表的o/m映射文件User.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="cn.com.bean">
<class name="User">
<id name="name" length="20"/>
<property name="psw" not-null="true" length="20"/>
<property name="sex" not-null="true" length="5">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">cn.com.bean.Sex</param>
<param name="name">12</param>
</type>
</property>
</class>
</hibernate-mapping>