所謂的Spring整合Mybatis其實說白了就是將mybatis的加載過程全權交給Spring託管,再也不須要加載配置工具等一些操做,而具體的dao層操做依舊是使用mybatis去操做數據庫。java
Java基礎知識大全mysql
從0到1Java學習路線和資料spring
<?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> <!-- 開啓日誌 --> <setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 開啓二級緩存 --> <setting name="cacheEnabled" value="true"/> <!-- 將經典的SQL字段映射爲Java駝峯命名變量--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 別名 --> <typeAliases> <typeAlias type="com.pojo.User" alias="User"/> </typeAliases> <!-- 映射註冊 --> <mappers> <mapper class="com.dao.UserMapper"/> </mappers> </configuration>
<?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配置環境集成到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"/> <!-- 加載數據環境 --> <!-- 綁定mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 幾乎全部的東西都能在這裏面配,徹底不須要mybatis的核心配置 根據我的喜愛,我的感受分開一點維護比較容易 --> </bean> <!-- 3. 將SqlSession對象的加載交給Spring託管 --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 按照mybatis的習俗,經過工廠得到SqlSession會話對象 --> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
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; }
package com.dao; import com.pojo.User; import java.util.List; public interface UserMapper { public List<User> getUserList(); }
<?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"> <!-- 解決字段-屬性不匹配問題 --> <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>
單獨使用mybatis時是不須要這個實現類的,將impl實現類作的操做數據庫事情移交到service層執行。而如今須要這個實現類,內部聚合SqlSessionTemplate對象(等價於SqlSession),而後在實現方法中經過反射獲取mapper,而後執行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; } }
在Spring - Mybatis最新的整合中提供了SqlSessionDaoSupport抽象類來取代內部聚合SqlSessionTemplate對象,只須要繼承這個類便可。雖然明面上不須要依賴注入,可是父類是須要依賴注入一個SqlSessionFactory工廠的!apache
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>
@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()); } }
以爲文章對你有幫助的話記得點個贊!緩存