一、基本介紹
所謂的Spring整合Mybatis其實(shí)說(shuō)白了就是將mybatis的加載過(guò)程全權(quán)交給Spring托管,不再需要加載配置工具等一些操作,而具體的dao層操作依舊是使用mybatis去操作數(shù)據(jù)庫(kù)。
1.1 mybatis使用步驟:
1.首先要寫一個(gè)mybatis-config.xml核心配置文件,配置基本的環(huán)境支持:數(shù)據(jù)源、驅(qū)動(dòng)、url、username、password…
2.然后編寫mybatisUtil工具類,先以IO流的形式加載mybatis-config.xml資源Resource,然后通過(guò)SqlSessionFactoryBuilder工廠建造者創(chuàng)建一個(gè)SqlSessionFactory工廠,并且將SqlSessionFactory工廠封裝為單例工廠;最后對(duì)外只拋出一個(gè)SqlSession會(huì)話獲取接口!
3.最后dao層通過(guò)SqlSession獲取Mapper映射器進(jìn)行SQL的執(zhí)行!
1.2 Spring整合mybatis步驟:
首先將mybatis-config.xml環(huán)境的配置:數(shù)據(jù)源、驅(qū)動(dòng)、url、username、password…這些基礎(chǔ)配置移交給Spring的核心配置文件application.xml文件中!創(chuàng)建bean對(duì)象dataSource頂替Resource。
再通過(guò)dataSource對(duì)象,創(chuàng)建bean對(duì)象SqlSessionFactory,這時(shí)候SqlSessionFactory工廠就交給Spring托管了?。ńㄗh按照mybatis的習(xí)俗配置成單例)
然后將mybatis-config.xml配置文件中的Configration配置以依賴注入的形式注入到SqlSessionFactory,mybatis-config.xml中的所有東西就可以在Spring中生效了。
最后將SqlSession的獲取也交給Spring托管,以構(gòu)造器注入的形式將SqlSessionFactory工廠注入SqlSession的依賴中!
之后所有的dao層操作都通過(guò)getBean的形式獲取SqlSession,然后在執(zhí)行SQL;對(duì)比上面mybatis使用步驟,整合基本上就是將mybatis的加載全權(quán)交由Spring掌管,僅此而已。
二、mybatis核心配置
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 開(kāi)啟日志 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- 開(kāi)啟二級(jí)緩存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 將經(jīng)典的SQL字段映射為Java駝峰命名變量-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 別名 -->
<typeAliases>
<typeAlias type="com.pojo.User" alias="User"/>
</typeAliases>
<!-- 映射注冊(cè) -->
<mappers>
<mapper class="com.dao.UserMapper"/>
</mappers>
</configuration>
三、Spring核心配置
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.dao"/>
<context:annotation-config/>
<!-- 1.將mybatis配置環(huán)境集成到spring中,交由Spring托管 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 2. 將SqlSessionFactory交給Spring托管 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="singleton">
<property name="dataSource" ref="dataSource"/> <!-- 加載數(shù)據(jù)環(huán)境 -->
<!-- 綁定mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 幾乎所有的東西都能在這里面配,完全不需要mybatis的核心配置
根據(jù)個(gè)人喜好,個(gè)人感覺(jué)分開(kāi)一點(diǎn)維護(hù)比較容易
-->
</bean>
<!-- 3. 將SqlSession對(duì)象的加載交給Spring托管 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 按照mybatis的習(xí)俗,通過(guò)工廠獲得SqlSession會(huì)話對(duì)象 -->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
四、數(shù)據(jù)庫(kù)與對(duì)應(yīng)的POJO實(shí)體類
package com.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private int id;
private String username;
private String password;
}
五、dao層
5.1 UserMapper接口
package com.dao;
import com.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> getUserList();
}
5.2 UserMapper.xml配置
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.UserMapper">
<!-- 解決字段-屬性不匹配問(wèn)題 -->
<resultMap id="map" type="User">
<id column="id" property="id"/>
<id column="name" property="username"/>
<id column="pwd" property="password"/>
</resultMap>
<select id="getUserList" resultMap="map">
select *from user;
</select>
</mapper>
5.3 接口實(shí)現(xiàn)類(一)
單獨(dú)使用mybatis時(shí)是不需要這個(gè)實(shí)現(xiàn)類的,將impl實(shí)現(xiàn)類做的操作數(shù)據(jù)庫(kù)事情移交到service層執(zhí)行。而現(xiàn)在需要這個(gè)實(shí)現(xiàn)類,內(nèi)部聚合SqlSessionTemplate對(duì)象(等價(jià)于SqlSession),然后在實(shí)現(xiàn)方法中通過(guò)反射獲取mapper,然后執(zhí)行SQL即可!
import com.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("userMapperImpl")
public class UserMapperImpl implements UserMapper{
@Autowired
private SqlSessionTemplate sqlSession;
@Override
public List<User> getUserList() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> list = mapper.getUserList();
return list;
}
}
5.4 接口實(shí)現(xiàn)類(二)
在Spring - Mybatis最新的整合中提供了SqlSessionDaoSupport抽象類來(lái)取代內(nèi)部聚合SqlSessionTemplate對(duì)象,只需要繼承這個(gè)類即可。雖然明面上不需要依賴注入,但是父類是需要依賴注入一個(gè)SqlSessionFactory工廠的!
import com.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> getUserList() {
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> list = mapper.getUserList();
return list;
}
}
<bean id="userMapperImpl2" class="com.dao.UserMapperImpl2">
<!-- SqlSessionDaoSupport抽象類需要注入依賴sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
六、測(cè)試
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
UserMapper userMapperImpl = context.getBean("userMapperImpl", UserMapper.class);
List<User> list = userMapperImpl.getUserList();
for (User user : list) {
System.out.println(user.toString());
}
}
到此這篇關(guān)于如何把Mybatis整合到Spring中的文章就介紹到這了,想要了解更多相關(guān)Spring和Mybatis內(nèi)容,請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,也希望大家以后多多支持!