java的orm框架 mybatis 一些学习和注意的地方

typeAliases,mapper中使用, 简化xml中配置类型,select中的resultType,resultMap中的type,collection中的ofType

<typeAliases>
  <typeAlias alias="userctrl" type="org.ccc.dsdf.sdfsfs.sd.fsfs.UserController"/>
</typeAliases>

java内置类型,mybatis已经内置了相应的typeAliases,如果你还使用java.lang.String等等,就说明没好好看文档,对mybatis不是很了解

Alias Mapped Type
_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="org.ccc.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日志

  1. 添加logback输出具体看 java的orm框架 mybatis 如何查看sql,debug输出
  2. 简单的mybatis配置
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值