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不是很了解
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="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日志
- 添加logback输出具体看 java的orm框架 mybatis 如何查看sql,debug输出
- 简单的mybatis配置
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
更多推荐
java的orm框架 mybatis 一些学习和注意的地方
发布评论