您的位置:首页 > 其它

电子相册系统(三)使用技术

2016-04-19 22:42 225 查看

1. 说明

电子相册的系统主要用到的技术是:Spring3.0 + Hibernate4。我们都知道Spring就是要管理我们项目所有的Bean对象,而Hibernate就是要让对象实现持久化,也就是ORM的应用,我们无需手动创建表,所有对象表的创建就交给Hibernate去执行吧。

2. Spring3.0 + Hibernate的结合配置

我们把Spring+hibernate的配置文件名叫做appicationContext.xml。配置信息如下:

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 定义数据源Bean,使用C3P0数据源实现,并通过依赖注入设置数据库的驱动、URL、用户名、密码
最大连接数、最小连接数、初始化连接数、最大空闲时间 -->
<!-- 定义数据源Bean,使用C3P0数据源实现
并通过依赖注入设置数据库的驱动、URL、用户名、密码
最大连接数、最小连接数、初始化连接数、最大空闲时间 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost:3306/photo"
p:user="root"
p:password="root"
p:maxPoolSize="200"
p:minPoolSize="2"
p:initialPoolSize="2"
p:maxIdleTime="200"/>

<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<!-- mappingResouces属性用来列出全部映射文件 -->
<property name="mappingResources">
<list>
<!-- 以下用来列出Hibernate映射文件 -->
<value>com/owen/domain/User.hbm.xml</value>
<value>com/owen/domain/Photo.hbm.xml</value>
</list>
</property>
<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<!-- 指定数据库方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<!-- 是否根据需要每次自动创建数据库 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<prop key="hibernate.show_sql">false</prop>
<!-- 将SQL脚本进行格式化后再输出 -->
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置userDao组件为userDao组件注入SessionFactory实例 -->
<bean id="userDao" class="com.owen.dao.impl.UserDaoImpl"
p:sessionFactory-ref="sessionFactory"/>

<!-- 配置photoDao组件,为photoDao组件注入SessionFactory实例 -->
<bean id="photoDao" class="com.owen.dao.impl.PhotoDaoImpl"
p:sessionFactory-ref="sessionFactory"/>

<!-- 配置albumService业务逻辑组件 -->
<bean id="albumService" class="com.owen.service.impl.AlbumServiceImpl"
p:userDao-ref="userDao"
p:photoDao-ref="photoDao"/>

<!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 -->
<!-- 该类实现PlatformTransactionManager接口,是针对Hibernate的特定实现-->
<!-- 配置HibernateTransactionManager时需要依注入SessionFactory的引用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>

<!-- 配置事务切面Bean,指定事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 用于配置详细的事务语义 -->
<tx:attributes>
<!-- 所有以'get'开头的方法是read-only的 -->
<tx:method name="get*" read-only="true"/>
<!-- 其他方法使用默认的事务设置 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 配置一个切入点,匹配指定包下所有以Impl结尾的类执行的所有方法 -->
<aop:pointcut id="owenService"
expression="execution(* com.owen.service.impl.*Impl.*(..))"/>
<!-- 指定在leeService切入点应用txAdvice事务切面 -->
<aop:advisor advice-ref="txAdvice"
pointcut-ref="owenService"/>
</aop:config>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: