MyBatis实现万能Map和模糊查询

目录
  • 万能Map
  • 模糊查询

万能Map

  我们在上一节博文里面将到利用Mybatis实现CRUD操作的时候,我们在数据库表中新增一条数据是这样操作的:

实体类对象的字段有:

package com.hpf.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//编写实体类User
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;
    private String username;
    private String password;
}

新增一条记录的xml文件配置内容为:

<insert id="addUser" parameterType="com.hpf.bean.User">
        insert into userinfo (id,username,password) values (#{id},#{username},#{password})
</insert>

其中,#后带的字段名都是我们实体类用户类里面一模一样的字段名。

接着我们再来试试用Map的方式实现用户记录的新增:

 <insert id="addUserByMap" parameterType="Map">
        insert into userinfo (id,username,password) values (#{id},#{user},#{pwd})
</insert>

测试部分:

 @Test
    public void testAddUserByMap(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        Map<String,Object> map = new HashMap<>();
        map.put("id", 6L);
        map.put("user", "张三");
        map.put("pwd", "666");
        int res = mapper.addUserByMap(map);
        sqlSession.commit();
        sqlSession.close();
    }

说明:我们业务相关的参数需要哪些字段内容,我们就往map里面传哪些字段内容就行。

模糊查询

  要求查询下表内为李性的用户信息:

package com.hpf.dao;

import com.hpf.bean.User;
import java.util.List;
import java.util.Map;
//这个接口实现的是对于用户的相关操作
public interface UserDao {
    //模糊查询用户信息
    List<User> getUserByLike(Map map);
}
 <select id="getUserByLike" parameterType="Map" resultType="com.hpf.bean.User">
        select * from userinfo where username like #{value}
 </select>
@Test
    public void testGetUserByLike(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        Map<String,Object> map = new HashMap<>();
        map.put("value", "李%");
        List<User> userByLike = mapper.getUserByLike(map);
        for(User user:userByLike)
            System.out.println(user);
    }

结果如图所示:

说明:模糊查询在这种方式下其实还有一种写法也可以得出结果,但是为了防止sql注入问题,我们不建议如下的写法:

<select id="getUserByLike" parameterType="Map" resultType="com.hpf.bean.User">
        select * from userinfo where username like #{value}"%"
</select>

到此这篇关于MyBatis实现万能Map和模糊查询的文章就介绍到这了,更多相关MyBatis 万能Map和模糊查询内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • MyBatis-plus 模糊查询的使用

    在使用MyBatis-plus的时候,一些基础的增删改查可以不用再自己写sql了: public interface UserDao extends BaseMapper<FykUser>{ } 就这样,就可以实现user表的增删改查了. 模糊查询 使用userDao.selectList(queryWrapper)方法,就可以查询出一个用户列表. 如果需要模糊查询,代码如下: //条件封装 QueryWrapper<FykUser> queryWrapper = new Quer

  • Mybatis模糊查询及自动映射实现详解

    这篇文章主要介绍了Mybatis模糊查询及自动映射实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Mybatis的模糊查询 1. 参数中直接加入%% 1 2 3 4 5 6 7 8 9 param.setUsername("%CD%"); param.setPassword("%11%"); <select id="selectPersons" resultType="p

  • Mybatis CURD及模糊查询功能的实现

    命名空间namespace: 配置文件中namespace中的名称为对应Mapper接口或者Dao接口的完整包名,必须一致! 1.查询(select) select: 接口中的方法名与映射文件中的SQL语句ID 一一对应 id parameterType resultType 案例:根据id查询用户 1.写接口(在UserMapper中添加对应的方法) public interface UserMapper { //根据ID查询用户 User getuserByID(int id); } 2.U

  • mybatis分页及模糊查询功能实现

    mybatis中分页有3种方式来实现,通过sql语句(两种传参方式)来实现,通过mybatis 的 Rowbounds 来实现. 通过(自定义类型)传参 来实现分页: 映射文件: <select id="findListBypage" parameterType="cn.wh.util.PageUtil" resultType="Role"> select * from t_role limit #{index},#{size} &l

  • mybatis的动态SQL和模糊查询实例详解

    现在以一个例子来介绍mybatis的动态SQL和模糊查询:通过多条件查询用户记录,条件为姓名模糊匹配,并且年龄在某两个值之间. 新建表d_user: create table d_user( id int primary key auto_increment, name varchar(10), age int(3) ); insert into d_user(name,age) values('Tom',12); insert into d_user(name,age) values('Bob

  • MyBatis使用Map与模糊查询的方法示例

    当我们的实体类.或者数据库里的表.字段或者参数很多,这个时候考虑使用map 一.使用map传参插入数据 1.编写Dao接口/Mapper层 使用Map做参数 //Dao接口/Mapper层 使用Map传参 int addUser2(Map<String,Object> map); 2.编写Mapper.xml中的sql语句 <!-- 传递map的key--> <insert id="addUser2" parameterType="map&quo

  • mybatis模糊查询、分页和别名配置的方法

    mybatis模糊查询(3种) 第一种 select * from user where username like "%" #{name} "%" 第二种 select * from user where username like "%${value}%" 第三种 <!--concat拼接字符串 mysql独有的函数--> select * from user where username like concat("%&

  • MyBatis实现万能Map和模糊查询

    目录 万能Map 模糊查询 万能Map   我们在上一节博文里面将到利用Mybatis实现CRUD操作的时候,我们在数据库表中新增一条数据是这样操作的: 实体类对象的字段有: package com.hpf.bean; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //编写实体类User @Data @AllArgsConstructor @NoArgsConstruc

  • 详解Mybatis中万能的Map和模糊查询写法

    1.万能的Map 假设,我们的实体类,或者数据库中的表,字段或参数过多,我们接口参数以前用的是实体类,现在考虑使用下Map! 接口: //万能的Map int addUser2(Map<String,Object> map); mapper.xml: <!--Map中的key--> <insert id="addUser2" parameterType="map"> insert into mybatis.user (id,nam

  • Mybatis 中 Oracle 的拼接模糊查询及用法详解

    一.结论 这里先给大家看一下结论 Oracle 中,拼接模糊查询的正确写法 SELECT A.USER_ID, A.USER_NAME FROM USER A AND A.USER_NAME like concat(concat('%','w'),'%') 或者 AND A.USER_NAME like '%' || 'w' || '%' Mybatis 中,拼接模糊查询的正确写法 <select id="selectByName" resultMap="BaseRes

  • MyBatis实现模糊查询的几种方式

    在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: 起初我在MyBatis的mapper文件中是这样写的: <select id="searchStudents" resultType="com.example.entity.StudentEntity" parameterType="com.exampl

  • Mybatis mysql模糊查询方式(CONCAT多个字段)及bug

    目录 Mybatis mysql模糊查询及bug 解决方案:一 解决方案:二 mybatis多个字段如何模糊查询一个值 Mybatis mysql模糊查询及bug 先看下如下xml SELECT t.id, t.mobile, t.account_name FROM t_account t WHERE 1=1 <if test="keyWord !=null and keyWord !=''"> and CONCAT(t.id,t.mobile,t.account_name

  • MyBatis中map的应用与模糊查询实现代码

    目录 1.MyBatis中map的应用 1.1.应用场景 1.2.具体实现 1.3.注意点!!! 2.模糊查询 1.MyBatis中map的应用 1.1.应用场景 假设,实体类,或者数据库中的表,字段或者参数过多,应当考虑使用Map!!! 1.2.具体实现 //万能map int addUser2(Map<String,Object> map); <!--对象中的属性,可以直接取出来 parameterType=传递map中的key--> <insert id="a

随机推荐