MyBatis中常见标签的使用
1. <collection>标签
例如有两张表:user表
role表
那么我们再写实体类User 、Role 的对应关系是,一个用户有多个角色,因此,在 User 的实体中加入一个 Role 的属性。
private List<Role> roles //1对多关系
然而在Mybatis的XML文件中我们要使用 <collection>标签来映射这一关系
<select id="queryUserRoleList" resultMap="queryForListMap"> SELECT u.user_id, u.user_name, u.password, r.role_idr.role_name, FROM user u LEFT JOIN role r ON u.user_id = r.user_id</select>
<resultMap id="queryUserRoleList" type="com.rh.domain.User"> <id column="user_id" property="userId" jdbcType="VARCHAR"/> <result column="user_name" property="userName" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> <collection property="roles" javaType="java.util.List" ofType="com.rh.domain.Role"> <id column="role_id" property="roleId" jdbcType="VARCHAR" /> <result column="role_name" property="roleName" jdbcType="VARCHAR" /> </collection> </resultMap>
2. <association>标签
既然有1对多标签就有1对1标签,<association>标签就是1对1标签, 嵌套查询是先查一个表,根据这个表里面的结果的外键id,去再另外一个表里面查询数据,也是通过association配置,但另外一个表的查询通过select属性配置,如下:
<resultMap id=”queryList” type=”Blog”>
<association property="author" column="blog_author_id" javaType="Author"
select=”selectAuthor”/>
</resultMap> <select id=”selectBlog” parameterType=”int” resultMap=”queryList”>
SELECT * FROM BLOG WHERE ID = #{id}
</select> <select id=”selectAuthor” parameterType=”int” resultType="Author">
SELECT * FROM AUTHOR WHERE ID = #{id}
</select>
3. 使用resultType、ResultMap处理返回结果
resultType: 指定返回值结果的完全限定名,处理多表查询的结果。多表查询需要定义vo封装查询的结果。只能用在单表查询或者定义vo的情况,如果是定义vo不能将关联的数据封装为需要获得某个类的对象
resultMap:不需要定义vo类,将关联数据对应的类,作为另外一个类的属性。
第一步:定义resultMap
第二步:引用resultMap
使用resultMap:用来多表关联的复杂查询中,通过需要将关联查询的数据封装为某个实体类对象,如果有特殊业务需要或者说明需要将管理数据封装为实体类的对象,使用resultMap
4.<if>、<choose>、<when>、<otherwise>
if标签是与的关系,而 choose 是或的关系。choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似我们Java中的 switch 语句,choose 为 switch,when 为 case,otherwise 为 default。
例子:
<select id="queryList" parameterType="Blog" resultType="Blog">select * from t_blog where 1 = 1 <choose><when test="name!= null">and name= #{name}</when><when test="password!= null">and password= #{password}</when><otherwise>and sign= "sign"</otherwise></choose></select>
5.<sql>和<include>
<sql>标签相当于一段SQL语句片段,引用它的时候使用<include>标签来引用
<sql id="testSql><if test="sex!=null">and sex=#{sex}</if><if test="email!=null>and email=#{email}</if>
</sql><select id="findUser" parameeterType="User" resultType="User">select * from user<where><include refid="testSql"></include></where>
</select>
6. <foreach>
一般使用它来做语句的拼接,在向sql传递 集合或者数组的时候 mybatis使用<foreach>来解析
例如:SQL语句SELECT * FROM user WHERE id=1 OR id=10 OR id=16
mybatis:
<if test="ids != null"> <foreach collection="ids"item="user_id"open="AND ("close=")" separator="or" >每次遍历需要拼接的串id= #{user_id}</foreach></if>
7. <set>、<if>
这两个标签一般用于更新语句中,表限定条件
<update id="updateStudent" parameterType="StudentEntity"> UPDATE student <set> <if test="studentName!=null and studentName!='' "> student_name = #{studentName}, </if> <if test="studentSex!=null and studentSex!='' "> student_sex = #{studentSex}, </if> <if test="studentBirthday!=null "> student_birthday = #{studentBirthday}, </if> </set> WHERE student_id = #{studentID};
</update>
8. <trim>
<trim>标签作用一般是用于去除多余关键字
<!-- 查询学生list,like姓名,=性别 -->
<select id="getStudentList" parameterType="StudentEntity" resultMap="studentResultMap"> SELECT * from student ST <trim prefix="WHERE" prefixOverrides="AND|OR"> <if test="studentName!=null and studentName!='' "> ST.student_name LIKE CONCAT(CONCAT('%', #{studentName}),'%') </if> <if test="studentSex!= null and studentSex!= '' "> AND ST.student_sex = #{studentSex} </if> </trim>
</select>
MyBatis中常见标签的使用相关推荐
- 杂记:三、mybatis中常见 Java 类型的内置类型别名
杂记:三.mybatis中常见 Java 类型的内置类型别名 杂记:三.mybatis中常见 Java 类型的内置类型别名 一.mybatis 二.类型别名 杂记:三.mybatis中常见 Java ...
- mybatis中where标签和trim标签的区别以及使用
mybatis中where和trim的区别以及使用 1.多个查询条件 2.where标签的使用 3.trim标签的使用 4.trim扩展 1.多个查询条件 在多个查询条件下,由于需要拼接sql语句,所 ...
- 前端—HTML中常见标签的使用
HTML 1.常见标签 <!DOCTYPE html> <html lang="en"> <head><meta charset=&quo ...
- mybatis中常见的坑
1.在mybatis中,<=号不能用,需要转换成>=号: 未完,待续<
- Mybatis中typeAliases标签和package标签
1.typeAliases 主配置文件: <typeAliases><typeAlias type="com.itheima.domain.User" alias ...
- HTML中常见标签的英文解释及说明
HTML标签 英文全称 中文释义 a Anchor 锚 alt alter 替用(一般是图片显示不出的提示) b Bold 粗体(文本) bdo Direction of Text Display 文 ...
- Mybatis中include标签的使用
1.正常情况下使用 sql标签用作封装参数或SQL块 <sql id="Param_Block"> 名字随便起id,name,phone </sql> 或者 ...
- Mybatis中selectKey 标签的作用,主键回填,找了好多文章没一个解释清楚。。
(1)没有配置selectKey 标签时候插入数据: <insert id="addUser" parameterType="model.User"> ...
- mybatis中resultMap标签中的extends属性有什么用?
继承父类的resultMap,然后父类有的那一部分属性标签(id.result标签)就不用自己写了,例如: 子类: public class PromotionProduct extends PmsP ...
最新文章
- mxnet symbol图的 变量 shape
- 让fedora18桌面显示图标
- 6、Dcoker 容器数据卷用DockerFile添加
- 1021. Deepest Root (25)
- volta架构 微型计算机,性能大爆炸 NVIDIA新GPU架构曝光
- for vue 一行2列_前端开发面试问什么?vue面试中经常问到的问题?用vue想拿20k,面试题要这样答!...
- IntelliJ Idea工作笔记009---代码没有错,但是在IDEA报错的原因
- torch 安装的问题
- hping 详解_hping3 详解
- android个人理财通项目实训计划书
- linux phpstudy卸载,linux 安装phpstudy
- butterworth matlab,Matlab实现Butterworth滤波器
- Google Play Academy 组队 PK 赛,正式开赛!
- 作为一名测试工程师,如何保证软件的质量?
- Jenkins docker下JNLP slave节点远程连接报错port not reachable的解决
- SpringCLoud实战微服务之——微服务简介以及入门使用
- 产业分析:中国债券市场投资手册
- 超微服务器如何查看服务器信息,超微服务器
- 用Python进行自然语言处理 读书笔记 第一章
- Stateful Firewall和SPI(stateful packet inspection) Firewall介绍
热门文章
- 卖方研究正临多重困境:万八佣金有待终结,分仓模式屡被敲打,未来谁为研究买单?多家券商开单转型
- 唯物辩证法的“三大规律”和“五大范畴”-联系与发展
- Xmind 8 pro 安装及破解教程
- android 8省电,智能手机怎么省电?Android手机省电攻略
- 阿里研究院崔瀚文:新零售的上下左右前后
- 铌酸锂调制器matlab仿真代码,《铌酸锂晶体的研究与分析》-毕业论文.DOC
- Dialog的知识点
- 一文掌握常用的机器学习模型
- 全网最齐全的《大数据选择题题库.pdf》限时开放下载!
- JavaScript做轮播图片(1)