企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## if标签 ~~~ <select id="findUserByCondition" parameterType="com.like.domain.User" resultType="com.like.domain.User"> select * from user where 1 = 1 <if test="username != null"> and username = #{username} </if> <if test="sex != null"> and sex = #{sex} </if> </select> ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); User user = new User(); user.setUsername("老王"); List<User> userByCondition = mapper.findUserByCondition(user); for (User user1 : userByCondition) { System.out.println(user1); } ~~~ ## where标签 ~~~ <select id="findUserByCondition" parameterType="com.like.domain.User" resultType="com.like.domain.User"> select * from user <where> <if test="username != null"> and username = #{username} </if> <if test="sex != null"> and sex = #{sex} </if> </where> </select> ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); User user = new User(); user.setUsername("老王"); user.setSex("女"); List<User> userByCondition = mapper.findUserByCondition(user); for (User user1 : userByCondition) { System.out.println(user1); } ~~~ ## foreach标签 ~~~ package com.like.domain; import java.util.List; public class QueryVo { private User user; private List<Integer> ids; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<Integer> getIds() { return ids; } public void setIds(List<Integer> ids) { this.ids = ids; } } ~~~ ~~~ <select id="findUserInIds" parameterType="com.like.domain.QueryVo" resultType="com.like.domain.User"> select * from user <where> <if test="ids != null and ids.size() > 0"> <foreach collection="ids" open="and id in (" close=")" item="id" separator=","> #{id} </foreach> </if> </where> </select> ~~~ ~~~ QueryVo queryVo = new QueryVo(); List<Integer> list = new ArrayList<>(); list.add(41); list.add(43); list.add(45); queryVo.setIds(list); System.out.println(queryVo); IUserDao mapper = session.getMapper(IUserDao.class); List<User> userInIds = mapper.findUserInIds(queryVo); for (User user : userInIds) { System.out.println(user); } ~~~ ## sql标签 抽取重复的SQL. ~~~ <sql id="findAll"> select * from user </sql> <select id="findUserInIds" parameterType="com.like.domain.QueryVo" resultType="com.like.domain.User"> <include refid="findAll"></include> <where> <if test="ids != null and ids.size() > 0"> <foreach collection="ids" open="and id in (" close=")" item="id" separator=","> #{id} </foreach> </if> </where> </select> ~~~