ҳ > > > Java >

Mybatisѧϰ֮·3

2020-03-14 16:02:25Դ԰ Ķ ()

ǿߣٴȺLinuxʹ

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
ԭϵ

ǩIEtimnamenttselect

Ȩվ²磬Ȩϵwest999com@outlook.com
رע⣺վת۲վ۵㣬վṩӰƬ廭ƷʹãԭϵȨԭ

һƪJAVAEXCELģ

һƪSpring񴫲