编程知识 cdmana.com

Mybatis typealiases element label (including annotation method) and its attributes and settings

Personal suggestion is to use the full class name , In this way, you can easily see all the methods of this class , It's more convenient and intuitive ; But there are disadvantages , Not conducive to maintenance, etc .

brief introduction

typeAliases: Alias processor , It can be for java type (resultType) names . Type alias is for Java Type to set a short name . It's only with XML Configure the , It exists only to reduce the redundancy of fully qualified names of classes .

Be careful : Without specifying an alias , By default, the alias is the class name in lowercase . In some cases, aliases are not case sensitive .

By defining an alias individually

grammar

<typeAliases>
<!--
    alias: Alias defined 
    type: Specifies the full class name of the type to be aliased 
     The default alias is lowercase 
-->
  <typeAlias alias=" Alias " type=" Specifies the full class name of the type to be aliased "/>
 
</typeAliases>
      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

for example

<typeAliases>
<!--
    alias: Alias defined 
    type: Specifies the full class name of the type to be aliased 
     The default alias is lowercase 
-->
  <typeAlias alias="Author" type="domain.blog.Author"/>
  <typeAlias alias="Blog" type="domain.blog.Blog"/>
  <typeAlias alias="Comment" type="domain.blog.Comment"/>
  <typeAlias alias="Post" type="domain.blog.Post"/>
  <typeAlias alias="Section" type="domain.blog.Section"/>
  <typeAlias alias="Tag" type="domain.blog.Tag"/>
</typeAliases>
      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

When configured like this ,Blog It can be used in any application domain.blog.Blog The place of .

By packet scanning

This method will scan all classes under the package , The default is lowercase of the class name .

Be careful : If there is a sub package under the package , And the sub package has the same class name as the package , You need to annotate one of the classes , Otherwise an error will be reported ( Conflict ), because mybatis I don't know which bag it is , It must be clearly indicated before .

grammar

<typeAliases>
  <package name=" Package name "/>
</typeAliases>
      
  • 1.
  • 2.
  • 3.

for example

<typeAliases>
  <package name="domain.blog"/>
</typeAliases>
      
  • 1.
  • 2.
  • 3.

By way of annotation

Every one in the bag  domain.blog  Medium Java Bean, In the absence of annotations , Will use Bean The first and lowercase unqualified class name is used as its alias . such as  domain.blog.Author  Its alias is  author; If you have any notes , Is alias to its annotation value .

Example :

@Alias("author")
public class Author {
    ...
}
      
  • 1.
  • 2.
  • 3.
  • 4.

common Java The corresponding type alias built into the type

They are case insensitive , Note the special handling caused by duplicate base type names .

Alias Type of mapping
_byte byte
_long long
_short short
_int int
_integer int
_double double
_float float
_boolean boolean
string String
byte Byte
long Long
short Short
int Integer
integer Integer
double Double
float Float
boolean Boolean
date Date
decimal BigDecimal
bigdecimal BigDecimal
object Object
map Map
hashmap HashMap
list List
arraylist ArrayList
collection Collection
iterator Iterator

版权声明
本文为[Hern song zhaoheng]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/08/20210807084541939Q.html

Scroll to Top