侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Spring Hibernate Mybatis配置详解

2024-05-15 星期三 / 0 评论 / 0 点赞 / 80 阅读 / 4977 字

1. Spring + Hibernate4(spring-hibernate.xml)<!-- 引入jdbc properites文件 --><context:property-placeholde

1. Spring + Hibernate4(spring-hibernate.xml)

<!-- 引入jdbc properites文件 -->

<context:property-placeholder location="classpath:/config/jdbc.properties" />

<!-- dataSource defined -->

<!-- destory-method="close" 作用:当数据库连接不适用时,把该连接重新放到数据连接池中 -->

<bean id="**dataSource**" class="" destroy-method="close"> <property name="driverClass" value="" /> <property name="jdbcUrl" value="" /> <property name="user" value="" /> <property name="password" value="" /> ......</bean>

<!-- 配置sessionFactory -->

<bean id="**sessionFactory**" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="**dataSource**" ref="dataSource" /> <property name="**hibernateProperties**"> <value>... ...</value> </property> <property name="**packagesToScan**" value="com.self.entity" ></bean>

<!-- 定义事务管理器transactionManager -->

<bean id="**transactionManager**" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="**sessionFactory**" ref="**sessionFactory**" /></bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">tx:attributes<tx:method name="create*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" />.......</tx:attributes></tx:advice>

<!-- 定义切面aop -->

aop:config<aop:pointcut id="serviceOption" expression="execution(* com.self.service.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOption" /></aop:config>

2. Spring + MyBatis(spring-mybatis.xml)

<!-- 引入jdbc properites文件 -->

<context:property-placeholder location="classpath:/config/jdbc.properties" />

<!-- dataSource defined -->

<bean id="**dataSource**" class="" destroy-method="close"> <property name="driverClass" value="" /> <property name="jdbcUrl" value="" /> <property name="user" value="" /> <property name="password" value="" /> ......</bean>

<!-- 定义sessionFactory, **mapperLocations**配置**Mapper.xml文件位置,**configLocation**配置mybatis-config文件位置 -->

<bean id="**sessionFactory**" class="com.mybatis.spring.SqlSessionFactoryBean"> <property name="**dataSource**" ref="**dataSource**" /> <property name="mapperLocations" value="classpath:/mapping/*Mapper.xml" /> <property name="**configLocation**" value="classpath:/config/mybatis-config.xml" /></bean>

<!-- DAO接口所在包名(com.self.dao),Spring会自动查找其下的类, 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->

<bean class="org.mybatis.spring.mapper.**MapperScannerConfigurer**"> <property name="**basePackage**" value="com.self.dao" /> <property name="**sessionFactory**" value="**sessionFactory**" /></bean>

<!-- 定义事务管理器transactionManager -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager><property name="**dataSource**" ref="**dataSource**" /></bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">tx:attributes<tx:method name="find*" propagation="REQUIRED" /><tx:method name="get*" propagation="REQUIRED" />......</tx:attributes></tx:advice>

<!-- 定义切面aop -->

aop:config<aop:pointcut id="serviceOption" expression="execution(* com.self.service.impl.*(..))" /><aop:advisor pointcut-ref="serviceOption" advice-ref="txAdivce" /></aop:config>

广告 广告

评论区