SSM13【Mybatis:Mybatis映射文件深入--动态sql语句&sql片段抽取】

动态sql语句

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

复制代码
 1 package com.haifei.mapper;
 2 
 3 import com.haifei.domain.User;
 4 
 5 import java.util.List;
 6 
 7 public interface UserMapper {
 8 
 9     public List<User> findByCondition(User user);
10     public List<User> findByIds(List<Integer> ids);
11 
12 }
复制代码
复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 
 4 <mapper namespace="com.haifei.mapper.UserMapper">
 5 
 6     <!--<select id="findByCondition" parameterType="user" resultType="user">
 7         select * from user where id=#{id} and username=#{username} and password=#{password}
 8     </select>-->
 9 
10     <!--<select id="findByCondition" parameterType="user" resultType="user">
11         select * from user where 1=1
12         <if test="id!=0">
13             and id=#{id}
14         </if>
15         <if test="username!=null">
16             and username=#{username}
17         </if>
18         <if test="password!=null">
19             and password=#{password}
20         </if>
21     </select>-->
22     <!--
23         <where>标签可以动态添加查询条件
24         当没有条件时sql语句中就没有where,有哪个条件就where哪个条件
25         可以代替 "where 1=1"
26     -->
27     <select id="findByCondition" parameterType="user" resultType="user">
28         select * from user
29         <where>
30             <if test="id!=0">
31                 and id=#{id}
32             </if>
33             <if test="username!=null">
34                 and username=#{username}
35             </if>
36             <if test="password!=null">
37                 and password=#{password}
38             </if>
39         </where>
40     </select>
41 
42     <!--select * from user where id in(1,2,3)-->
43     <!--<foreach>标签中,当传入的参数是列表时collection值为list,当传入的是数组时collection值为array-->
44     <select id="findByIds" parameterType="list" resultType="user" >
45         select * from user
46         <where>
47             <foreach collection="list" open="id in(" close=")" item="id" separator=",">
48                 #{id}
49             </foreach>
50         </where>
51     </select>
52 
53 </mapper>
复制代码

 

 

复制代码
 1 package com.haifei.test;
 2 
 3 import com.haifei.domain.User;
 4 import com.haifei.mapper.UserMapper;
 5 import org.apache.ibatis.io.Resources;
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Test;
10 
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.ArrayList;
14 import java.util.List;
15 
16 public class MapperTest {
17 
18     @Test
19     public void test1() throws IOException {
20         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
21         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
22         SqlSession sqlSession = sqlSessionFactory.openSession();
23 
24         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
25 
26         //模拟查询条件
27         User condition = new User();
28         condition.setId(1);
29         condition.setUsername("zhangsan");
30         condition.setPassword("123");
31 
32         List<User> userList = mapper.findByCondition(condition);
33         System.out.println(userList); //[User{id=1, username='zhangsan', password='123'}]
34     }
35 
36     @Test
37     public void test2() throws IOException {
38         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
39         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
40         SqlSession sqlSession = sqlSessionFactory.openSession();
41 
42         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
43 
44         //模拟查询条件
45         User condition = new User();
46         condition.setId(1);
47 //        condition.setUsername("zhangsan");
48 //        condition.setPassword("123");
49 
50         List<User> userList = mapper.findByCondition(condition);
51         System.out.println(userList); //[User{id=1, username='zhangsan', password='123'}]
52     }
53 
54     @Test
55     public void test3() throws IOException {
56         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
57         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
58         SqlSession sqlSession = sqlSessionFactory.openSession();
59 
60         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
61 
62         //模拟ids数据
63         List<Integer> ids = new ArrayList<>();
64         ids.add(1);
65         ids.add(2);
66         ids.add(3);
67 
68         List<User> userList = mapper.findByIds(ids);
69         System.out.println(userList);
70     }
71 
72 }
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 sql片段抽取

 

 

复制代码
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 
 4 <mapper namespace="com.haifei.mapper.UserMapper">
 5 
 6     <!--sql语句抽取-->
 7     <sql id="selectUser">select * from user</sql>
 8 
 9     <!--<select id="findByCondition" parameterType="user" resultType="user">
10         select * from user where id=#{id} and username=#{username} and password=#{password}
11     </select>-->
12 
13     <!--<select id="findByCondition" parameterType="user" resultType="user">
14         select * from user where 1=1
15         <if test="id!=0">
16             and id=#{id}
17         </if>
18         <if test="username!=null">
19             and username=#{username}
20         </if>
21         <if test="password!=null">
22             and password=#{password}
23         </if>
24     </select>-->
25     <!--
26         <where>标签可以动态添加查询条件
27         当没有条件时sql语句中就没有where,有哪个条件就where哪个条件
28         可以代替 "where 1=1"
29     -->
30     <select id="findByCondition" parameterType="user" resultType="user">
31         <include refid="selectUser"/>
32         <where>
33             <if test="id!=0">
34                 and id=#{id}
35             </if>
36             <if test="username!=null">
37                 and username=#{username}
38             </if>
39             <if test="password!=null">
40                 and password=#{password}
41             </if>
42         </where>
43     </select>
44 
45     <!--select * from user where id in(1,2,3)-->
46     <!--<foreach>标签中,当传入的参数是列表时collection值为list,当传入的是数组时collection值为array-->
47     <select id="findByIds" parameterType="list" resultType="user" >
48         <include refid="selectUser"/>
49         <where>
50             <foreach collection="list" open="id in(" close=")" item="id" separator=",">
51                 #{id}
52             </foreach>
53         </where>
54     </select>
55 
56 </mapper>
复制代码

 

 

 

posted @   yub4by  阅读(51)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示