67,513
社区成员
发帖
与我相关
我的任务
分享
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 其他datasource有C3P0和BoneCP -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<tx:annotation-driven/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- <property name="packagesToScan" value="com.apress.prospring3.ch9.domain"/> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<context:component-scan base-package="com.springhibernate.hibernate" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:HibernateSessionFactory.xml");
// ctx.refresh();
ContactDao contactDao = ctx.getBean("contactDao", ContactDao.class);
SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
// DataSource datasource = (DataSource)ctx.getBean("dataSource");
System.out.println(sessionFactory.toString());
<!-- 这个是声明要扫描的 xxx.hbm.xml所在的文件(也扫描了对应的类 ),主要是XXX.hbm.xml文件,更具该文件生成数据库表-->
<property name="mappingDirectoryLocations" >
<list>
<value>classpath:com/zsq/sjcj1/model</value>
</list>
</property>
这个是一种
要是用的是hibernate的注解的话,必须要配置扫描的实体类所在的包@Entity
@Table(name="contact")
public class Contact implements Serializable {
private static final long serialVersionUID = -554352144027434074L;
private Long id;
private String firstName;
private String lastName;
private Date birthDay;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name="LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Temporal(TemporalType.DATE)
@Column(name="BIRTH_DAY")
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
}
你说必须要配置扫描实体类所在的包 是加上这个么:<property name="packagesToScan" value="com.apress.prospring3.ch9.domain"/> 不过不是spring3.1之后就不用配置这句话了么?