MyBatis interface binding and multiparameter

Keywords: Mybatis xml Java JDBC

Article Directory

MyBatis Interface Binding Scheme and Multi-parameter Passing

In addition and deletion checks of MyBatis, it is found that only one parameter can be passed in. If you want to pass multiple parameters, you can use map to pass in parameters.

map mode for multiparameter transfer

<select id="selectByMap" resultType="User" parameterType="map">
        select * from user where username=#{username} and password=#{password};
</select>
        Map<String,String> map=new HashMap<>();
        map.put("username","reverie");
        map.put("password","123456");

        User u=sqlSession.selectOne("cn.com.mapper.selectByMap",map);
        System.out.println(u);

In the select tag, use map as the parameterType and #{key} to get the data in the map
Multiparameters are passed indirectly

Interface Binding

You can create an interface in MyBatis and bind the implementation class and mapper.xml of the interface generated by mybatis so that you can get sql in mapper.xml by calling the interface

Implementation Steps
1. Create an interface
The package name and interface name of the interface have the same attribute value as <mapper>namespace in mapper.xml
The method name of the interface has the same id attribute as the mapper.xml tag

package cn.com.mapper;

import cn.com.pojo.User;

import java.util.List;

public interface UserMapper {
    List<User> selectAll();
}

2. Use <package>to scan interfaces and mapper.xml to bind in MyBatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
        <package name="cn.com.pojo"/>
    </typeAliases>

    <environments default="default">
        <environment id="default">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOlED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/db_contracts?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--<mapper resource="cn/com/mapper/UserMapper.xml" />-->
        <package name="cn.com.mapper"/>    <!--name Consistent with package name of interface-->
    </mappers>
</configuration>

3. Call Interface

        //Gets the interface through the getMapper() method with the class parameter of the interface
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //Direct invocation of methods in interfaces
        List<User> list=userMapper.selectAll();
        System.out.println(list);

Interface for multiparameter delivery

1. Common mode of transmission

package cn.com.mapper;

import cn.com.pojo.User;

import java.util.List;

public interface UserMapper {
    List<User> selectAll();

    User selectByNameAndPassword(String username,String password);
}

Declare methods in interfaces

    <select id="selectByNameAndPassword" resultType="User">
        select * from user where username=#{0} and password=#{1}
    </select>

Write the appropriate method in mapper.xml

When multiple parameters are used, no parameterType needs to be written
Use 0,1,2 or param1,param2 when invoking parameters

       UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user=userMapper.selectByNameAndPassword("reverie","r123456");
        System.out.println(user);

2. Annotation Pass-through

Declare methods in interfaces

package cn.com.mapper;

import cn.com.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapper {
    List<User> selectAll();

    User selectByNameAndPassword(@Param("username") String username, @Param("password") String password);
}

mybatis converts the parameter to map, where @Param("key") is the value of the map

<select id="selectByNameAndPassword" resultType="User">
        select * from user where username=#{username} and password=#{password}
</select>

Note that **#{} is consistent with what is in @param**, not with the parameter name, which can start anywhere

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user=userMapper.selectByNameAndPassword("reverie","r123456");
        System.out.println(user);
93 original articles published, 4 praised, 1662 visited
Private letter follow

Posted by TRB on Tue, 04 Feb 2020 19:39:38 -0800