- 编写查询的接口
-
package com.dao; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; import com.pojo.Role; import com.pojo.User; public interface UserMapper { // 使用where标签多条件查询 List<User> findUserByNameAndRoleIdAndPhone(@Param("uname") String name, @Param("roleId") Integer roleId, @Param("phone") String phone); //使用trim标签多条件查询 List<User> findUserByNameAndRoleIdAndPhone1(@Param("uname") String name, @Param("roleId") Integer roleId, @Param("phone") String phone); // 使用set标签修改 int updateRole(Role role); // 删除 int deleteUser(int id); // 添加 int addRole(Role role); // 用户传递过来的是一个数组,根据数组进行查询 List<User> findUserAndRoleByIds(Integer[] ids); // 用户传递过来的是一个数组,根据数组进行查询 List<User> findUserAndRoleByNames(List<String> names); // 使用map集合进行入参查询 List<User> findUserAndRoleByMap(Map<String, Object> map); //使用choose标签确定只能以某一个作为条件进行查询 List<User> findUserByOne(User user); }
-
- 编写sql的映射文件UserMapper.xml
-
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dao.UserMapper"> <!-- 开启二级缓存 --> <cache></cache> <!-- 使用if对参数进行判断,多条件查询(根据不为空的条件进行查询) --> <select id="findUserByNameAndRoleIdAndPhone" resultType="User"> SELECT * FROM `smbms_user` <!-- WHERE 1=1 这样写就不用使用where标签 --> <!-- 使用where标签的特性就是会自动的根据条件是否加and --> <where> <if test="uname!=null and uname!=''"> AND userName LIKE CONCAT('%',#{uname},'%') </if> <if test="roleId!=null"> AND userRole=#{roleId} </if> <if test="phone!=null and phone!=''"> AND phone=#{phone} </if> </where> </select> <!-- 使用trim进行sql语句拼接 --> <select id="findUserByNameAndRoleIdAndPhone1" resultType=&#
-
MyBatis中动态Sql实现增删改查
最新推荐文章于 2022-11-28 22:29:35 发布
本文详细介绍了如何在MyBatis中利用动态Sql进行CRUD操作。从编写查询接口开始,逐步讲解如何创建UserMapper.xml映射文件,并通过测试类验证其功能。
摘要由CSDN通过智能技术生成