首先为大家科普一下,Mybatis事实上就是Ibatis。它是由Ibatis更名而来。
最近在学Mybatis的知识,在讲到为实体类定义别名是有两个小知识与大家共勉。
为实体类定义别名一般有以下几个部分组成
1.在Mybatis的配置(一般为conf.xml)文件中添加以下代码
<typeAliases> <!-- <typeAlias type="zq.mybatis.test1.User" alias="_User"/> --> <package name="zq.mybatis.test1"/> </typeAliases> |
细心的朋友会看到代码中备注掉的和没有被注释掉的。没错,今天的主题就是这两个key word------->typeAlias & pakage
(1).typeAlias具有属性 type 和alias<typeAlias type="zq.mybatis.test1.User" alias="_User"/>
type中的属性值写的是对应实体的全类名。以上为例则表示实体类“User”所在的包路径“zq.mybatis.test1.”两个部分组成。
alias的属性值则是取代type中的属性值
(2).package具有属性name。顾名思义就是所在包的名字。<package name="zq.mybatis.test1.User"/>
2.在对应的映射文件中将使用别名
<!-- 插入对象是的传入参数为整个实体类,相对应的参数要与实体中属性相同 --> <insert id="insertUser" parameterType="_User"> insert into users(name,age) values(#{name},#{age}) </insert>
<!-- 查询所有结果时,因为是将一个一个的USer实体添加到list中, 所以在指定其返回结果参数只需要指定为返回实体就可以了 --> <select id="getAllUser" resultType="_User"> select * from users </select>
|
很显然,以上映射文件中我使用的别名是“typeAlias”定义出来的别名。当使用这种方式定义别名的时候操作起来不会出错。但如果换成“package”定义的时候呢?他又没有设置别名,使用的时候该怎么使用别名呢?此时如果直接使用“_User”则会出现以下错误
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) ... 22 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) ... 25 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) ... 27 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) ... 30 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) ... 34 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) ... 36 more
|