mybatis alias of typeAliases

I. Introduction

typeAliases is an alias processor class in MyBatis, translation is an alias meaning. Alias is the role of the Java processor type, give it a little simple alias, so we introduced the full class name in the XML file where you can use an alias instead. This can reduce a lot of workload, saving development time. For example let's use a full class name:

<select id="findByName" parameterType="string" resultType="com.aaa.entity.User">
        select * from user where name like #{name}
</select>

Second, the case

Modify mybatis-config.xml core profile

<typeAliases>
        <!--typeAlias:某个java类型的全限定名;type:类名;alias:别名;如果类比较多,需要写很多个?怎么办?-->
        <typeAlias type="com.aaa.entity.User" alias="user"></typeAlias>
</typeAliases>

The above case we can see in the following typeAliases label, can play an alias for the User class, if it has a lot of Java classes, it is not below there are a lot of <typeAlias> </ typeAlias>, if we can one kind other configurations to achieve a certain class for all the following packets are aliases, instead of one single class. The answer is yes.

MyBatis provides a package label for us to achieve the above functions can be configured by the label. Let us look at how to use the package label:

<typeAliases>
        <!--package,默认别名是user-->
        <package name="com.aaa.entity"></package>
    </typeAliases>

Configured as above can be achieved by batch aliases

Although we can use aliases bulk package, but there is a situation will lead to conflict, we have to simulate it, create a new User class, the User class below com.queen.mybatis.bean.child directory, which is a sub-bean package directory next, as shown:

This is an alias conflict. . . That we now have to how to solve this problem? In fact, you can think of, MyBatis are we thought of

A, using @Alias ​​annotation to specify a new alias for a type

B, Modify User class file

IUserDao.xml modify configuration files

Through the above test we can conclude: the case of a batch of aliases, use @Alias ​​annotation to specify a new alias for a type alias to avoid conflict error 

发布了675 篇原创文章 · 获赞 631 · 访问量 126万+

Guess you like

Origin blog.csdn.net/zhangchen124/article/details/104094495