在咱們使用mybatis的時候,先來大致講一下mybatis的一些東西。以及以及基本的類。有SqlSessionFactory.SqlSession.在咱們不適用spring的時候。咱們本身使用mybatis。進行操做數據的時候,這兩個類是必須的。下面是沒有使用spring的狀況下。直接進行操做數據庫的地方。以下:java
private static SqlSessionFactory sqlSessionFactory; private static Reader reader; private static SqlSession session; private static TestMapper testMapper; static { try { reader = Resources.getResourceAsReader("sqlTest/myBatis.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); session = sqlSessionFactory.openSession(); testMapper = session.getMapper(TestMapper.class); } catch (IOException e) { e.printStackTrace(); } } public void inserUSer(){ testMapper.insertTest(test); session.commit(); }
首先知道SqlSession是如何使用的。而後咱們來進行Spring和Mybatis的整合。本人使用的是全註解的方式。只貼出相關的整合的代碼。Spring的配置地方不進行代碼貼出。git
以下:github
<!-- spring 整合 mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> <property name="mapperLocations" value="classpath:sqlmap/*.xml"/> <!-- mapper和resultmap配置路徑 --> <!--<property name="mapperLocations"> <list> <value>classpath:sqlmap/TUserMapper.xml</value> </list> </property>--> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.inga.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
上面的mapperLocations的地方,能夠使用上面兩種方式其中的一種。下面是sqlMapConfig.xml的源碼。spring
分頁插件的地方。能夠本身選擇使用。這個分頁插件是在github上面找的一個插件。sql
<?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> <typeAliases> <typeAlias type="com.inga.bean.TUser" alias="TUser"/> </typeAliases> <!-- 分頁插件 --> <plugins> <plugin interceptor="com.inga.utils.page.utils.MybatisPageableInterceptor"> <!-- <property name="dialectClass" value="com.inga.utils.page.utils.MySQLDialect"/> --> <property name="dialect" value="MYSQL"/> </plugin> </plugins> </configuration>
上面配置好之後。就能夠使用了。還有一種註解的方式。
數據庫
配置只是把上面的地方進行了修改。只貼出修改的地方的代碼。session
<!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.inga.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="annotationClass" value="com.inga.utils.mybatis.MyBatisDao"/> </bean>
下面是MyBatisDao的源碼。mybatis
package com.inga.utils.mybatis; import org.springframework.stereotype.Component; import java.lang.annotation.*; /** * Created by abing on 2015/11/18. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Component public @interface MyBatisDao { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any */ String value() default ""; }
查看mybatis的源碼。知道有這個方式。在jeesite中看到了以下使用方式。進行了測試。徹底能夠使用。app
具體的項目源碼在oscchina中有。測試