ibatis與hibernate結合使用(配置篇一)
<PRE class=xml style="FONT-WEIGHT: bold; BACKGROUND-COLOR: #c5c5c5" name="code">1、配置ibatis:</PRE><PRE class=xml style="FONT-WEIGHT: bold; BACKGROUND-COLOR: #c5c5c5" name="code"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
";
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
<!-- 加載數據庫與實體之間的映射配置 -->
<sqlMap resource="com/nalike//model/sql/" />
</sqlMapConfig>
</PRE>
2、配置applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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">
<!-- 設置需要進行Spring注解掃描的類包 -->
<context:component-scan base-package="com.nalike" />
<!-- 定義受環境影響易變的變量 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 標準配置 -->
<value>classpath*:/resource/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 數據源配置 -->
<!-- c3p0 詳細配置 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 連接池中保留的最小的連接數 -->
<property name="minPoolSize" value="5" />
<!-- 連接池中保留的最大的連接數 默認為:15-->
<property name="maxPoolSize" value="30" />
<!--初始化時獲取的連接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="10" />
<!--最大空閑時間,60秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--JDBC的標準參數,用以控制數據源內加載的PreparedStatements數量。但由于預緩存的statements
屬于單個connection而不是整個連接池。所以設置這個參數需要考慮到多方面的因素。
如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default: 0-->
<property name="maxStatements" value="0" />
<!--每60秒檢查所有連接池中的空閑連接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
<!--定義在從數據庫獲取新連接失敗后重復嘗試的次數。Default: 30 -->
<property name="acquireRetryAttempts" value="30" />
<!--獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數據源仍有效
保留,并在下次調用getConnection()的時候繼續嘗試獲取連接。如果設為true,那么在嘗試
獲取連接失敗后該數據源將申明已斷開并永久關閉。Default: false-->
<property name="breakAfterAcquireFailure" value="true" />
<!--因性能消耗大請只在需要的時候使用它。如果設為true那么在每個connection提交的
時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable
等方法來提升連接測試的性能。Default: false -->
<property name="testConnectionOnCheckout" value="false" />
</bean>
<!-- ibatis配置 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/resource/SqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通過hibernate生成數據庫 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<value>
<!-- 設置數據庫方言 -->
hibernate.dialect=${hibernate.dialect}
<!-- 設置自動創建|更新|驗證數據庫表結構 -->
hibernate.hbm2ddl.auto=update
</value>
</property>
<property name="packagesToScan" value="com.nalike.*.model" />
</bean>
<!-- 使用注解方式定義事務 -->
<tx:annotation-driven proxy-target-class="true"
transaction-manager="transactionManager" />
<!-- 配置事務管理器 單數據源事務 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- SMTP郵件服務配置 -->
<bean id="javaMailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
<!-- SSL連接配置 -->
<!--
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
-->
</props>
</property>
</bean>
<!-- SMTP郵件異步發送 -->
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心線程數 -->
<property name="corePoolSize" value="10" />
<!-- 最大線程數 -->
<property name="maxPoolSize" value="50" />
<!-- 最大隊列數 -->
<property name="queueCapacity" value="10000" />
<!-- 線程池維護線程所允許的空閑時間 -->
<property name="keepAliveSeconds" value="60" />
</bean>
</beans>
3、配置數據庫與實體之間的映射文件:(以LogSQL.xml為例)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="Log" type="com.nalike.system.model.Log"/>
<resultMap class="Log" id="LogResult">
<result property="id" column="id" />
<result property="createTime" column="create_time" />
<result property="ip" column="ip" />
<result property="content" column="content" />
<result property="type" column="type" />
</resultMap>
<insert id="addLog" parameterClass="Log">
<![CDATA[
insert into s_log (id,create_time, ip, content, type)
values (#id#,#createTime#, #ip#, #content#, #type#)
]]>
<selectKey keyProperty="id" resultClass="int">
select @@identity as id
</selectKey>
</insert>
</sqlMap>
4、配置數據庫連接的jdbc.properties文件:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test11?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=root hibernate.dialect=org.hibernate.dialect.MySQLDialect
本文由用戶 zoopnin 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!