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
- 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 - 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
- Use the package tag under mappers in mybatis.xml
<mappers> <package name="com.bjsxt.mapper"/> </mappers>
- New interface under com.bjsxt.mapper
public interface LogMapper { List<Log> selAll(); }
- 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
- Declare methods in interfaces
List<Log> selByAccInAccout(String accin,String accout);
- 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>
- 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>