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

编程知识 更新时间:2023-04-08 00:31:00

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日志

  1. 添加logback输出具体看 java的orm框架 mybatis 如何查看sql,debug输出
  2. 简单的mybatis配置
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>

更多推荐

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

本文发布于:2023-04-08 00:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/f974c01755a1138590e8aed46c57031d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   地方   java   orm   mybatis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 55323文章数
  • 14阅读数
  • 0评论数