Explanation of @mapper Annotations and supporting annotations in MyBatis (on)

Source: Internet
Author: User
Tags log4j
Preface: The @mapper annotation was added from mybatis3.4.0 to stop writing the mapper mapping file (the XML that wrote the real egg ache ...). )。 It's disgusting. The fact is that there is no detailed explanation for this annotation in the source code. now we have a simple MAVEN project to understand how @mapper annotations are used full Project Please visit my GitHub project address to downloadBuild a MAVEN Web project with the following directory structure:
Import the appropriate dependencies
    <dependency> <groupId>org.mybatis</groupId> <artifactid>mybatis</artifactid > <version>3.4.5</version> </dependency> <dependency> <groupid>or G.mybatis</groupid> <artifactId>mybatis-spring</artifactId> <version>1.3.1</ver
        sion> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.2.RELEASE</version> </dependen cy> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-
        tx</artifactid> <version>5.0.2.RELEASE</version> </dependency> <dependency>
        <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <d Ependency> <groupId>org.springframework</groupId> &LT;ARTIFACTID&GT;SPRING-JDBC&LT;/ARTIFAC tid> <version>5.0.2.RELEASE</version> </dependency> <dependency> <gr Oupid>mysql</groupid> <artifactId>mysql-connector-java</artifactId> <version>6. 0.6</version> </dependency>


3. On the Code

Userdao
import org.apache.ibatis.annotations.Mapper;
Import Org.apache.ibatis.annotations.Param;
Import Org.apache.ibatis.annotations.Select;

Import entity. User;

/**
 * After adding the @mapper annotation, this interface will generate the corresponding implementation class at compile time
 * 
 * Note that this interface cannot define a method with the same name because it generates the same ID * which
 means that the interface is not overloaded
 *
/@Mapper Public
interface Userdao {

    @Select (' select * from user where name = #{name} ')
    public User Find (String name);

    @Select ("SELECT * from user where name = #{name} and pwd = #{pwd}")
    /**
      * For multiple arguments, precede each parameter with a @param annotation,
      * or You will not find the corresponding parameter and then error
      */public
    User Login (@Param ("name") string name, @Param ("pwd") string pwd);

Test class Code

Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

Import DAO. Userdao;
Import entity. User;

public class TestCase {

    @Test public
    void Testmapper () {
        ApplicationContext ac = new Classpathxmlapplicationcontext ("Spring-mybatis.xml");
        Userdao DAO = Ac.getbean (userdao.class);
        User u1 = Dao.find ("hehe");
        User U2 = Dao.login ("hehe", "123");
        System.out.println (U1.getname (). Equals (U2.getname ()));
    }
}
Test Results:

next post plus additions and deletions and dynamic SQL, GitHub projects will also be updated synchronously

~ok

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.