hibernate与ibatis也可以这样使用
摘要:这样配置主要目的是使用ibatis操作数据库,实体对象映射到表的过程通过hibernate的annotation来完成。
?
?
第一步:准备数据库连接参数
?
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=roothibernate.dialect=org.hibernate.dialect.MySQLDialect
?第二步:ibatis的配置
?
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"><sqlMapConfig><settings cacheModelsEnabled="true" enhancementEnabled="true"lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"maxSessions="10" maxTransactions="5" useStatementNamespaces="true" /><!--加载实体与表的映射文件 --><sqlMap resource="com/nalike/*/model/sql/LogSQL.xml" /></sqlMapConfig>
?第三步:配置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/><property name="ignoreResourceNotFound" value="true" /><property name="locations"><list><!-- 标准配置 --><value>classpath*:/resource/jdbc.properties</value></property></bean><!-- 数据源配置 --><!-- c3p0 详细配置 --><bean id="dataSource"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" ref="dataSource" /></bean><!-- 通过hibernate生成数据库 --><bean id="sessionFactory"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-/><!-- 配置事务管理器 单数据源事务 --><bean id="transactionManager"ref="dataSource" /></bean><!-- SMTP邮件服务配置 --><bean id="javaMailSender"value="10" /><!-- 最大线程数 --><property name="maxPoolSize" value="50" /><!-- 最大队列数 --><property name="queueCapacity" value="10000" /><!-- 线程池维护线程所允许的空闲时间 --><property name="keepAliveSeconds" value="60" /></bean></beans>?
未完待续。文章写的太少的,组织语言的能力太差了。想法在大脑里面,就是不能用最好最体贴的方式表达出来。我杯具了...