Mybatis Typealiases (alias) in global configuration file

Source: Internet
Author: User

In a specific mapper.xml file, defining a lot of statement,statement requires parametertype specifying the type of input parameter, the type of mapping that needs to be resulttype to specify the output result.

If you enter a type full path when specifying a type, it is inconvenient to develop, you can define some aliases for the type specified by ParameterType or Resulttype, and the alias definition in mapper.xml to facilitate development.


one. MyBatis Default support aliases

Alias

Types of mappings

_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


As shown in the following illustration: Int is an alias;



two. Custom aliases

Individual definition aliases

Use the typealiases tag to define the alias; alias the Cn.itcast.mybatis.po.User to User;

[HTML] view plain copy <!--alias definition-<typeAliases> <!--define type for a single alias: path alias for a type : Alias--<typealias type= "Cn.itcast.mybatis.po.User" alias= "User"/> </typeAliases>

Bulk definition aliases

MyBatis automatically scans the PO class in the package, automatically defines the alias, which is the class name (uppercase or lowercase, usually lowercase)

For example: Cn.itcast.mybatis.po.User alias for user or user;

[HTML] view plain copy <!--alias definition-<typeAliases> <!--batch alias definition, specify package name, MyBatis automatically scans the PO class in the package, automatically Define aliases, which are class names (either uppercase or lowercase, usually lowercase)-<package name= "Cn.itcast.mybatis.po"/> </typeAliases>

In the Mapper.xml file

Before the alias is defined

[HTML] view plain copy <select id= "Finduserbyid" parametertype= "int" resulttype= "Cn.itcast.mybatis.po. User "> select * from USER where id = #{id} </select>

Alias user is used in direct resulttype after defining aliases;

[HTML]   View plain  copy <select id= "Finduserbyid"  parametertype= "int"        resulttype= "User" >  

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.