@Entity
@Table(name = "Student")
public class Student implements java.io.Serializable {
private static final long serialVersionUID = 1L;
/** 属性userCode */
private String userCode;
private String userName;
/**
* 类Student的默认构造方法
*/
public Student() {
}
/**
* 属性userCode的getter方法
*/
@Id
@Column(name = "USERCODE")
public String getUserCode() {
return this.userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
@Column(name = "USERNAME")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
applicationContext.xml
<context:component-scan base-package="com.sinosoft" />
<!-- 属性文件读入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSourceTarget"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
</bean>
<!-- 开发用,用于拦截执行的SQL并打印-->
<bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource">
<constructor-arg>
<ref local="dataSourceTarget" />
</constructor-arg>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--
<property name="mappingResources">
<list></list>
</property>
<property name="mappingLocations">
<list>
<value>classpath*:/*.hbm.xml</value>
</list>
</property>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
</bean>
3 个解决方案
#1
你用注解你也要告诉spring扫描啊。有两种扫描方式,我这里就用最方便的一种.在sessionFactory配置里加入
<property name="packagesToScan" value="你的实体类的所在包"/>
这个可以扫描此包下所有@Entity的类。关于packagesToScan的说明楼主可以google
<property name="packagesToScan" value="你的实体类的所在包"/>
这个可以扫描此包下所有@Entity的类。关于packagesToScan的说明楼主可以google
#2
<!--定义Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 交由注解扫描实体类,省去了以前hibernate的配置文件,自动生成数据表 -->
<property name="packagesToScan" value="com.fro.entity"/>
...............
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 交由注解扫描实体类,省去了以前hibernate的配置文件,自动生成数据表 -->
<property name="packagesToScan" value="com.fro.entity"/>
...............
#3
感谢楼上的回答,的确没配置扫描
#1
你用注解你也要告诉spring扫描啊。有两种扫描方式,我这里就用最方便的一种.在sessionFactory配置里加入
<property name="packagesToScan" value="你的实体类的所在包"/>
这个可以扫描此包下所有@Entity的类。关于packagesToScan的说明楼主可以google
<property name="packagesToScan" value="你的实体类的所在包"/>
这个可以扫描此包下所有@Entity的类。关于packagesToScan的说明楼主可以google
#2
<!--定义Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 交由注解扫描实体类,省去了以前hibernate的配置文件,自动生成数据表 -->
<property name="packagesToScan" value="com.fro.entity"/>
...............
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 交由注解扫描实体类,省去了以前hibernate的配置文件,自动生成数据表 -->
<property name="packagesToScan" value="com.fro.entity"/>
...............
#3
感谢楼上的回答,的确没配置扫描