MyBatis集合Spring(三)之mapper

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

public interface StudentMapper
{
@Select("select stud_id as studId, name, email, phone from
students 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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring
http://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
{
@Bean
public DataSource dataSource() {
return new PooledDataSource("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/elearning", "root", "admin");
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBeansessionFactory = new
SqlSessionFactoryBean();
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的接口。注意是,只有是接口的将会被扫描注册,如果是具体的类将会被忽略。







  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值