<!--查询姓名中包含“雨”,并且年龄>20的学生信息-->
ublic List<StudentInfo> findStudentsByCondition(Map<String,Object> map);
[java] view plain copy
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_2_2638930" name="code" class="java"><!--多条件查询-->
<select id="findStudentsByCondition" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{stuName} '%' and stuAge>#{stuAge}
</select></pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_3_305399" name="code" class="java">多条件查询
@Test
public void testSelectLike(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
Map<String,Object> map=new HashMap<String,Object>();
map.put("stuName","雨");
map.put("stuAge",20);
List<StudentInfo> list = dao.findStudentsByCondition(map);
for (StudentInfo stu:list) {
System.out.println(stu.getStuName());
}
session.close();
}
}
</pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_4_4907205" name="code" class="java"><!--查询姓名中包含“雨”,并且年龄>20的学生信息-->
public List<StudentInfo> findStudentsByConditionMutliArgs(String stuName,int stuAge);</pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_5_1145214" name="code" class="java"><!--多条件查询使用索引-->
<select id="findStudentsByConditionMutliArgs" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{0} '%' and stuAge>#{1}
</select></pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_6_7175479" name="code" class="java">//多条件查询
@Test
public void testSelectLikeMulti(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
List<StudentInfo> list = dao.findStudentsByConditionMutliArgs("雨",20);
for (StudentInfo stu:list) {
System.out.println(stu.getStuName());
}
session.close();
}</pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_7_6793795" name="code" class="java">//智能标签if
public List<StudentInfo> findByIf(StudentInfo stu);</pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_8_8048314" name="code" class="java"></pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_9_7666630" name="code" class="java"><!--智能标签if-->
<select id="findByIf" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="stuName!=null"><!--用户录入的姓名字段-->
and stuName like '%' #{stuName} '%'
</if>
<if test="stuAge!=null">
and stuAge>#{stuAge}
</if>
</where>
</select></pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_10_8888129" name="code" class="java">//智能标签if
@Test
public void testIf(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
StudentInfo stu=new StudentInfo();
// stu.setStuName("雨");
stu.setStuAge(20);
List<StudentInfo> list = dao.findByIf(stu);
for (StudentInfo stuinfo:list) {
System.out.println(stuinfo.getStuName());
}
session.close();
}
</pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_11_458294" name="code" class="java">//智能标签choose
public List<StudentInfo> findByChoose(StudentInfo stu);</pre><br>
<br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_12_8124761" name="code" class="java"><!--智能标签choose-->
<select id="findByChoose" resultType="StudentInfo">
select * from studentinfo
<where>
<choose>
<when test="stuName!=null">
and stuName like '%' #{stuName} '%'
</when>
<when test="stuAge!=null">
and stuAge>#{stuAge}
</when>
<otherwise>
and 1=2
</otherwise>
</choose>
</where>
</select></pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_13_6314618" name="code" class="java">//智能标签choose
@Test
public void testChoose(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
StudentInfo stu=new StudentInfo();
// stu.setStuName("雨");
// stu.setStuAge(20);
List<StudentInfo> list = dao.findByChoose(stu);
for (StudentInfo stuinfo:list) {
System.out.println(stuinfo.getStuName());
}
session.close();
}</pre>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_14_5932934" name="code" class="java">//智能标签foreach
public List<StudentInfo> findByForeachArray(int[] ids);</pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_15_2727351" name="code" class="java"><!--智能标签foreach Array-->
<select id="findByForeachArray" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="array.length>0">
stuid in
<foreach collection="array" open="(" close=")" separator="," item="stuno">
#{stuno}
</foreach>
</if>
</where>
</select></pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_16_2345667" name="code" class="java">//智能标签Foreach array
@Test
public void testForeachArray(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
int[] ids={2,5};
List<StudentInfo> list = dao.findByForeachArray(ids);
for (StudentInfo stuinfo:list) {
System.out.println(stuinfo.getStuName());
}
session.close();
}
</pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_17_1963983" name="code" class="java">//智能标签foreach List<Integer>
public List<StudentInfo> findByForeachList(List<Integer> list);</pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_21_5279816" name="code" class="java"> <!--智能标签foreach List-->
<select id="findByForeachListStudent" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="list.size>0">
stuid in
<foreach collection="list" open="(" close=")" separator="," item="stu">
#{stu.stuId}
</foreach>
</if>
</where>
</select></pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_19_9423490" name="code" class="java">//智能标签Foreach List<StudentInfo>
@Test
public void testForeachListStudent(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
List<StudentInfo> list=new ArrayList<StudentInfo>();
StudentInfo s1=new StudentInfo();
s1.setStuId(2);
StudentInfo s2=new StudentInfo();
s2.setStuId(5);
list.add(s1);
list.add(s2);
List<StudentInfo> list2 = dao.findByForeachListStudent(list);
for (StudentInfo stuinfo:list2) {
System.out.println(stuinfo.getStuName());
}
session.close();
}</pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_20_7089959" name="code" class="java">//智能标签foreach List<StudentInfo>
public List<StudentInfo> findByForeachListStudent(List<StudentInfo> list);
}
</pre><br>
<pre code_snippet_id="2480216" snippet_file_name="blog_20170712_21_5279816" name="code" class="java"> <!--智能标签foreach List-->
<select id="findByForeachListStudent" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="list.size>0">
stuid in
<foreach collection="list" open="(" close=")" separator="," item="stu">
#{stu.stuId}
</foreach>
</if>
</where>
</select></pre><pre code_snippet_id="2480216" snippet_file_name="blog_20170712_22_2389877" name="code" class="java"> //.智能标签Foreach List<StudentInfo>
@Test
public void testForeachListStudent(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
List<StudentInfo> list=new ArrayList<StudentInfo>();
StudentInfo s1=new StudentInfo();
s1.setStuId(2);
StudentInfo s2=new StudentInfo();
s2.setStuId(5);
list.add(s1);
list.add(s2);
List<StudentInfo> list2 = dao.findByForeachListStudent(list);
for (StudentInfo stuinfo:list2) {
System.out.println(stuinfo.getStuName());
}
session.close();
}</pre><br>
智能标签if set
<update id="modify">
UPDATE studentinfo
<set>
<if test="stuName!=null">stuName=#{stuName},</if>
<if test="stuAge!=null">stuAge=#{stuAge},</if>
</set>
WHERE stuId=#{stuId}
</update>
智能标签 if trim 更新操作
<update id="mod" parameterType="StudentInfo">
UPDATE studentinfo
<trim prefix="set" suffixOverrides="," suffix="where stuId=#{stuId}">
<if test="stuName!=null">stuName=#{stuName},</if>
<if test="stuAge!=null">stuAge=#{stuAge},</if>
</trim>
</update>