mybatis整理

mybatis

存储过程参数支持

#{id,mode=IN} #{age,mode=OUT} #{name,mode=INOUT}
IN :输入参数
OUT : 输出参数
INOUT :输入输出参数

自定义主键
<insert>
	<selectKey keyProperty="id" resultType="long" order="BEFORE">
		SELECT IF( max(id) = null,  1 ,max(id)+3)  from t_role
	</selectKey >
	insert into t_role (id,role_name,note) values(#{},#{},#{})
</insert>

keyProperty指定作为主键的属性
order BEFORE**在插入之前先执行生成主键的SQL,然后在插入数据库 AFTER 插入数据库后在执行查询主键SQL,主要用在有嵌套引用的时候

</>
<sql id="roleCols">
	${alias}.id,	${alias}.name
</sql > 
<select id="" resultMap="">
	SELECT 
	<include refid="roleCols">
		<property  name="alias" value="r"/>
	</imclude>
	from t_role
</select>
result’Map映射

属性:
property映射列结果的字段或者属性
column 对应的SQL列
javaType 配置JAVA类型
jdbcType 配置数据库类型
typeHandler 类型处理器
association 对象用于一对一
collection 集合对象 用于一对多
discriminator 鉴别器,是根据某些特定条件决定采用具体实现类级联的方案 比如有的表根据性别,根据分数等

<resultMap>
	<id property=""  column=""/>
	<result property=""  column=""/>
	<association property="phone"  column="phone_Id" select="xx.xx.xx.xx.PhoneMapper.getById"/>
	<collection  fetchType =“laze” property="userList" column="user_id"   select ="xx.xxx.xx.UserMapper.byId"/>
	<discriminator  javaType="long" cloumn="sex">//根据列取映射那个Mapper
		<case value="1" resultMap="maleHealthForMapper"/>
		<case value="2" resultMap="femaleHealthForMapper"/>
	</discriminator  >
</resultMap>
//employee 在配置文件定义的别名
<resultMap id="maleHealthForMapper" type="xx.xx.xx.xxFemaleEmployee" extends="employee">
</resultMap>
<resultMap id="femaleHealthForMapper" type="xx.xx.xx.xx.MaleEmployee" extends="employee">
</resultMap>
fetchType =eager 获得pojo对象后立即加载数据 =lazy 获得POJO对象后延迟加载数据 fetchType 会忽略全局配置项 lazyLoadingEnabld和aggressiveLazyLoading

lazyLoadingEnabldfalse默认 不开启,拿到所有关联数据,具体拿数据则aggressiveLazyLoading一起使用。
aggressiveLazyLoading
false 默认值 不开启(3.4.2之后),false关闭层级开关的延迟加载,只拿基本数据,不拿外键等层级数据
第二种 join等拼表

三元表达式 if(emp.sex=1,mhf.id,fhf.id)as h_id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值