先看一下項目圖,基本就理解了整合的內容 這次主角不再是Mybats的配置文件SqlMapConfig.xml了,而是Spring的applicationContext.xml applicationContext.xml Spring同樣開始整合數據源datasource IoC控制反轉生成sqlS ...
先看一下項目圖,基本就理解了整合的內容
這次主角不再是Mybats的配置文件SqlMapConfig.xml了,而是Spring的applicationContext.xml
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <context:property-placeholder location="classpath:db.properties" /> <!-- 數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:SqlMapConfig.xml"></property> <property name="typeAliasesPackage" value="com.itheima.mybatis.pojo"></property> </bean> <bean id="userDao" class="com.itheima.mybatis.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
Spring同樣開始整合數據源datasource
IoC控制反轉生成sqlSessionFactory對象,註意有一個屬性configLocation,用來關聯Mybatis的映射文件SqlMapConfig.xml,
也是在該對象中對包進行別名操作
IoC控制反轉生成要調用的對象userDao,其實現類繼承SqlSessionDaoSupport,還要註入屬性sqlSessionFactory
Mybatis配置文件SqlMapConfig.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> <!-- 載入sql映射文件 --> <mappers> <mapper resource="sqlmap/UserMapper.xml" /> </mappers> </configuration>
映射文件UserMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 映射,存放sql語句 --> <!-- namespace:分類管理sql語句 唯一確定sql語句:namespace + "." + id --> <mapper namespace="test"> <!-- 查詢使用select標簽 id:sql語句標識符,通常也稱為statement的id parameterType:參數類型(輸入映射) resultType:結果類型(輸出映射),配置為pojo,那麼mybatis就把結果集中的每一條記錄封裝為指定的pojo --> <select id="queryUserById" parameterType="Integer" resultType="user"> <!-- #{}固定取參語法 #{}取參名稱:parameterType為基礎數據類型的時候,名稱任意,建議見名思意 傳 入pojo的時候,取參名稱為屬性名 #{}原理:預編譯語句,可以防止sql註入 #{}對字元串參數會自動添加單引號對 --> select * from user where uid=#{uid} </select> </mapper>
介面
package com.itheima.mybatis.dao; import com.itheima.mybatis.pojo.User; public interface UserDao { User queryUserById(int id); }
實體類
package com.itheima.mybatis.dao; import org.mybatis.spring.support.SqlSessionDaoSupport; import com.itheima.mybatis.pojo.User; public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { public User queryUserById(int id) { // TODO Auto-generated method stub // 不需要關閉session,因為父類當中完成攔截關閉動作 User user = super.getSqlSession().selectOne("test.queryUserById", id); return user; } }
測試類
package com.itheima.mybatis.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.transaction.annotation.Transactional; import com.itheima.mybatis.dao.UserDao; import com.itheima.mybatis.pojo.User; public class TestUserDao { private ApplicationContext ctx; @Test @Transactional public void testQueryUserById() { ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = ctx.getBean(UserDao.class); User user = userDao.queryUserById(3); System.out.println(user); } }