基于maven 管理的工程
1..加入以下jar包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId></dependency>
2.导入数据库连接jar包
<pre name="code" class="html"><dependency>
<span style="white-space:pre"> </span><groupId>mysql</groupId>
<span style="white-space:pre"> </span><artifactId>mysql-connector-java</artifactId>
</dependency>
3.配置文件mybatis.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package= "项目的包名" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "location" value="数据库配置文件所在路径" />
</bean >
<!--数据库配置-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" >
<property name= "driverClassName" value="${driver}" />
<property name= "url" value ="${url}" />
<property name= "username" value ="${username}" />
<property name= "password" value ="${password}" />
<!-- 初始化连接大小 -->
<property name= "initialSize" value="${initialSize}" ></property>
<!-- 连接池最大数量 -->
<property name= "maxActive" value="${maxActive}" ></property>
<!-- 连接池最大空闲 -->
<property name= "maxIdle" value="${maxIdle}" ></property>
<!-- 连接池最小空闲 -->
<property name= "minIdle" value="${minIdle}" ></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean >
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "dataSource" ref ="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name= "mapperLocations" value="mapper.xml文件存放路径" ></property>
</bean >
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "basePackage" value="dao层接口所在位置" />
<property name= "sqlSessionFactoryBeanName" value="sqlSessionFactory" ></property>
</bean>
</beans>
4.将mybatis.xml配置文件配置到web.xml中即可
至此:mybatis的基本配置就完成了