1.回复MyBatis使用
1.导入MyBatis的Jar包。及连接数据库的驱动。
2.添加主配置文件,文件名建议是 mybatis-config.xml
属性文件,自身配置,别名,环境(事务,数据源),映射文件加载
3.添加映射文件,映射封装了操作数据库的SQL指令,并对查询结果进行封装映射。
4.通过MyBatis中的相关API来操作数据。
SqlSessionFactoryBuilder : 此对象加载主配置文件,并创建SqlSessionFactory工厂。工厂只需要创建一个就行。所以通常做成单例模式。再通过SqlSessionFactory工厂来创建SqlSession对象。此对象就是用来操作数据的对象。调用此对象的getMapper()方法获取持久层接口的代理对象,再执行代理对象的方法。注意用SqlSession对象操作数据时,增,删,改的操作必须要进行事务的提交或回滚,结束后还需要释放这个资源。
2.1MyBatis与Spring要改变的地方
1.MyBatis的相关API对象由Spring来创建
2.数据源的处理,以前是在Mybatis的配置文件中配置,现在要用另一种连接池技术,所以,数据源对象也要在Spring中进行配置。
2.2SqlSessionFactoryBean 对象
这个对象是MyBites与Spring整合时需要的对象它负责SqlSessionFactoryBuilder 对象与SqlSessionFactory对象的处理。这个类在mybatis-spring-1.2.0.jar包下。所以需要引用这个包。因为Spring没有做对Mybatis的支持,这个包是MyBatis自己做的,来对Spring框架的溶合。
4.数据源的处理,以前是在Mybatis的配置文件中配置,现在要用另一种连接池技术,所以,数据源对象也要在Spring中进行配置。
3.1SqlSessionFactoryBean 对象
这个对象是MyBites与Spring整合时需要的对象它负责SqlSessionFactoryBuilder 对象与SqlSessionFactory对象的处理。这个类在mybatis-spring-1.2.0.jar包下。所以需要引用这个包。因为Spring没有做对Mybatis的支持,这个包是MyBatis自己做的,来对Spring框架的溶合。
3.2添加MyBatis与Spring的配置文件,及映射文件,映射文件可以与数据访问接口分别存放在不同的目录下,方便管理。
3.2.1MyBatis的配置文件
3.2.2Spring的配置文件 配置数据源
<!-- 这个配置是把属性文件加载读取到 -->
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/database.properties" />
</bean>
<!-- 配置数据源对象,应用dbcp连接池技术 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 连接数据库的配置 -->
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<!-- 连接池的配置 不配置,它其实是有默认值的-->
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="maxWait" value="${maxWait}"/>
</bean>
3.2.3配置SqlSessionFactoryBean
<!-- 这个配置是把属性文件加载读取到 -->
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/database.properties" />
</bean>
<!-- 配置数据源对象,应用dbcp连接池技术 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 连接数据库的配置 -->
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<!-- 连接池的配置 不配置,它其实是有默认值的-->
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="maxWait" value="${maxWait}"/>
</bean>
3.3数据操作 数据操作方式有三种:
##3.1 SqlSessionTemplate对象
SqlSessionTemplate对象是一个类似于以前的SqlSession的对象,封装了一系列的方法,我们可以直接利用这个对象来操作数据。
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
提供操作数据的方法如下:
sqlSessionTemplate.selectList("com.bdqn.dao.GradeDao.find");
sqlSessionTemplate.selectOne(statement); //查询单个数据
sqlSessionTemplate.insert(statement); //增删改操作,且不需要提交事务
sqlSessionTemplate.update(statement);
sqlSessionTemplate.delete(statement);
业务层代码如下:
package com.bdqn.service;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bdqn.entity.Grade;
@Service //
public class GradeService implements IGradeService {
//自动注入数据操作对象 SqlSessionTemplate
@Autowired
SqlSessionTemplate sqlSessionTemplate;
@Override
public List getgrGrades(){
//selectList里面的参数是接口层的方法路径
List list = sqlSessionTemplate.selectList(“com.bdqn.dao.GradeDao.find”);
return list;
}
@Override
public int add(Grade grade) {
return sqlSessionTemplate.insert(“com.bdqn.dao.GradeDao.add”,grade);
}
}