MyBatis-14 Interface Binding Scheme and Multi-parameter Transfer

Keywords: xml Mybatis JDBC MySQL

Interface Binding Scheme

Effect

After creating an interface, the implementation class of the interface is generated by mybatis. By calling the interface object, the sql written in mapper.xml can be obtained.
Note: This solution is used in the later integration of mybatis and spring

Implementation steps

  1. Create an interface
    The interface package name and interface name are the same as the mapper tag namespace in mapper.xml
    The method name in the interface has the same id attribute as the mapper.xml tag
  2. Scanning interface and mapper.xml using package 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"> 
  <!-- DTD: xml The grammar checker limits what to write. -->
<configuration> 
	<typeAliases>
		<package name="com.youdian.pojo"/>
	</typeAliases>
<!-- default Quote environment Of id,Current environment --> 
<environments default="default"> 
<!-- Declare environments that can be used --> 
<environment id="default"> 
<!-- Use native JDBC affair --> 
<transactionManager type="JDBC">
</transactionManager> 
<!-- Database connection pool -->
<dataSource type="POOLED"> 
<property name="driver" value="com.mysql.jdbc.Driver"/> 
<property name="url" value="jdbc:mysql://localhost:3306/sum"/> 
<property name="username" value="root"/> 
<property name="password" value="root"/> 
</dataSource> 
</environment> 
</environments>
<mappers> 
<package name="com.youdian.mapper"/>
</mappers> 
</configuration>

Code implementation steps

  1. Use the package tag under mappers in mybatis.xml
<mappers>
<package name="com.bjsxt.mapper"/> 
</mappers>
  1. New interface under com.bjsxt.mapper
public interface LogMapper { List<Log> selAll(); }
  1. Create a new LogMapper.xml at com.bjsxt.mapper
    namespace must be consistent with the fully qualified path of the interface (package name + class name)
    The id value must be the same as the method name in the interface
    If the method in the interface has multiple parameters, you can omit the parameterType
 <mapper namespace="com.bjsxt.mapper.LogMapper">
	  <select id="selAll" resultType="log"> 
  	  select * from log 
  	  </select> 
  </mapper>

Multi-parameter Realization Method

  1. Declare methods in interfaces
List<Log> selByAccInAccout(String accin,String accout);
  1. Add in mapper.xml
    # Use 0, 1, 2 or param1,param2 in {}
<!-- When there are many parameters,No need to write parameterType -->
<select id="selByAccInAccout" resultType="log" > 
select * from log where accin=#{0} and accout=#{1} 
</select>
  1. The second point can be changed to using annotations.

Declare methods in interfaces

/**
* mybatis The parameter is converted to map, where the @Param("key") parameter content is the value of the map 
* @param accinsss
* @param accoutsssss  */ 
List<Log> selByAccInAccout(@Param("accin") String accin123,@Param("accout") String accout3454235);

Add in mapper.xml
# The @Param("content") parameter is written in {}

<!-- When there are many parameters,No need to write parameterType --> 
<select id="selByAccInAccout" resultType="log" > 
select * from log where accin=#{accin} and accout=#{accout} 
</select>

Posted by CoderGoblin on Thu, 03 Oct 2019 16:13:44 -0700