MyBatis Study Notes (i)--aliases in configuration files and namespace in Mapper

Source: Internet
Author: User
Tags aliases

In MyBatis, if it is too unfriendly to write a full name for each configuration class, we can no longer need to specify the complete package name by configuring the alias in the master configuration file.

Basic usage of aliases:

<configuration>
    <typeAliases>
      <typealias type= "com.domain.Student" alias= "Student"/>
  </typeAliases> ...
</configuration>
But if every entity class is a bit of a hassle, then we can specify the package name directly, MyBatis will automatically scan the JavaBean under the specified packet, and the default setting is an alias, the default name is: JavaBean An unqualified class name with the first letter lowercase as its alias (in fact, the alias is not case-insensitive). You can also customize aliases in JavaBean plus annotations @alias, for example: @Alias (Student)
<typeAliases>
    <package name= "Com.domain"/>
</typeAliases>

Thus, in Mapper we don't have to write the full name of the class every time we configure it, but with one exception, that is namespace.


Namespace Property

In MyBatis, the namespace in mapper is used to bind the DAO interface, which is interface-oriented programming.

The advantage of this is that when the namespace is used, the interface implementation class can not be written, and the business logic will find the corresponding SQL statement for the corresponding data processing directly through this binding.

Student = (student) session.selectone ("Com.domain.Student.selectById", New Integer (10));

<mapper namespace= "Com.domain.Student" >  

    <select id= "Selectbyid" parametertype= "int" resulttype= " Student ">  
       select * from student where Id=#{id}  
    </select>
     
</mapper>  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.