1 加入jar包
mybatis-3.2.0.jar
mybatis-spring-1.2.3.jar
2 在applicationContext.xml中配置
<!-- Spring集成MyBatis -->
<!-- 1.数据源 tomcat.jdbc.pool.DataSource -->
<!-- 导入资源文件 -->
<bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="locations">
<list>
<value>/WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>
<!--配置数据源 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="poolProperties">
<bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 数据库连接池配置 -->
<!-- 初始化连接数量 -->
<property name="initialSize" value="30"/>
<!-- 最大连接数量 -->
<property name="maxActive" value="300"/>
<!-- 最小空闲连接数量 -->
<property name="minIdle" value="30"/>
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="600" />
<!-- 验证连接是否有效 -->
<property name="validationQuery" value="select 1" />
<!-- 验证失败时,是否将连接从池中丢弃 -->
<property name="testWhileIdle" value="true" />
<!-- 把空闲时间超过minEvictableIdleTimeMillis毫秒的连接断开,直到连接池中的连接数到minIdle为止(毫秒,30分钟) -->
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<!-- 连接池中连接可空闲的时间(毫秒,5分钟) -->
<property name="minEvictableIdleTimeMillis" value="18000000" />
<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="5" />
<property name="testOnBorrow" value="true" />
</bean>
</property>
</bean>
<!-- 2.mybatis的SqlSession工厂:SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 配置mapper的位置-->
<property name="mapperLocations">
<list>
<value>classpath:com/ithings/mapper/*.xml</value>
</list>
</property>
</bean>
<!-- 3.sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 4. mybatis 自动扫描加载Sql 映射文件 :MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ithings.dao;" />
<property name="sqlSessionTemplateBeanName" value="sqlSession"/>
</bean>
注1:整合过程中出现的错误记录
1.报错:Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfig解决:两个jar包的版本过低
2.报错:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
解决:需要将mysql-connector-java-5.1.7-bin.jar放入tomcat/lib目录下
注2:加载数据库连接配置文件
方法一 context:property-placeholder
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- or -->
<context:property-placeholder location="/WEB-INF/jdbc.properties"/>
方法二:使用PropertyPlaceholderConfigurer
</pre><p><pre name="code" class="html">
//1.使用PropertyPlaceholderConfigurer
<bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="locations">
<list>
<value>/WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>
//2.PropertyPlaceholderConfigurer不仅能从属性文件中获取值,也能从系统属性,甚至是环境变量中取值。
如:
System.setProperty("jdbc.driverClassName", "com.mysql.jdbc.Driver");
System.setProperty("jdbc.url", "jdbc:mysql://localhost:3306/test");
System.setProperty("jdbc.username", "root");
System.setProperty("jdbc.password", "123");
<bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
//3. PropertyPlaceholderConfigurer 来读取属性文件,有两个子类:
//org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer
//org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>