Springboot Settings for MyBatis aliases

Source: Internet
Author: User

MyBatis alias settings

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.

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

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.

Previously, when we referenced entity classes in the SQL mapping XML file, we needed to write the full class name of the entity class (package name + class name), as follows

    1. <!--Create User---
    2. <insert id="AddUser" parametertype="Me.gacl.domain.User">
    3. Insert into users (name,age) values (#{name},#{age})
    4. </Insert>

Parametertype= "Me.gacl.domain.User" Here write the entity class User's full class name Me.gacl.domain.User, each time it is difficult to write such a long string of content, and we want to be able to be written in the following form

 
    1. <insert id="AddUser2" parametertype="_user">
    2. Insert into users (name,age) values (#{name},#{age})
    3. </Insert>

Parametertype= "_user" is much easier to write, in order to achieve this effect, we need to define an alias "_user" for entity class = "Me.gacl.domain.User" in the Conf.xml file, as follows:
Add the following configuration to the <configuration></configuration> tag in the Conf.xml file:

 
    1. <typealiases>
    2. <typealias type="Me.gacl.domain.User" alias="_user"/>
    3. </typealiases>

This allows you to define an alias of _user for the Me.gacl.domain.User class, and later _ The user represents the Me.gacl.domain.User class, so that any place in the SQL mapping XML file that needs to refer to the Me.gacl.domain.User class can be replaced with _user, which is the purpose of simplifying the entity class reference.

In addition to using <typealias type= "Me.gacl.domain.User" alias= "_user"/> This way to set an alias for an entity class individually, We can also set aliases for all entity classes under a package in bulk using the following method:

 
  1. <!--Configure an alias for an entity class to configure the entity class name so that it can be used instead of the entity class when referencing the entity class, for the purpose of shorthand-
  2. <typealiases>
  3. <!--Configure an alias for the entity class Me.gacl.domain.User _user--
  4. <!--<typealias type= "Me.gacl.domain.User" alias= "_user"/> --
  5. <!--configure aliases for all entity classes under the Me.gacl.domain package, mybatis The default setting alias is to remove the simple class name after the package containing the class
  6. For example, Me.gacl.domain.User this entity class alias will be set to user
  7. -->
  8. <package name="Me.gacl.domain"/>
  9. </typealiases>

<package name= "Me.gacl.domain"/> means setting aliases for all entity classes under this package. MyBatis The default setting alias is to remove the simple class name of the package in which the class is located, such as Me.gacl.domain.User, the alias of the entity class will be set to user.

In the springboot we can also set the alias, this is in the configuration file one sentence:

Springboot settings for MyBatis aliases

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.