MyBatis学习笔记(二)——标签使用
来源:互联网 发布:ip与mac绑定怎么设置 编辑:程序博客网 时间:2024/06/05 12:47
resultMap手动映射
当数据库表中的字段名称与pojo的实体类的名称不同的时候,使用resultMap:
示例代码:
<mapper namespace="com.zrxjuly.mybatis.mapper.OrderMapper"> <!-- type:为实体类的类名 id的值要与写的sql语句中resultMap的值一致. colume:数据库中表的字段名称 property:实体类中属性的名称 --> <resultMap type="Orders" id="orders"> <result column="user_id" property="userId"/> </resultMap> <!-- resultMap的值要与resultMap标签中的id的值一致 --> <select id="selectOrder" resultMap="orders"> select * from orders </select></mapper>
动态sql
通过MyBatis提供的各种标签方法实现动态拼接sql。
if标签
示例代码:
<select id="selectUserBySexAndUsername" parameterType="User" resultType="User"> select * from user where 1=1 <if test="sex != null and sex != ''"> sex=#{sex} </if> <if test="username != null and username != ''"> and username=#{username} </if> </select>
注:where后的1=1的含义:如果sex为空的话,则语句就会变成select * from user where and username=#{username}
,这样就会报sql语法错误。1=1意思是true
where标签
不用where 1=1的方法,直接用where标签:
<select id="selectUserBySexAndUsername" parameterType="User" resultType="User"> select * from user <!-- where可以去掉第一个前and,也就是说在加and语句的时候要把and放在条件之前 --> <where> <if test="sex != null and sex != ''"> sex=#{sex} </if> <if test="username != null and username != ''"> and username=#{username} </if> </where> </select>
sql片段
若sql语句中多次出现重复部分,可以提取出来公用:
<!-- 提取出来的sql片段,公共部分, --> <sql id="selectA"> select * from user </sql> <select id="selectUserBySexAndUsername" parameterType="User" resultType="User"> <include refid="selectA" /><!-- 此处引用了select * from user --> <!-- where可以去掉第一个前and,也就是说在加and语句的时候要把and放在条件之前 --> <where> <if test="sex != null and sex != ''"> sex=#{sex} </if> <if test="username != null and username != ''"> and username=#{username} </if> </where> </select>
foreach标签
当要求根据多个参数进行查询时,用foreach循环遍历多个值:
<!-- collection:传入的参数的名称, 例如:如果接口传过来的参数为Integer[]类型的,则collection="array",此时parameterType属性去掉 如果接口传过来的参数为List<Integer>类型的,则collection="list",此时parameterType属性去掉 item:循环遍历的值 open:遍历开始 close:遍历结束 separator:分隔符 该sql语句相当于:select * from user where id in(1,2,3) --><select id="selectUserByIds" parameterType="QueryVo" resultType="User"> select * from user <where> id in <foreach collection="idList" item="id" open="(" close=")" separator=","> #{id} </foreach> </where></select>
参数为Integer[]类型:
<select id="selectUserInteger" resultType="User"> select * from user <where> id in <foreach collection="array" item="id" open="(" close=")" separator=","> #{id} </foreach> </where></select>
参数为List<Integer>
类型:
<select id="selectUserList" resultType="User"> select * from user <where> id in <foreach collection="list" item="id" open="(" close=")" separator=","> #{id} </foreach> </where></select>
阅读全文
0 0
- MyBatis学习笔记(二)——标签使用
- mybatis 学习笔记(二)mybatis常用标签
- MyBatis读书学习笔记(二)——映射器
- mybatis学习笔记(二)
- Mybatis学习笔记(二)
- Mybatis学习笔记(二)
- MyBatis学习笔记(二)
- MyBatis学习笔记(二)
- MyBatis学习笔记(二)
- MyBatis学习笔记(二)
- JavaScript读书学习笔记(二)——标签 引用
- MyBatis学习总结(二)——使用MyBatis…
- Mybatis学习笔记之二:在项目中使用Mybatis
- MyBatis学习笔记二
- myBatis学习笔记(二)
- MyBatis学习笔记二
- MyBatis学习笔记二——CRUD操作
- Mybatis学习笔记(二)配置文件
- mysql设置用户权限时报错
- eclipse的dx问题
- 【AKKA 官方文档翻译】actor模型如何满足现代分布式系统的需求
- 正则表达式6~15位数字、字母
- Jenkins-工作区清理插件
- MyBatis学习笔记(二)——标签使用
- 海思3516A开发板调试整理(一)
- 【AKKA 官方文档翻译】akka库和模块概览
- angularJS 增加 删除 修改 查询 排序
- Python reduce()函数
- Kubernetes 介绍
- [TensorFlow] demo1 导入tensorflow相关的包,并生成Weights和biases
- MongoDB3.6新特性深入理解
- Raft一致性算法