admin管理员组文章数量:1794759
java的orm框架 mybatis 一些学习和注意的地方
typeAliases,mapper中使用, 简化xml中配置类型,select中的resultType,resultMap中的type,collection中的ofType
<typeAliases> <typeAlias alias="userctrl" type="orgc.dsdf.sdfsfs.sd.fsfs.UserController"/> </typeAliases>java内置类型,mybatis已经内置了相应的typeAliases,如果你还使用java.lang.String等等,就说明没好好看文档,对mybatis不是很了解
_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 |
object | Object |
map | Map |
hashmap | HashMap |
list | List |
arraylist | ArrayList |
collection | Collection |
iterator | Iterator |
resultMap,type为类型,自定义返回的类型与表column对应,select里使用resultMap(而不是resultType),值为resultMap的id,适用于关联查询时自定义返回,【注意resultMap标签内的result可以对应sql查询结果的【字段名】与【对象属性】 】
<resultMap type="orgc.dsdf.sdfsfs.sd.fsfs.UserController" id="usermap"> <id column="id" property="id"/> <result column="name" property="user.name"/> <result column="funkyNumber" property="funkyNumber"/> <result column="roundingMode" property="roundingMode"/> </resultMap>【定义公共sql片段】
<sql id="columns"> 字段名 </sql>【引用sql片段】
<include refid="columns"/>【条件判断,值不为空时,需要的sql片段】
<if test="id!=null"> and id=#{id}</if>【where 常与if搭配使用】where 标签是sql,语句中的where,如果where标签中内容为空,是不会有where的
<where> <if test="id!=null">and id=#{id}</if></where>【resultMap标签中的collection】适用于一对多查询
mybatis配置中指定mapper文件位置
<mappers> <mapper resource="org/mybatis/builder/Authormapper.xml"/> <mapper url="file:///var/mappers/AuthorMapper.xml"/> <mapper class="org.mybatis.builder.AuthorMapper"/> 【mapper.xml与mapper接口类同目录】 <package name="org.mybatis.builder"/> 包下的所有mapper </mappers>mybatis输出sql日志
版权声明:本文标题:java的orm框架 mybatis一些学习和注意的地方 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686777214a101469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论