Mapper interface, Mpper XML file and mappers configuration
The Mapper XML file is used to map the defined Mapper interface to the corresponding SQL statement, which simplifies the process of dealing with the database, and can be configured to directly load the result of the query into the object you want to return to you.
This article only from the definition of the Mapper interface to Mybatis-config.xml in the Mapper registration, no detailed explanation mapper usage, if you want to see other usage, and so I try to update the water.
I have divided the entire mapper configuration into three steps, the code speaks ...
Mapper interface mpper XML file and definition of mappers configuration Mapper Interface Mapper XML file mappers configuration using a mapper to create sqlsessionfactory using a mapper in Daoimpl
definition of the Mapper interface
Define the Mapper interface, and delete and change the method is defined by yourself, I define a simplest selectuser
Package com.shenmiu.mapper;
Import Com.shenmiu.po.UserPO;
Public interface Usermapper {
Userpo selectuser (int id);
}
PO definition (Persisted object)
Package Com.shenmiu.po;
Import Org.apache.ibatis.type.Alias;
Note Alias is a way to set type aliases in MyBatis
@Alias ("Userpo") public
class Userpo {
private int id;
private String name;
private int age;
public int getId () {
return ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
this.age = age;
}
@Override public
String toString () {
return ' User [id= + ID + ', name= "+ name +", age= "+ Age +"] ";
}
}
Mapper XML File
My mapper XML file that corresponds to the above Usermapper interface is named Usermapper.xml, and the name of the file can be named casually, but I think it is best to name it "interface name. XML".
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--specifying a unique Namespace,namespace value for this mapper is customarily set to the fully qualified name of the mapped interface, so that the value of namespace is guaranteed to be unique--and
<mapper Namespace= "Com.shenmiu.mapper.UserMapper" >
<select id= "Selectuser" parametertype= "int" resulttype= " Userpo ">
select ID, name, age from
users
WHERE id = #{id}
</select>
</mapper>
Set namespace to the fully qualified name of the interface for a reason, and so on will be clear, do not hurry.
corresponding to the Selectuser method in the Usermapper interface, there is a select child element with an ID of "Selectuser" in the file that does not necessarily match the method name in the interface, but is strongly recommended to remain consistent
ParameterType is the parameter type of the Selectuser method, Resulttype is the return value type of the Selectuser method
The element content in the Select child element is the SQL statement you write yourself, and the SQL statement's writing requirements and format are not explained.
In addition to select, there are other top-level elements in the file
The SQL mapping file has a few top-level elements (in the order in which they should be defined): cache– the cache configuration for a given namespace. cache-ref– references to other namespace cache configurations. Resultmap– is the most complex and powerful element to describe how to load objects from a database result set. sql– A reusable statement block that can be referenced by other statements. insert– Map INSERT Statement update– Map UPDATE statement delete– Map DELETE statement select– map query statement
In this example, only the use of select is given, and the other elements are used in the official documentation. configuration of the mappers
The final step involves mybatis-config.xml(MyBatis's mapping profile, your file name may not be the same as my file name) in mappers configuration.
Here is the directory structure of the Usermapper.xml file that I have stored, which is related to configuring the first mapper registration configuration
For the first method, the Usermapper interface does not have to make any modifications, and then it registers the mapper in the mybatis-config.xml file.
<mappers>
<mapper resource= "Mapping/usermapper.xml"/>
</mappers>
Note the path of resource, incorrect words will cause MyBatis to find your mapping file
Class structure directory, related to the configuration of the second mapper registration
This configuration needs to be annotated with your Usermapper method, which has the same value as the SQL statement set in the Usermapper file .
Public interface Usermapper {
@Select ("SELECT * from users where id = #{id}")
Userpo selectuser (int id);
}
Registering the mapper in mybatis-config.xml
<mappers>
<!--Using Mapper interface Classes-
<mapper class= " Com.shenmiu.mapper.UserMapper "/>
<!--or Register all interfaces with a package as Mappers-
<!-- <package name= "Com.shenmiu.mapper"/>-->
</mappers>
Note that the path to the class in mapper and the path to name in the package are guaranteed to be correct, and I use the idea that there will be hints for paths using the Mapper
Because there are two ways to register, there are two ways to use the Mapper to create sqlsessionfactory using a singleton pattern .
Package Com.shenmiu;
Import org.apache.ibatis.io.Resources;
Import Org.apache.ibatis.session.SqlSessionFactory;
Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;
Import java.io.IOException;
Import Java.io.InputStream;
public class Sqlsessionfactoryutil {static Sqlsessionfactory sqlsessionfactory; Private Sqlsessionfactoryutil () {} private static void Factoryinit () {//mybatis configuration file, the file path is shown above in the figure Str
ing resource = "mybatis-config.xml";
Load mybatis configuration file InputStream inputstream = null;
try {inputstream = Resources.getresourceasstream (Resource);
} catch (IOException e) {e.printstacktrace ();
}//Build sqlsession Factory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream); public static final Sqlsessionfactory getsqlsessionfactory () {if (null = = sqlsessionfactory) {FA
Ctoryinit ();
} return sqlsessionfactory; }
}
using the mapper in Daoimpl
Package Com.shenmiu.daoImpl;
Import Com.shenmiu.SqlSessionFactoryUtil;
Import Com.shenmiu.dao.UserDao;
Import Com.shenmiu.mapper.UserMapper;
Import Com.shenmiu.po.UserPO;
Import org.apache.ibatis.session.SqlSession;
public class Userdaoimpl implements Userdao {public
userpo getUser (int id) {
sqlsession session = Sqlsessionfacto Ryutil.getsqlsessionfactory (). Opensession ();
Userpo user = null;
try {
// user = Session.selectone ("Com.shenmiu.mapper.UserMapper.selectUser", id), corresponding to the first method of registration
// user = Session.getmapper (usermapper.class). Selectuser (1); corresponding to the second method of registration
} finally {
session.close ();
}
return user;
}
}
Note: The SelectOne parameter in the first usage method is the namespace+idin the Usermapper.xml file, which uniquely specifies the second use of the SQL statement to be executed The Getmapper parameter in the usermapper.classis the class object of your mapper when you use the First registration method , if Namespace is a fully qualified class name and the ID is the method name of the corresponding class , you can also use Getmapper method to obtain the required Mapper object when you use the second registration method, But must and can only use the Getmapper method
In summary, it is highly recommended to use the registration method of the resource attribute in mapper (write a usermapper.xml file with no harm)