導(dǎo)入相應(yīng)的jar包
1、spring的jar包
2邓馒、Mybatis的jar包
3光酣、Spring+mybatis的整合包。
4脉课、Mysql的數(shù)據(jù)庫驅(qū)動jar包救军。
5、數(shù)據(jù)庫連接池的jar包倘零。
配置MyBatis: Configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置bean類型的別名 -->
<typeAliases>
<typeAlias type="com.gongxm.bean.Book" alias="book"/>
<package name="com.gongxm.bean"/>
</typeAliases>
<!--這個可以放在spring中配置-->
<mappers>
<mapper resource="mapper/Book.xml"/>
</mappers>
</configuration>
配置spring: spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!--連接池中保留的最小連接數(shù)唱遭。-->
<property name="minPoolSize" value="${minPoolSize}"/>
<!--連接池中保留的最大連接數(shù)。Default: 15 -->
<property name="maxPoolSize" value="${maxPoolSize}"/>
<!--初始化時獲取的連接數(shù)呈驶,取值應(yīng)在minPoolSize與maxPoolSize之間拷泽。Default: 3 -->
<property name="initialPoolSize" value="${initialPoolSize}"/>
<!--最大空閑時間,maxIdleTime秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄俐东。Default: 0 -->
<property name="maxIdleTime" value="${maxIdleTime}"/>
<!--當(dāng)連接池中的連接耗盡的時候c3p0一次同時獲取的連接數(shù)跌穗。Default: 3 -->
<property name="acquireIncrement" value="${acquireIncrement}"/>
<!--每60秒檢查所有連接池中的空閑連接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"/>
<!--定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù)虏辫。Default: 30 -->
<property name="acquireRetryAttempts" value="${acquireRetryAttempts}"/>
</bean>
<!-- SQLSessionFactory的配置 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/Configuration.xml"/>
</bean>
</beans>
傳統(tǒng)dao的配置方法
<bean id="bookDao" class="com.gongxm.dao.impl.BookDaoImpl">
<property name="sqlSessionFactory" ref="sessionFactory"/>
</bean>
mapper代理形式dao的配置
- 第一種方式 : 代理指定的接口
1.定義Dao接口
2.定義mapper的xml文件, 文件名必須是接口名
3.mapper的xml中的namespace必須是接口的全類名
4.在spring配置文件中加入內(nèi)容:
<bean id="bookMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 指定要代理的接口 -->
<property name="mapperInterface" value="com.gongxm.dao.BookMapper"/>
<!-- 指定sqlSessionFactory對象 -->
<property name="sqlSessionFactory" ref="sessionFactory"/>
</bean>
- 第二種方式 : 配置包掃描器
1.定義Dao接口
2.定義mapper的xml文件, 文件名必須是接口名
3.mapper的xml中的namespace必須是接口的全類名
4.在spring配置文件中加入內(nèi)容:
<!-- 第二種方式 -->
<!-- 配置包掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 如果要掃描多個包, 可以使用逗號隔開 -->
<property name="basePackage" value="com.gongxm.dao"/>
</bean>