Introduction to MyBatis
MyBatis is an excellent persistence layer framework that supports plain SQL queries, stored procedures and advanced mapping. MyBatis eliminates manual settings of almost all JDBC code and parameters and search encapsulation of the result set. MyBatis can use simple XML or annotations for configuration and original mapping, mapping interfaces and Java's POJOs (Plain Old Java Objects) into records in the database.
The following is the focus of introducing the use of mapper proxy.
1. Tasks that developers need to complete:
mapper.xml mapping file and mapper.java
2. Development specifications
1. In mapper.xml, namespace is equal to mapper interface address.
2. The method name in the mapper.java interface is the same as the id of the statement in mapper.xml
3. The method input parameter type in the mapper.java interface is the same as the type specified by the parameterType of the statement in mapper.xml.
4. The return value type of the method in the mapper.java interface is the same as the type specified by the resultType of the statement in mapper.xml.
mapper.java example
//Query user information based on id public User findUserById(int id) throws Exception;
mappper.xml example
<select id="findUserById" parameterType="int" resultType="user">SELECT * FROM USER WHERE id=#{value}</select>5. Load mapper.xml in SqlMapConfig.xml
<!-- Loading a single mapping file through the mapper interface follows some specifications: the mapper interface class name and mapper.xml mapping file name need to be consistent, and the premise of the above specification in a directory is: the mapper proxy method is used--><mappers><mapper/></mappers>
or
<mappers><!-- Bulk loading of mapper-specified mapper interface package name. Mybatis automatically scans all mapper interfaces below the package to load according to some specifications: the mapper interface class name and mapper.xml mapping file name need to be consistent, and the premise of the above specification in a directory is: the mapper proxy method is used--><package name="cn.itcast.mybatis.mapper"/></mappers>
The above is a detailed explanation of the usage method of MyBatis Mapper agent introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!