Mybatisѧϰ֮·3
2020-03-14 16:02:25À´Ô´£º²©¿ÍÔ° ÔĶÁ ()
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ÊÂÎñ´«²¥»úÖÆ
- MyBatisÖеÄ$ºÍ#£¬Óò»ºÃ£¬×¼±¸×ßÈË£¡ 2020-06-11
- ѧϰJava 8 Stream Api (4) - Stream Öն˲Ù×÷Ö® collect 2020-06-11
- javaѧϰ֮µÚÒ»Ìì 2020-06-11
- Javaѧϰ֮µÚ¶þÌì 2020-06-11
- SpringBoot 2.3 ÕûºÏ×îаæ ShardingJdbc + Druid + MyBatis 2020-06-11
IDC×ÊѶ£º Ö÷»ú×ÊѶ ×¢²á×ÊѶ ÍйÜ×ÊѶ vps×ÊѶ ÍøÕ¾½¨Éè
ÍøÕ¾ÔËÓª£º ½¨Õ¾¾Ñé ²ß»®Ó¯Àû ËÑË÷ÓÅ»¯ ÍøÕ¾Íƹã Ãâ·Ñ×ÊÔ´
ÍøÕ¾ÁªÃË£º ÁªÃËÐÂÎÅ ÁªÃ˽éÉÜ ÁªÃ˵ãÆÀ Íø׬¼¼ÇÉ
ÐÐÒµ×ÊѶ£º ËÑË÷ÒýÇæ ÍøÂçÓÎÏ· µç×ÓÉÌÎñ ¹ã¸æ´«Ã½
ÍøÂç±à³Ì£º Asp.Net±à³Ì Asp±à³Ì Php±à³Ì Xml±à³Ì Access Mssql Mysql ÆäËü
·þÎñÆ÷¼¼Êõ£º Web·þÎñÆ÷ Ftp·þÎñÆ÷ Mail·þÎñÆ÷ Dns·þÎñÆ÷ °²È«·À»¤
Èí¼þ¼¼ÇÉ£º ÆäËüÈí¼þ Word Excel Powerpoint Ghost Vista QQ¿Õ¼ä QQ FlashGet ѸÀ×
ÍøÒ³ÖÆ×÷£º FrontPages Dreamweaver Javascript css photoshop fireworks Flash
³ÌÐòÉè¼Æ£º Java¼¼Êõ C/C++ VB delphi
- Photoshop»æÖÆÁ¢Ìå·ç¸ñµÄ΢Ц±íÇé
- PSÎÄ×ÖÌØЧ½Ì³Ì£ºÖÆ×÷¹«Â·ÉϸöÐÔµÄÍ¿Ñ»
- PhotoshopÉè¼Æ¾í±ßЧ¹ûµÄ±äÐνð¸ÕµçÓ°
- PSÉ«²ÊÐÞ¸´½Ì³Ì£ºÀûÓÃÉ«½×¹¤¾ß¿ìËÙ¸øºì
- PS°ë͸Ã÷ÎïÌå¿Ùͼ£ºÀûÓÃͨµÀÑ¡Çø¹¤¾ß¿Ù
- PSº£±¨Éè¼Æ¼¼Çɽ̳̣ºÑ§Ï°ÖÆ×÷¸öÐԵč
- PSͼƬÌØЧÖÆ×÷½Ì³Ì£ºÑ§Ï°¸øÌúËþͼƬÖÆ
- ѧϰÓÃphotoshop°Ñ»ë×ǵĺ£Ë®ÕÕƬºóÆÚ
- PS¹Å·çÕÕƬ½Ì³Ì£º¸ø¹Å·çÃÀÅ®´òÔì³öÅ®ÏÀ
- PS¸öÐÔÈËÎﺣ±¨ÖÆ×÷£ºÉè¼Æ´´ÒâʱÉеIJ£
- ʲôÈí¼þ¿ÉÒÔµÁȡ΢ÐźÅÃÜÂë,ÔõôµÁ±ð
- ´ÅÁ¦ËÑË÷ÍøÕ¾µ¼º½2020Äê¸üÐÂ
- springcloudѧϰ֮·: (Ò») ×î¼òµ¥µÄ´î
- ´´½¨Gradle¹¤³Ì³öÏÖCould not install
- ³¹µ×Ū¶®¡°PKIX path building failed
- TomcatÆô¶¯±¨´í:org.apache.catalina.L
- spring boot ´íÎó£ºCheck your ViewRes
- »ùÓÚHttpClientµÄаæÕý·½½ÌÎñϵͳģÄâ
- Ö»ÓгÌÐòÔ±²ÅÄÜ¿´¶®µÄ³µÅÆ£¬¶®µÃ×ÔÈ»¶®
- mybatis ×¢½â@Results¡¢@Result¡¢@Resu