简要的说一下mybatis整合spring的配置
首先导入几个比较重要的jar包
1.mybatis
2.mybatis-spring
在applicationContext.xml中配置,使得mybatis交给spring托管
<!-- 交易数据库 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${database.driver}"></property>
<property name="jdbcUrl" value="${database.url}"></property>
<property name="user" value="${database.username}"></property>
<property name="password" value="${database.password}"></property>
<property name="minPoolSize" value="${database.minimumConnectionCount}"></property>
<property name="maxPoolSize" value="${database.maximumConnectionCount}"></property>
<property name="maxIdleTime" value="${database.houseKeepingSleepTime}"></property>
</bean>
<bean id="sqlSessionFactory" name="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations">
<list>
<value>classpath:com/wuxing/learn/dao/impl/mappers/*Mapper.xml
</value>
</list>
</property>
</bean>
<bean id="sqlSessionFactoryTemplate" name="sqlSessionFactoryTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value=" com.wuxing.learn.mapper" />
<!-- spring 3.1.1 和mybatis 要用名字指定连接工厂 因为跟property-placeholder冲突 -->
<property name="sqlSessionTemplateBeanName" value="sqlSessionFactoryTemplate" />
</bean>
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
第一个bean标签是配置数据源..数据取了config配置文件中的值, 这个就不多说了.
第二个bean标签是配置sqlsessionfactory, 其中可以指定mybatis的配置文件以及mapperlocations,即mapper.xml文件的路径.
在一般的 MyBatis-Spring 用法中, 你不需要直接使用 SqlSessionFactoryBean 或和其对 应的 SqlSessionFactory。相反,session 工厂将会被注入到 MapperFactoryBean 或其它扩 展了 SqlSessionDaoSupport 的 DAO(Data Access Object,数据访问对象,译者注)中。
第三个bean标签配置了sqlsessionfactorytemplate, 从SqlSessionDaoSupport的源码中我们可以看到,最后的sqlsessionfactory一样会被封装成sqlsessiontemplate,所以两者其实效果是一样的.
/**
* Copyright 2010-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring.support;
import static org.springframework.util.Assert.notNull;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.support.DaoSupport;
/**
* Convenient super class for MyBatis SqlSession data access objects.
* It gives you access to the template which can then be used to execute SQL methods.
* <p>
* This class needs a SqlSessionTemplate or a SqlSessionFactory.
* If both are set the SqlSessionFactory will be ignored.
* <p>
* {code Autowired} was removed from setSqlSessionTemplate and setSqlSessionFactory
* in version 1.2.0.
*
* @author Putthibong Boonbong
*
* @see #setSqlSessionFactory
* @see #setSqlSessionTemplate
* @see SqlSessionTemplate
* @version $Id$
*/
public abstract class SqlSessionDaoSupport extends DaoSupport {
private SqlSession sqlSession;
private boolean externalSqlSession;
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
if (!this.externalSqlSession) {
this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
}
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSession = sqlSessionTemplate;
this.externalSqlSession = true;
}
/**
* Users should use this method to get a SqlSession to call its statement methods
* This is SqlSession is managed by spring. Users should not commit/rollback/close it
* because it will be automatically done.
*
* @return Spring managed thread safe SqlSession
*/
public SqlSession getSqlSession() {
return this.sqlSession;
}
/**
* {@inheritDoc}
*/
@Override
protected void checkDaoConfig() {
notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");
}
}
第四个bean标签定义了scanner,这样就可以以扫描的形式去找mapper的映射文件,而不需要一一指定.
最后是配置了事务交给spring控制.
其中mybatis-config.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>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
</configuration>
这边简单定义一个mapper接口
package com.wuxing.learn.mapper;
/**
* @author wuxing
* @date 2015年9月21日 下午3:47:38
*
*/
public interface GetDataMapper {
public String query(int id);
}
其中就一个方法,query!!
然后编写对应的mapper.xml,也是存放mybatis的sql的核心文件.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.wuxing.learn.mapper.GetDataMapper" >
<select id="query" parameterType="java.lang.Integer" resultType="java.lang.String">
select IMEI
from pay_mobile_info
where id = #{id}
</select>
</mapper>
通过namespace去指定mapper对应的接口.然后用sql语句查询.具体的使用就不多说了.
大致的配置就是这样..有些原理还不是特别懂...最近多看看源码理解下..