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的接口。注意是,只有是接口的将会被扫描注册,如果是具体的类将会被忽略。
- MyBatis集合Spring(三)之mapper
- Mybatis之Spring整合Mapper代理
- MyBatis集合Spring(二)之SqlSession
- mybatis系列-Mapper XML 文件(三)
- mybatis整合spring(mapper+原始dao)
- MyBatis集合Spring(四)之使用Spring处理事务
- MyBatis集合Spring(四)之使用Spring处理事务
- mybatis学习三之mapper.xml开发方法
- (五)mybatis学习之mapper代理开发方式及与spring整合
- MyBatis之整合Spring(Dao和Mapper两种方式)
- MyBatis之整合Spring(Dao和Mapper两种方式)
- Spring 整合Mybatis mapper问题
- org.mybatis.spring.mapper.MapperScannerConfigurer
- Springmvc 与 MyBatis的整合(三)mapper
- MyBatis学习笔记(三)Mapper动态代理开发
- 06.Mybatis初级使用-Mapper映射器(三)
- mybatis中mapper配置方式进行查询(三)
- mybatis和spring整合开发(mapper代理)
- javascript-类定义-随
- Python 数据分析包:pandas 基础
- poj1144(求图中割点的个数)
- Ruby学习笔记
- 酒店管理系统_05-Servlet版本-mysql数据库-页面添加数据表
- MyBatis集合Spring(三)之mapper
- js获取子节点childNodes和children的区别
- 一个简单java程序的运行全过程
- 贪心算法——寻找剩余最大数
- 酒店管理系统_06_-Servlet版本-添加用户数据
- 基于BASE64原理编码解码实现-----C语言
- POJ 2104 K-th Number (划分树,主席树写过了,这次是整体二分解法 )
- LeetCode - 367. Valid Perfect Square
- 酒店管理系统_07-Servlet版本-用户列表和删除用户-EL表达式