MyBatis学习笔记(一)——配置文件中的别名以及mapper中的namespace
2017-04-25 18:14
621 查看
MyBatis中如果每次配置类名都要写全称也太不友好了,我们可以通过在主配置文件中配置别名,就不再需要指定完整的包名了。
别名的基本用法:
[html] view
plain copy
<configuration>
<typeAliases>
<typeAlias type="com.domain.Student" alias="Student"/>
</typeAliases>
......
</configuration>
但是如果每一个实体类都这样配置还是有点麻烦这时我们可以直接指定package的名字, mybatis会自动扫描指定包下面的javabean,并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名(其实别名是不去分大小写的)。也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(student)
[html] view
plain copy
<typeAliases>
<package name="com.domain"/>
</typeAliases>
这样,在Mapper中我们就不用每次配置都写类的全名了,但是有一个例外,那就是namespace。
namespace属性
在MyBatis中,Mapper中的namespace用于绑定Dao接口的,即面向接口编程。
它的好处在于当使用了namespace之后就可以不用写接口实现类,业务逻辑会直接通过这个绑定寻找到相对应的SQL语句进行对应的数据处理
[java] view
plain copy
student = (Student) session.selectOne("com.domain.Student.selectById", new Integer(10));
[java] view
plain copy
<mapper namespace="com.domain.Student">
<select id="selectById" parameterType="int" resultType="student">
select * from student where id=#{id}
</select>
</mapper>
别名的基本用法:
[html] view
plain copy
<configuration>
<typeAliases>
<typeAlias type="com.domain.Student" alias="Student"/>
</typeAliases>
......
</configuration>
但是如果每一个实体类都这样配置还是有点麻烦这时我们可以直接指定package的名字, mybatis会自动扫描指定包下面的javabean,并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名(其实别名是不去分大小写的)。也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(student)
[html] view
plain copy
<typeAliases>
<package name="com.domain"/>
</typeAliases>
这样,在Mapper中我们就不用每次配置都写类的全名了,但是有一个例外,那就是namespace。
namespace属性
在MyBatis中,Mapper中的namespace用于绑定Dao接口的,即面向接口编程。
它的好处在于当使用了namespace之后就可以不用写接口实现类,业务逻辑会直接通过这个绑定寻找到相对应的SQL语句进行对应的数据处理
[java] view
plain copy
student = (Student) session.selectOne("com.domain.Student.selectById", new Integer(10));
[java] view
plain copy
<mapper namespace="com.domain.Student">
<select id="selectById" parameterType="int" resultType="student">
select * from student where id=#{id}
</select>
</mapper>
相关文章推荐
- Mybatis 源码学习笔记(八)mapper映射文件配置之select、resultMap
- mybatis学习笔记--mapper配置文件中大于小于怎么处理
- MyBatis学习笔记(一)——配置文件中的别名以及mapper中的namespace
- 配置文件中的别名以及mapper中的namespace
- MyBatis学习笔记(一)——配置文件中的别名以及mapper中的namespace
- Mybitis配置文件中的别名以及mapper中的namespace
- mybatis配置文件resultType和resultMap的区别以及mybatis自带的别名
- caffe学习笔记6:Blob,layer and net以及对应配置文件的编写
- mybatis学习笔记--配置文件+注解完整案例
- MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- mybatis学习笔记——Xml配置文件
- springboot学习笔记-2 一些常用的配置以及整合mybatis
- Mybatis学习笔记-CURD(基于配置文件的方式)
- Struts 2 学习笔记 - 3.配置文件深入(1):struts.xml,bean,constant,package,namespace,include
- MyBatis MapperScannerConfigurer配置——MyBatis学习笔记之八 推荐
- 电商平台学习笔记(四)——Spring配置Mybatis简化DAO层省略Mybatis核心配置文件
- (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- MyBatis学习笔记(三)——优化MyBatis配置文件中的配置
- Mybatis学习笔记(二)配置文件
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma