Mybatis学习之路3

@Param()注解

  • 引用类型不需要加
  • 如果只有一个基本类型的,可以忽略
  • 在 sql 中引用的就是在注解中设定的字段名

高级结果映射

public class Student {
    private int id;
    private String name;
    private Teacher teacherr;
}
public class Teacher {
    private int id;
    private String name;
}

多对一:

<select id="getStudentsWithTeacher2" resultMap="studentTeac2">
    select s.id sid,s.name sname,t.id tid,t.name tname from student s join teacher t on s.tid = t.id;
</select>
<resultMap id="studentTeac2" type="student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacherr" javaType="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
    </association>
</resultMap>

一对多

<select id="getStudentsWithTeacher2" resultMap="studentTeac2">
    select s.id sid,s.name sname,t.id tid,t.name tname from student s join teacher t on s.tid = t.id;
</select>
<resultMap id="studentTeac2" type="student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacherr" javaType="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
    </association>
</resultMap>

动态 sql

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

实体类

public class Blog {
    private String id;
    private String name;
    private String author;
    private Date create_time;
    private int views;
}

if

<select id="findBlog" parameterType="map" resultType="blog">
    <!--此处为了sql语句能够执行只能加上条件-->
    select * from blog where views>1
    <if test="name !=null">
        and name = #{name}
    </if>
    <if test="author != null">
        and author like "%"#{author}"%"
    </if>
</select>
@Test
public void findBlog() {
    ...
    Map map = new HashMap();
    map.put("author", "w");
    map.put("name", "Maven配置");
    mapper.findBlog(map);
    ...
}

如果上面的 views>1 也变成动态条件那么语句就会变成这样

<select id="findBlog" parameterType="map" resultType="blog">
    <!--此处为了sql语句能够执行只能加上条件-->
    select * from blog where
    <if test="views !=null">
        views = #{views}
    </if>
    <if test="name !=null">
        and name = #{name}
    </if>
    <if test="author != null">
        and author like "%"#{author}"%"
    </if>
</select>

如果此时 views 为空,那么 sql 语句就出现了错误 select * from blog where and... select * from blog where 此时就需要用 where 来解决

where

where 会根据条件去掉 and 或者 or 来使 sql 能够正确执行

<select id="findBlog" parameterType="blog" resultType="blog">
    select * from blog
    <where>
        <if test="name !=null">
            name = #{name}
        </if>
        <if test="author != null">
            and author like "%"#{author}"%"
        </if>
    </where>
</select>

choose (when, otherwise)

相当于 Java 中的 switch case 语句匹配条件中的一个若条件都不符合就匹配 otherwise 中的条件

<select id="findBlogByRequirement" parameterType="map" resultType="blog">
    select * from blog where
    <choose>
        <when test="id != null">
            id = #{id}
        </when>
        <when test="name != null">
            name = #{name}
        </when>
        <otherwise>
            views > 1
        </otherwise>
    </choose>
</select>

set

根据条件判断要影响的列

<update id="updateBlog" parameterType="blog">
    update blog
    <set>
        <if test="name != null">
            name = #{name},
        </if>
        <if test="author">
            author = #{author},
        </if>
        <if test="views != 0">
            views = #{views}
        </if>
    </set>
    where id = #{id}
</update>

sql 语句块

<sql id="ifn_name">
    <if test="name != null">
        name = #{name}
    </if>
</sql>

使用:

<include refid="ifn_name"/>

foreach

<select id="findBlogByRequirement2" parameterType="map" resultType="blog">
    select * from blog
    <where>
        <foreach collection="names" item="na" open="name in (" separator="," close=")">
            #{na}
        </foreach>
    </where>
</select>
Map map = new HashMap();
List<String> names = new ArrayList<>();
names.add("mybatis配置");
//names.add("Maven配置");
map.put("names", names);
List<Blog> blogs = mapper.findBlogByRequirement2(map);
for (Blog blog : blogs) {
    System.out.println(blog);
}
原文地址:https://www.cnblogs.com/wangjr1994/p/12494646.html