1. 程式人生 > >spring和mybatis的整合開發(傳統Dao開發方式)

spring和mybatis的整合開發(傳統Dao開發方式)

size lac 數據庫驅動 xxx 實現 init imp 找到 sele

spring和mybatis整合開發有三種整合方式1.傳統DAO方式的開發整合(現在基本上不會用這種方式了,不推薦使用這種方式),2.mapper接口方式的開發整合(基於MapperFactoryBean的整合和基於MapperScannerConfigurer的整合)

mybatis和spring的開發整合環境:

1.mybatis核心jar包和解壓過後lib目錄下的所有jar包

2.spring核心jar包,以及aop開發要用到的jar包,關於事務的jar包,jdbc jar包,這些jar包都在下載的spring框架包中能找到

3.日誌jar包,數據庫驅動jar包,DBCP數據源jar包,以及連接池jar包,junit測試jar包

4.spring和mybatis整合的中間件,mybatis-spring jar包

整合所需要的配置文件

1.mybatis-config.xml 2.applicationContext.xml 3.db.properties 4.log4j.properties 5.xxxMapper.xml

傳統DAO方式的開發整合

采用傳統DAO方式的開發整合需要編寫DAO接口以及接口的實現類 ,並且需要向DAO實現類中註入SqlSessionFactory,然後在方法體內通過SqlSessionFactory創建sqlsession。所以可以使用mybatis-spring包中的SqlSessionTemplate類和SqlSessionDaoSupport類來實現。

SqlSessionTemplate類是mybatis-spring的核心類,他負責管理mybatis的sqlsession,調用mybatis的sql方法。當調用sql方法時,SqlSessionTemplate將會保證使用的SqlSession和當前的spring的事務是相關的。並且他還管理bean的生命周期,包含必要的關閉,提交和回滾等。

SqlSessionDaoSupport類:是一個抽象支持類,他繼承了DaoSupport類,主要是作為DAO的基類來使用。可以通過SqlSessionDaoSupport類的getSqlSession()方法來獲取需要的SqlSession。

例如

/*客戶持久化類*/

public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
  setter/getter......
   @override
    toString()
}

CustomerMapper映射文件
根據id查找客戶
<mapper namespace="com.itheima.po.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
</mapper>
記得在mybatis-config.xml中配置mapper的路徑

接口CustomerDao
public interface CustomerDao {
public Customer findCustomerById(Integer id);
}

接口實現類CustomerDaoImpl
CustomerDaoImpl需要先繼承SqlSessionDaoSupport類,然後實現CustomerDao接口
public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {
@Override
public Customer findCustomerById(Integer id) {
return this.getSqlSession().selectOne("com.itheima.po.CustomerMapper.findCustomerById", id);
}

}
在applicationContext.xml中實例化customerDao
<!--實例化dao-->
<bean id="customerDao" class="com.itheima.po.dao.impl.CustomerDaoImpl">
<!--註入sqlsessionFactory對象實例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
測試
public class FindCustomerByIdTest {
@Test
public void findCustomerById(){
ApplicationContext applicationContex = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerDao customerDao=applicationContex.getBean(CustomerDao.class);
Customer customer=customerDao.findCustomerById(2);
System.out.println(customer);
}
}
測試結果
Customer{id=2, username=‘chen‘, jobs=‘java‘, phone=‘456789‘}
application.xml中完整配置
<!--讀取db.properties配置信息-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置數據源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!--數據驅動-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--連接數據庫的url地址-->
<property name="url" value="${jdbc.url}"/>
<!--連接數據庫的用戶名-->
<property name="username" value="${jdbc.username}"/>
<!--連接數據庫的密碼-->
<property name="password" value="${jdbc.password}"/>
<!--最大連接數-->
<property name="maxTotal" value="${jdbc.maxTotal}"/>
<!--最大空閑連接-->
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!--初始化連接數-->
<property name="initialSize" value="${jdbc.initialSize}"/>
</bean>
<!--事務管理器,依賴於數據源-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--開啟事務註解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--配置Mybatis工廠-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--註入數據源-->
<property name="dataSource" ref="dataSource"/>
<!--指定核心配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--實例化dao-->
<bean id="customerDao" class="com.itheima.po.dao.impl.CustomerDaoImpl">
<!--註入sqlsessionFactory對象實例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

spring和mybatis的整合開發(傳統Dao開發方式)