mybatis在项目中的使用

    xiaoxiao2021-03-26  28

    在spring web项目中,使用mybatis,首先参见applicationContext.xml文件,基本配置都在其中。

    其中,sqlSessionTemplate和sqlSessionFactory配置了mybatis具体设置。

    <!-- spring与MyBatis的配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > <property name="dataSource" ref="dataSource"/> <!-- 配置扫描实体类的包路径 --> <property name="typeAliasesPackage" value="com.temp.mybatis.pojo"/> <!-- 配置mybatis配置文件的位置 --> <property name="configLocation" value="classpath:/springconfig/mybatis-config.xml"/> </bean> 关于dataSource的配置稍后讨论。

    首先可以看到,配置实体类的别名扫描,mybatis可自动扫描包下面的实体类,将类名作为别名可在mapper.xml文件中直接使用。

    mybatis配置文件中主要是mybatis的性能、插件等自身配置。

    关于dataSource配置:

    <bean id="dataSource" class="com.temp.database.datasource.DataSourceDelegating"> <property name="defaultDBLabel" value="main_db"/> </bean>主要是在class中进行数据库的包装,可实现多数据源。

    对mybatis的mapper类进行自动搜索,使用注解自动注入:

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.temp.mybatis.mapper" /> </bean>

    通过@Repository即可进行注解注入。使用的是SqlSessionFactoryBean工厂类。

    注 意 , 没 有 必 要 去 指 定 SqlSessionFactory 或 SqlSessionTemplate , 因 为 MapperScannerConfigurer 将会创建 MapperFactoryBean,之后自动装配。但是,如果你使 用了一个 以上的 DataSource ,那 么自动 装配可 能会失效 。这种 情况下 ,你可 以使用 sqlSessionFactoryBeanName 或 sqlSessionTemplateBeanName 属性来设置正确的 bean 名 称来使用。这就是它如何来配置的,注意 bean 的名称是必须的,而不是 bean 的引用,因 此,value 属性在这里替代通常的 ref: <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> MapperScannerConfigurer 支 持 过 滤 由 指 定 的 创 建 接 口 或 注 解 创 建 映 射 器 。 annotationClass 属性指定了要寻找的注解名称。 markerInterface 属性指定了要寻找的父 接口。如果两者都被指定了,加入到接口中的映射器会匹配两种标准。默认情况下,这两个 属性都是 null,所以在基包中给定的所有接口可以作为映射器加载。

    此外,还可通过其他方式实现。

    使用mybatis进行数据处理的四种方式(SqlSessionTemplate/SqlSessionDaoSupport/MapperFactoryBean/MapperScannerConfigurer 不同方式的特点SqlSessionTemplate 这个需要写配置文件,在实现类中注入sqlsession,再使用sqlsession,是细颗粒控制SqlSessionDaoSupport 这个只需要在实现类中继承特殊类就可以使用sqlsessionMapperFactoryBean 这个要写配置文件,把对应的所有接口在配置文件中引用即可,无需写实现类MapperScannerConfigurer 这个要写配置文件,只要给出接口所在的包即可,会自动把包中的接口引入,无需写实现类

    使用SqlSessionTemplate:配置文件加入新配:<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> <constructor-arg index="1" value="BATCH" /><!--- 如果想要进行批量操作可加入这个属性 -></bean>注入sqlsession():@Reasource //使用spring3的注解注入private SqlSession sqlSession;使用sqlsession来进行操作:public User getUser(String userId) { return (User) sqlSession.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId); }使用SqlSessionDaoSupport(sqlSessionFactory会被spring自动装配,不需要手动注入):继承SqlSessionDaoSupport类:public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {}使用getSqlSession()方法取sqlSession来进行数据处理:public User getUser(String userId) { return (User) getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId); }使用MapperFactoryBean:写配置文件,引入每个DAO接口:<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>在业务层可直接注入dao的接口进行操作使用MapperScannerConfigurer:写配置文件,配置包名将自动引入包中的所有接口<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.mybatis.spring.sample.mapper" /></bean>在业务层可直接注入DAO接口操作,注入时使用的是接口名,其首字母小写注意:如果有别的实现类,其提供的名称如果是接口名,且首字母小写,则会在启动时出现冲突错误

    转载请注明原文地址: https://ju.6miu.com/read-664120.html

    最新回复(0)