首先爲你們科普一下,Mybatis事實上就是Ibatis。它是由Ibatis改名而來。java
最近在學Mybatis的知識,在講到爲實體類定義別名是有兩個小知識與你們共勉。apache
爲實體類定義別名通常有如下幾個部分組成session
1.在Mybatis的配置(通常爲conf.xml)文件中添加如下代碼mybatis
<typeAliases> <!-- <typeAlias type="zq.mybatis.test1.User" alias="_User"/> --> <package name="zq.mybatis.test1"/> </typeAliases> |
細心的朋友會看到代碼中備註掉的和沒有被註釋掉的。沒錯,今天的主題就是這兩個key word------->typeAlias & pakageapp
(1).typeAlias具備屬性 type 和alias<typeAlias type="zq.mybatis.test1.User" alias="_User"/>eclipse
type中的屬性值寫的是對應實體的全類名。以上爲例則表示實體類「User」所在的包路徑「zq.mybatis.test1.」兩個部分組成。ui
alias的屬性值則是取代type中的屬性值lua
(2).package具備屬性name。顧名思義就是所在包的名字。<package name="zq.mybatis.test1.User"/>spa
2.在對應的映射文件中將使用別名
xml
<!-- 插入對象是的傳入參數爲整個實體類,相對應的參數要與實體中屬性相同 --> <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
|
使用package設置別名時還使用「typeAlias」定義出來的別名「_User二出現上面的這樣的錯誤」是能夠預料到的。那到底使用package設置別名的手該如何運用這個別名呢?其實道理很簡單,我指定了他的包名,那這個包下面的全部實體至關於已經被設置了別名,而這個別名實際上就是某一個實體本身的實體名。
<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> |
總的來講就是一個是設置到了具體的實體名(TypeAlias),而另外一個則是隻指定到了實體所在的包名(package)。從長遠了看來使用後者設置包名在項目開發中更方便更好使