MyBatis集合Spring(三)之mapper

来源:互联网 发布:矩阵迹的性质及证明 编辑:程序博客网 时间:2024/05/16 12:38

我们可以配置Mapper接口作为Spring使用的MapperFactoryBean的Bean。

public interface StudentMapper{@Select("select stud_id as studId, name, email, phone fromstudents where stud_id=#{id}")Student findStudentById(Integer id);}<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="com.owen.mybatis.mappers.StudentMapper" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>
现在StudentMapper的Bean可以注入到任何的Spring bean中,和可以执行映射语句的方法如下:
public class StudentService{private StudentMapper studentMapper;public void setStudentMapper (StudentMapperstudentMapper){this. studentMapper = studentMapper;}public void createStudent(Student student){this.studentMapper.insertStudent(student);}}
<bean id="studentService" class="com.owen.mybatis.services.StudentService"><property name="studentMapper" ref="studentMapper" /></bean>

如果我们像上面那样配置,一个Mapper 接口配置一个的话,那么就会存在代码的冗余,所以我们可以使用MapperScannerConfigurer,来指定Mapper接口所在的包名就行了。

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

其实在MyBatis-Spring1.2.0就已经引进了新的扫描Mapper接口的方法。下面我们将会讲解。

1. <mybatis:scan/>

<mybatis:scan>元素将会去查找Mapper接口下的所有的类,如果是多个包名那么就要用逗号分开。为了运用这个标签,你需要在MyBatis-Spring的命名空间中添加如下的定义。

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://mybatis.org/schema/mybatis-springhttp://mybatis.org/schema/mybatis-spring.xsd"><mybatis:scan base-package="com.owen.mybatis.mappers" /></beans>

<mybatis:scan>元素提供了下面的属性,这些可以用于自定义的过程。

1)        annotation:这个方法,扫描器会去注册所有的接口,也可以指定接口作为父类。

2)        factory-ref:在指定使用SqlSessionFactory的情况下,这里有一个或多个的Spring的容器。经常我们会使用一个或多个的数据 库。

3)        marker-interface:这个扫描器将会注册所有的接口,也有指定有注解。

4)        template-ref: 在指定使用SqlSessionTemplate的情况下,这里有一个或多个的Spring的容器。经常我们会使用一个或多个的数据库。

5)        name-generator: 它的完全限定类名beannamegenerator用于命名所检测到的组件.

2.  @MapperScan

如果你习惯了使用基于Java的注解来配置,你可以应用@MapperScan注解去扫描所有的Mapper接口。@MapperScan工作的原理与<mybatis:scan/>是一样的。也提供了自定义的选择注解属性。

@Configuration@MapperScan("com.owen.mybatis.mappers")public class AppConfig{@Beanpublic DataSource dataSource() {return new PooledDataSource("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/elearning", "root", "admin");}@Beanpublic SqlSessionFactory sqlSessionFactory() throws Exception {SqlSessionFactoryBeansessionFactory = newSqlSessionFactoryBean();sessionFactory.setDataSource(dataSource());return sessionFactory.getObject();}}

这个@MapperScan注解有下面的自定义属性。

 

1)        annotationClass:这个是基于包下面扫描所有的接口类并注册,也有指定的属性。     

2)        markInterface:基于包下面扫描所有接口类并注册,也可以指定特殊的接口为父类。

3)   sqlSessionTemplateRef: 在指定使用sqlSessionFactoryRef的情况下,这里有一个或多个的Spring的容器。经常我们会使用一个或多个的数据库.

4)        sqlSessionTemplateRef: 在指定使用sqlSessionTemplateRef的情况下,这里有一个或多个的Spring的容器。经常我们会使用一个或多个的数据库.

5)        nameGenerator:在Spring的容器中,将使用BeanNameGenerator去命名检测到的组件。

6)        basePackageClasses:这是一个安全替代basePackages()作为指定组件的扫描包。包下面的所有接口都将会被扫描。

7)        basepackages:基于包下面的扫描MyBatis的接口。注意是,只有是接口的将会被扫描注册,如果是具体的类将会被忽略。







1 0
原创粉丝点击