有勇气的牛排博客

javaWeb MyBatis(六)动态sql where foreach sql片段

有勇气的牛排 1045 Java 2021-10-22 11:38:14

1 案例

传递pojo综合查询用户信息

2 UserMapper.xml

<!-- 传递pojo综合查询用户信息 --> < select id="findUserList" parameterType="UserBean" resultType="UserBean"> select * from user where 1=1 < if test="id!=null and id!=''"> and id=#{id} < /if> < if test="username!=null and username!=''"> and username like "%#{username}%" < /if> < /select>

3 UserMapper.java

package net920vip.mapper; import net920vip.bean.UserBean; import java.util.List; /** * 接口文件 对应UserMapper.xml文件 * 用户管理mapper * */ public interface UserMapper { // 根据id查询user public UserBean findUserById(int id) throws Exception; // 根据用户名模糊查询 public List<UserBean> findUserByUsername(String username)throws Exception; // 新增user 返回类型 int或void均可 public int insertUser(UserBean user) throws Exception; // 新增user 返回类型 int或void均可 public int updateUserById(UserBean user) throws Exception; // 根据user删除user public int deleteUserById(int id) throws Exception; // 传递pojo综合查询用户信息 public List<UserBean> findUserList(UserBean USER) throws Exception; }

4 Test.java

// 使用sql片段,综合查询测试 List<UserBean> userList = userMapper.findUserList(null); // 查询所有 //System.out.println(userList.toString()); // UserBean user = new UserBean(); user.setId(10); List<UserBean> userList1 = userMapper.findUserList(user); System.out.println(userList1.toString());

5 加入where对UserMapper.xml升级

sql只接收一个数组参数,这时sql解析参数的明湖曾mybatis固定为array,如果数组是通过一个pojo传递到sql则参数的名称为poji中的属性名。

<!-- 传递pojo综合查询用户信息 --> <!--< select id="findUserList" parameterType="UserBean" resultType="UserBean">--> <!-- select * from user where 1=1--> <!-- < if test="id!=null and id!=''">--> <!-- and id=#{id}--> <!-- < /if>--> <!-- < if test="username!=null and username!=''">--> <!-- and username like "%#{username}%"--> <!-- < /if>--> <!--</ select>--> <select id="findUserList" parameterType="UserBean" resultType="UserBean"> select * from user < where> < if test="id!=null and id!=''"> and id=#{id} < /if> < if test="username!=null and username!=''"> and username like "%#{username}%" < /if> </ where> < /select>

6 foreach

向sql传递数组或List,mybatis使用foreach解析,如下

UserMapper.xml

<select id="selectUserByIds" parameterType="java.util.List" resultType="UserBean"> select * from user <where> <if test="list != null"> <foreach collection="list" item="id" open="and id in(" separator="," close=")"> #{id} </foreach> </if> </where> </select>

如果数组中是简单类型,则写为#{item},不用通过ognl获取对象属性值了

接口:UserMapper.java

package net920vip.mapper; import net920vip.bean.UserBean; import java.util.List; /** * 接口文件 对应UserMapper.xml文件 * 用户管理mapper * */ public interface UserMapper { // 批量根据ids查询 public List<UserBean> selectUserByIds(List<Integer> list) throws Exception; }

Test.java

// 使用foreach动态sql,批量查询 传空 null:查询所有 ArrayList<Integer> list = new ArrayList<>(); // list.add(1); // list.add(10); // List<UserBean> users = userMapper.selectUserByIds(list); List<UserBean> users = userMapper.selectUserByIds(Arrays.asList(1,10)); System.out.println(users.toString());

7 sql片段

sql中可将重复的sql提取出来,使用时用include即可,最终达到sql重用的目的,如:

<!-- 抽取模板--> <sql id="query_user_sql"> <if test="id!=null and id!=''"> and id=#{id} </if> <if test="username!=null and username!=''"> and username like "%#{username}%" </if> </sql> <!-- 传递pojo综合查询用户信息 --> <select id="findUserList" parameterType="UserBean" resultType="UserBean"> select * from user <where> <!-- <if test="id!=null and id!=''">--> <!-- and id=#{id}--> <!-- </if>--> <!-- <if test="username!=null and username!=''">--> <!-- and username like "%#{username}%"--> <!-- </if>--> <include refid="query_user_sql"/> </where> </select>

注意:如果引用其他mapper.xml的片段,则在引用时需要加上namspace,如下

<include refid="namespace.sql片段id"/>

gitee地址:https://gitee.com/net920vip/mybatis-demo01.git


留言

专栏
文章
加入群聊