实体类:
public class Student {
private int id;
private String name;
private String gender;
private Date startDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">:
<hibernate-mapping package="com.cn.entity">
<class name="Student" table="student">
<id name="id" type="java.lang.Integer" >
<column name="id"/>
<generator class="sequence"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true"/>
</property>
</class>
</hibernate-mapping>
spring配置文件:
<?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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">
<!-- ********************************************************************************************************* -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" lazy-init="true"
depends-on="propertyConfigurer">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="autoCommitOnClose" value="true" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="80" />
<property name="maxIdleTime" value="1800" />
<property name="maxStatements" value="100" />
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="6000" />
<property name="testConnectionOnCheckout" value="true" />
<property name="checkoutTimeout" value="30000" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath:com/gv/appstore/common/pojo/*.hbm.xml</value>
<value>classpath:com/gv/*/common/pojo/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.default_entity_mode">pojo</prop>
<prop key="hibernate.query.substitutions">true=1,false=0</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
</entry>
</map>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
depends-on="sessionFactory">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="interceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor"
depends-on="transactionManager,txAttributes">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref bean="txAttributes" />
</property>
</bean>
<bean id="dao" class="com.gv.core.hibernate3.HibernateBaseDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="txAttributes"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="upload*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="read*">PROPAGATION_REQUIRED,-readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,-readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,-readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,-readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="autoProxyCreatorGdasn"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
depends-on="interceptor">
<property name="interceptorNames">
<list>
<value>interceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>*Service*</value>
</list>
</property>
</bean>
<!-- 开启注解配置 -->
<context:annotation-config />
<!-- 对指定的包进行组件扫描 -->
<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="com.gv" />
</beans>
public class Student {
private int id;
private String name;
private String gender;
private Date startDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">:
<hibernate-mapping package="com.cn.entity">
<class name="Student" table="student">
<id name="id" type="java.lang.Integer" >
<column name="id"/>
<generator class="sequence"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true"/>
</property>
</class>
</hibernate-mapping>
spring配置文件:
<?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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">
<!-- ********************************************************************************************************* -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" lazy-init="true"
depends-on="propertyConfigurer">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="autoCommitOnClose" value="true" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="80" />
<property name="maxIdleTime" value="1800" />
<property name="maxStatements" value="100" />
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="6000" />
<property name="testConnectionOnCheckout" value="true" />
<property name="checkoutTimeout" value="30000" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath:com/gv/appstore/common/pojo/*.hbm.xml</value>
<value>classpath:com/gv/*/common/pojo/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.default_entity_mode">pojo</prop>
<prop key="hibernate.query.substitutions">true=1,false=0</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
</entry>
</map>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
depends-on="sessionFactory">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="interceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor"
depends-on="transactionManager,txAttributes">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref bean="txAttributes" />
</property>
</bean>
<bean id="dao" class="com.gv.core.hibernate3.HibernateBaseDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="txAttributes"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="upload*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="read*">PROPAGATION_REQUIRED,-readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,-readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,-readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,-readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="autoProxyCreatorGdasn"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
depends-on="interceptor">
<property name="interceptorNames">
<list>
<value>interceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>*Service*</value>
</list>
</property>
</bean>
<!-- 开启注解配置 -->
<context:annotation-config />
<!-- 对指定的包进行组件扫描 -->
<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="com.gv" />
</beans>