MyBatis进行CRUD查询操作时的数据绑定方法详解演示

2023-12-19 22:17:07

MyBatis进行CRUD查询操作时的数据绑定方法详解演示

1、创建一个mybatis项目

可以参考之前的博文mybatis首次使用并查询数据库数据
下文在该项目基础上进行优化操作,UserDao.java改名为UserMapper.java,UserDao.xml改名为UserMapper.xml,也可以不改名字内容一样。

2、单个参数绑定查询

在MyBatisTest.java中添加下面方法

@Test
    public void testGetUserById() throws IOException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUserById(43);
        System.out.println(user);
    }

在UserMapper.java中添加对应的属性

User getUserById(Integer id);

在UserMapper.xml中添加查询sql语句如下

<!--
        id:和方法名保持一致
        parameterType:输入参数的类型
        resultType:返回值的类型
    -->
    <select id="getUserById" parameterType="java.lang.Integer" resultType="cn.fpl1116.pojo.User">
        <!-- #{}:占位符,等价于jdbc的?,用来替换实际的参数-->
        select * from user where id = #{id}
    </select>

3、序号参数绑定查询

在MyBatisTest.java中添加下面方法

@Test
    public void testGetUser() throws IOException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUser(43, "俞莲舟");
        System.out.println(user);
    }

在UserMapper.java中添加对应的属性

User getUser(Integer id, String username);

在UserMapper.xml中添加查询sql语句如下

<select id="getUser" resultType="cn.fpl1116.pojo.User">
        <!--select * from user where id=#{arg0} and username=#{arg1}--><!-- arg0 arg1-->
        select * from user where id=#{param1} and username=#{param2}   <!-- param1 param2-->
    </select>

4、注解参数绑定查询

在MyBatisTest.java中添加下面方法

@Test
    public void testGetUser2() throws IOException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUser2(43, "俞莲舟");
        System.out.println(user);
    }

在UserMapper.java中添加对应的属性

User getUser2(@Param("id") Integer id, @Param("username") String username);

在UserMapper.xml中添加查询sql语句如下

<select id="getUser2" resultType="cn.fpl1116.pojo.User">
        select * from user where id=#{id} and username=#{username}
    </select>

5、对象参数绑定查询

在MyBatisTest.java中添加下面方法

@Test
    public void testGetUser3() throws IOException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User userParam = new User();
        userParam.setId(43);
        userParam.setUsername("俞莲舟");
        User user = userMapper.getUser3(userParam);
    }

在UserMapper.java中添加对应的属性

User getUser3(User user);

在UserMapper.xml中添加查询sql语句如下

<select id="getUser3" parameterType="cn.fpl1116.pojo.User" resultType="cn.fpl1116.pojo.User">
        select * from user where id=#{id} and username=#{username}
    </select>

6、Map参数绑定查询

在MyBatisTest.java中添加下面方法

@Test
    public void testGetUser4() throws IOException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> map = new HashMap<>();
        map.put("id", 43);
        map.put("username", "俞莲舟");
        User user = userMapper.getUser4(map);
        System.out.println(user);
    }

在UserMapper.java中添加对应的属性

User getUser4(Map<String, Object> map);

在UserMapper.xml中添加查询sql语句如下

<select id="getUser4" parameterType="java.util.Map" resultType="cn.fpl1116.pojo.User">
        select * from user where id=#{id} and username=#{username}
    </select>

文章来源:https://blog.csdn.net/qq_62124267/article/details/135092168
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。