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> <ARTIFACTID>SPRING-JDBC</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