MyBatis define aliases for entity classes typealiases

Source: Internet
Author: User

First for everyone science, mybatis in fact is ibatis. It was renamed by Ibatis.

Recently Learning MyBatis knowledge, in terms of defining an alias for the entity class, there are two small knowledge to encourage each other.

Defining an alias for an entity class typically consists of the following parts

1. Add the following code in the MyBatis configuration (typically Conf.xml) file

<typeAliases>
<!--<typealias type= "Zq.mybatis.test1.User" alias= "_user"/>--
<package name= "Zq.mybatis.test1"/>
</typeAliases>

Careful friends will see the code in the comments off and not commented out. Yes, today's theme is these two key word------->typealias & Pakage

(1). Typealias has the attribute type and alias<typealias type= "Zq.mybatis.test1.User" alias= "_user"/>

The property value in type is written for the full class name of the corresponding entity. The above example indicates the package path "Zq.mybatis.test1" where the entity class "User" is located. Two components.

Alias property value is substituted for the property value in type

(2). The package has the attribute name. It is the name of the package, as its name implies. <package name= "Zq.mybatis.test1.User"/>

2. Aliases will be used in the corresponding mapping file

<!--insert an object the passed-in parameter is the entire entity class, and the corresponding parameter should be the same as the attribute in the entity--
<insert id= "Insertuser" parametertype= "_user">
Insert into users (name,age) values (#{name},#{age})
</insert>

<!--all results are queried because a single user entity is added to the list,
So when you specify that its return result parameter only needs to be specified as the return entity--
<select id= "Getalluser" resulttype= "_user">
SELECT * from Users
</select>

Obviously, the alias I used in the above mapping file is the alias defined by "Typealias". When you define an alias in this way, it does not go wrong. But what about when you change to the "package" definition? He did not set the alias, use the time how to use the alias? The following error occurs if you use "_user" directly at this time

Java.lang. Exceptionininitializererror
At Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
At sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown Source)
At sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown Source)
At java.lang.reflect.Constructor.newInstance (Unknown Source)
At org.junit.runners.BlockJUnit4ClassRunner.createTest (blockjunit4classrunner.java:195)
At Org.junit.runners.blockjunit4classrunner$1.runreflectivecall (blockjunit4classrunner.java:244)
At Org.junit.internal.runners.model.ReflectiveCallable.run (reflectivecallable.java:12)
At Org.junit.runners.BlockJUnit4ClassRunner.methodBlock (blockjunit4classrunner.java:241)
At Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:70)
At Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:50)
At Org.junit.runners.parentrunner$3.run (parentrunner.java:238)
At Org.junit.runners.parentrunner$1.schedule (parentrunner.java:63)
At Org.junit.runners.ParentRunner.runChildren (parentrunner.java:236)
At org.junit.runners.parentrunner.access$000 (parentrunner.java:53)
At org.junit.runners.parentrunner$2.evaluate (parentrunner.java:229)
At Org.junit.runners.ParentRunner.run (parentrunner.java:309)
At Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (junit4testreference.java:50)
At Org.eclipse.jdt.internal.junit.runner.TestExecution.run (testexecution.java:38)
At org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:459)
At org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:675)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (remotetestrunner.java:382)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (remotetestrunner.java:192)
caused by:org.apache.ibatis.exceptions.PersistenceException:
# # # Error building sqlsession.
# # # The error may exist in Zq/mybatis/test2/usermapper.xml
# # # Cause:org.apache.ibatis.builder.BuilderException:Error parsing SQL Mapper Configuration.

Cause:java.lang.RuntimeException:Error parsing Mapper XML. Cause:org.apache.ibatis.builder.BuilderException:Error resolving class.

Cause:org.apache.ibatis.type.TypeException:Could not resolve type alias ' _user '.

