Mybatisѧϰ֮·3

2020-03-14 16:02:25À´Ô´£º²©¿ÍÔ° ÔĶÁ ()

ÐÂÀÏ¿Í»§´ó»ØÀ¡,ÔÆ·þÎñÆ÷µÍÖÁ5ÕÛ

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
ÈçÓÐÒÉÎÊÇëÓëÔ­×÷ÕßÁªÏµ

±êÇ©£º

°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»­£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ£¬°æȨ¹éÔ­×÷ÕßËùÓÐ

ÉÏһƪ£ºJAVAÉú³ÉEXCELÄ£°å

ÏÂһƪ£ºSpringÊÂÎñ´«²¥»úÖÆ