本文章只是mybatis的一些判断使用案例。
1.关于封装map使用
<!-- map封装 -->
<!--<resultMap id="UserResultMap" type="map">
<id property="id" column="id" />
<result property="userName" column="user_name" />
</resultMap>
<select id="getUserMap" resultMap="UserResultMap">
select id,user_name from user_t where id = #{id}
</select>-->
2.关于case when else end的使用
<select id="findStudentAll" resultType="com.example.demo.model.Student">
select
a.id,
a.name,
(case a.sex
when 1 then '男'
when 0 then '女'
else '空的'
end
)sex
from student_t a
</select>
3.关于查询返回值判断问题
<select id="getById" resultType="com.example.demo.model.Student">
select
a.id,
a.name,
IF(a.sex IS NULL,'你都没性别',a.sex) sex
from student_t a
where id = '4'
</select>
4.字符串的拼接使用
<select id="findConcatAll" resultType="com.example.demo.model.Student">
select
a.id,
a.name,
concat('老家是',a.adress) adress
from student_t a
</select>
5.批量添加
<!-- 新增内部号==批量插入 -->
<insert id="addBatch" parameterType="java.util.List" >
INSERT INTO `user_base_info`
(`user_id`, `user_code`, `real_name`, `id_card`, `sex`, `birthday`, `phone`, `email`, `create_time`)
VALUES
<foreach collection="list" item="item" separator=",">
(
#{item.userId},
#{item.userCode},
#{item.realName},
#{item.idCard},
#{item.sex},
#{item.birthday},
#{item.phone},
#{item.email},
#{item.createTime}
)
</foreach>
</insert>