Cause:java.lang.ClassNotFoundException:Cannot Find class: _user
At org.apache.ibatis.exceptions.ExceptionFactory.wrapException (exceptionfactory.java:23)
At Org.apache.ibatis.session.SqlSessionFactoryBuilder.build (sqlsessionfactorybuilder.java:79)
At Org.apache.ibatis.session.SqlSessionFactoryBuilder.build (sqlsessionfactorybuilder.java:63)
At zq.mybatis.utils.mybitsUtils.getfaFactory (mybitsutils.java:17)
At zq.mybatis.test2.test2.<clinit> (test2.java:13)
    ... more
caused By:org.apache.ibatis.builder.BuilderException:Error parsing SQL Mapper Configuration. Cause:java.lang.RuntimeException:Error parsing Mapper XML. Cause:org.apache.ibatis.builder.BuilderException:Error resolving class.  Cause:org.apache.ibatis.type.TypeException:Could not resolve type alias ' _user '. Cause:java.lang.ClassNotFoundException:Cannot Find class: _user
At org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration (xmlconfigbuilder.java:105)
At Org.apache.ibatis.builder.xml.XMLConfigBuilder.parse (xmlconfigbuilder.java:88)
At Org.apache.ibatis.session.SqlSessionFactoryBuilder.build (sqlsessionfactorybuilder.java:77)
    ... more
caused By:java.lang.RuntimeException:Error parsing Mapper XML. Cause:org.apache.ibatis.builder.BuilderException:Error resolving class.  Cause:org.apache.ibatis.type.TypeException:Could not resolve type alias ' _user '. Cause:java.lang.ClassNotFoundException:Cannot Find class: _user
At org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement (xmlmapperbuilder.java:113)
At Org.apache.ibatis.builder.xml.XMLMapperBuilder.parse (xmlmapperbuilder.java:88)
At org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement (xmlconfigbuilder.java:311)
At org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration (xmlconfigbuilder.java:103)
    ... more
caused by:org.apache.ibatis.builder.BuilderException:Error resolving class.  Cause:org.apache.ibatis.type.TypeException:Could not resolve type alias ' _user '. Cause:java.lang.ClassNotFoundException:Cannot Find class: _user
At Org.apache.ibatis.builder.BaseBuilder.resolveClass (basebuilder.java:94)
At Org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode (xmlstatementbuilder.java:79)
At Org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext (xmlmapperbuilder.java:128)
At Org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext (xmlmapperbuilder.java:121)
At org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement (xmlmapperbuilder.java:111)
    ... more
caused by:org.apache.ibatis.type.TypeException:Could not resolve type alias ' _user '. Cause:java.lang.ClassNotFoundException:Cannot Find class: _user
At Org.apache.ibatis.type.TypeAliasRegistry.resolveAlias (typealiasregistry.java:109)
At Org.apache.ibatis.builder.BaseBuilder.resolveAlias (basebuilder.java:115)
At Org.apache.ibatis.builder.BaseBuilder.resolveClass (basebuilder.java:92)
    ... more
caused by:java.lang.ClassNotFoundException:Cannot Find class: _user
At org.apache.ibatis.io.ClassLoaderWrapper.classForName (classloaderwrapper.java:188)
At org.apache.ibatis.io.ClassLoaderWrapper.classForName (classloaderwrapper.java:87)
At org.apache.ibatis.io.Resources.classForName (resources.java:250)
At Org.apache.ibatis.type.TypeAliasRegistry.resolveAlias (typealiasregistry.java:105)
    ... more

typealias "defined aliases" _user two appear above the error "is predictable." How do you use the alias for the hand that uses the package to set the alias? In fact, the reason is very simple, I have specified his package name, then this package of all entities below the equivalent has been set alias, and this alias is actually an entity's own entity name.

<update id= "UpdateUser" parametertype= "User">
Update users set Name=#{name},age=#{age} where Id=#{id}
</update>
<select id= "Getalluser" resulttype= "User">
SELECT * from Users
</select>

All in all, one is set to the specific entity name (Typealias), and the other is the package name that the entity resides in. In the long run it seems that using the latter to set the package name is more convenient and better in project development.

MyBatis define aliases for entity classes typealiases

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.