MyBatis框架(6)动态sql
本次全部学习内容:MyBatisLearning
在之前小案例的基础上我们先进行简单的实现一下:
if:
在UserMapper.xml文件中找到:
<!-- 动态sql --> <!-- 综合查询 --> <select id="findBySelect" parameterType="com.MrChengs.po.UserView" resultType="com.MrChengs.po.UserCustomer" > select * from user <where> <if test="userCustomer!=null"> <if test="userCustomer.sex!=null and userCustomer.sex!='' "> and user.sex=#{userCustomer.sex} </if> <if test="userCustomer.username!=null and userCustomer.username!=''"> and user.username like '%${userCustomer.username}%' </if> </if> </where> </select>
注意:where标签可以自动去掉条件中的第一个 and
//动态sql //高级查询 @Test public void testfindBySelect() throws Exception{ SqlSession sqlSession = getSqlSessionFactory().openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserView userView = new UserView(); UserCustomer userCustomer = new UserCustomer(); //userCustomer.setSex(1); userCustomer.setUsername("小明"); userView.setUserCustomer(userCustomer); List<User> user = mapper.findBySelect(userView); for(User u : user){ System.out.println(u); } sqlSession.close(); }
结果:
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@545997b1] DEBUG [main] - ==> Preparing: select * from user WHERE user.username like '%小明%' DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 3 User [id=16, username=张小明, birthday=null, sex=1, address=河南郑州] User [id=22, username=陈小明, birthday=null, sex=1, address=河南郑州] User [id=25, username=陈小明, birthday=null, sex=1, address=河南郑州]
SQL片段:
<!-- sql片段 --> <!-- id唯一,是sql片段的唯一标识 --> <!-- 基于单表定义sql片段,这样的话sql片段的可重用性才高,在sql片段中不要包括where --> <sql id="selectBySql"> <if test="userCustomer!=null"> <if test="userCustomer.sex!=null and userCustomer.sex!='' "> and user.sex=#{userCustomer.sex} </if> <if test="userCustomer.username!=null and userCustomer.username!=''"> and user.username like '%${userCustomer.username}%' </if> </if> </sql>
引用sql片段:
<!-- 动态sql --> <!-- 综合查询 --> <select id="findBySelect" parameterType="com.MrChengs.po.UserView" resultType="com.MrChengs.po.UserCustomer" > select * from user <where> <!-- 引用sql片段 --> <include refid="selectBySql"></include> </where> </select>
测试代码同上次测试代码!
foreach标签:
假设我们同时查询多个id
<!-- sql片段 --> <!-- id唯一,是sql片段的唯一标识 --> <!-- 基于单表定义sql片段,这样的话sql片段的可重用性才高,在sql片段中不要包括where --> <sql id="selectBySql"> <if test="userCustomer!=null"> <if test="userCustomer.sex!=null and userCustomer.sex!='' "> and user.sex=#{userCustomer.sex} </if> <if test="userCustomer.username!=null and userCustomer.username!=''"> and user.username like '%${userCustomer.username}%' </if> </if> <!-- foreach --> <!-- 测试 --> <!-- select from user where id = 1 or id = 10 or id = 12 --> <!-- collection:指定输入对象的集合 --> <!-- item:每个遍历生成成的对象 --> <!-- open:开始遍历时 拼接的串 --> <!-- close:结束遍历时 拼接的串 --> <!-- separator:遍历时两个对象中需要拼接的串 --> <foreach collection="ids" close=")" item="userId" open="1=1 and (" separator="or"> id=#{userId} </foreach> </sql> <!-- 动态sql --> <!-- 综合查询 --> <select id="findBySelect" parameterType="com.MrChengs.po.UserView" resultType="com.MrChengs.po.UserCustomer" > select * from user <where> <!-- 引用sql片段 --> <include refid="selectBySql"></include> </where> </select>
测试代码:
//foreach //动态sql //高级查询 @Test public void testfindBySelect() throws Exception{ SqlSession sqlSession = getSqlSessionFactory().openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserView userView = new UserView(); //foreach List<Integer> ids = new ArrayList<>(); ids.add(1); ids.add(2); ids.add(30); userView.setIds(ids); List<User> users = mapper.findBySelect(userView); for(User user : users){ System.out.println(user); } sqlSession.close(); }
结果:
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@545997b1] DEBUG [main] - ==> Preparing: select * from user WHERE 1=1 and ( id=? or id=? or id=? ) DEBUG [main] - ==> Parameters: 1(Integer), 2(Integer), 30(Integer) DEBUG [main] - <== Total: 2 User [id=1, username=王五, birthday=null, sex=2, address=null] User [id=30, username=Ma, birthday=null, sex=1, address=安徽]
<foreach collection="ids" close=")" item="userId" open="1=1 and id in(" separator=","> id=#{userId} </foreach>