标签:
这篇主要是展示spring和hibernate的整合的实例(上)——自己也是第一弄,有些内容还不是很规范,就是项目能跑通,有好的建议欢迎提出来~
继续上篇,下篇给出两个重要的文件spring.xml和pom.xml文件:
8、spring.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/aop http://www.springframework.org/schema/aop/spring-aop-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-3.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:hibernate.properties</value> </list> </property> </bean> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${databaseurl}"></property> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 数据库连接池保持的最小连接数 --> <property name="minIdle" value="5" /> <!-- 数据库连接池保持的最大连接数 --> <property name="maxIdle" value="30" /> <!-- 当数据库连接因为某种原因断掉之后,再重新从连接池中拿另外一个连接时实际上这个连接可能 已经无效,所以为了确保所拿到的连接全都有效需要在获取连接,返回连接以及连接空闲时进行 有效性验证 下面3个设置为ture时进行验证,默认为false --> <!-- 取得连接时是否进行有效性验证 --> <property name="testOnBorrow" value="true" /> <!-- 返回连接时是否进行有效性验证 --> <property name="testOnReturn" value="true" /> <!-- 连接空闲时是否进行有效性验证 --> <property name="testWhileIdle" value="true" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="datasource" /> <!-- 注意:我用的是Annotation的方式配置的Hibernate,这里的property的name是annotatedClasses --> <property name="annotatedClasses"> <list> <value>spring.hibernate.entity.Person</value> </list> </property> <property name="hibernateProperties"> <props> <!-- 设置Hibernate方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 是否打印sql --> <prop key="hibernate.show_sql">true</prop> <!-- 格式化sql --> <prop key="hibernate.format_sql">true</prop> <!-- 是否自动更新表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> <!-- 最大抓取深度,如果为0,则关闭默认的外连接抓取。建议值为0-3 --> <prop key="hibernate.max_fetch_depth">1</prop> <!-- 用于生成有助于调试的注释信息,默认为关闭 --> <prop key="hibernate.use_sql_comments">true</prop> </props> </property> <property name="packagesToScan" value="spring.hibernate.entity" /> </bean> <!-- 事务管理 注:事实上当事务管理器名字为transactionManager时,就不需要加transaction-manager属性 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean name="personDao" class="spring.hibernate.dao.PersonDao" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <context:component-scan base-package="service"></context:component-scan> </beans>
9、pom.xml文件(注意,spring的版本建议统一一下)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring</groupId> <artifactId>hibernate</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hibernate</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.1.4.RELEASE</spring.version> <slf4j.version>1.7.2</slf4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>3.2.0.Final</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.7.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency> </dependencies> </project>
10、附带一张执行后的截图:
要是大家发现有遗漏的代码或者内容,欢迎留言提出来,我会补上去的~
标签:
原文地址:http://www.cnblogs.com/tiramisuyj/p/4774412.html