how 1.Mapper agents are developed
Using MyBatis to develop DAO, there are usually two methods, namely the original DAO development method and the Mapper interface development method. MyBatis in the development of DAO, it involves three sisters, namely Sqlsessionfactorybuilder, Sqlsessionfactroy, sqlsession.
Small partners know that the sqlsession in the operation of the database, such as: adding and removing changes, through sqlsessionfactory to create sqlsession, And Sqlsessionfactory is created through Sqlsessionfactorybuilder.
A, Sqlsessionfactorybuilder
Sqlsessionfactorybuilder is used to create sqlsessionfacoty,sqlsessionfacoty once created, no sqlsessionfactorybuilder is needed. Because Sqlsession is produced by Sqlsessionfactory, Sqlsessionfactorybuilder can be used as a tool class, and the best use range is the method scope, the method body local variables.
B, Sqlsessionfactory
Sqlsessionfactory is an interface that defines the different overloaded methods of opensession, the best use of sqlsessionfactory is the entire application run, once created can be reused, Sqlsessionfactory is usually managed in a singleton mode.
C, sqlsession
Sqlsession is a user-oriented interface that defines database operations in Sqlsession and uses Defaultsqlsession implementation classes by default.
There are many ways to manipulate databases in sqlsession: SelectOne (returning a single object), SelectList (returning single or multiple objects), sqlsession being thread insecure, In the Sqlsesion implementation class, in addition to the method in the interface (the method of manipulating the database) there are data domain properties, sqlsession the best application in the method body, defined as local variables used, you can never place a reference to the Sqlsession instance in a static field or instance field of a class. In today's blog post, the small series will focus on the small partners to introduce the two methods of developing DAO in MyBatis, the development Way of primitive DAO and the development of mapper agent.
The development method of DAO in MyBatis is the development of Primitive DAO and the development of mapper agent, the original DAO development and Mapper Dynamic agent development, both of which have advantages. Primitive DAO Development: Programmers need more code to write DAO and DAO implementations, but better understand. Mapper Dynamic Agent: Programmers only need to write Mapper interface, and then configure according to specifications, MyBatis will automatically implement similar DAO implementation, reduce the template method. MyBatis official recommend the use of Mapper proxy method to develop Mapper interface, programmers do not have to write Mapper interface implementation class, when using the Mapper proxy method, input parameters can use Pojo wrapper object or Map object, to ensure the universality of DAO.
Here mainly introduces the mapping agent development mode
2.Mybatis Development process
1, write the MyBatis configuration file Sqlmapconfig.xml
2, write MyBatis mapping file Mapper.xml
Here is the main definition of statement and SQL statements
3, programming through the configuration file to create Sqlsessionfactory
4. Get sqlsession through Sqlsessionfactory
5, through the sqlsession operation database
If you do add, update, delete, you need to call Sqlsession.commit ()
6. Sqlsesion use complete to close
3.
Development Specification of mapper Agent
1. The fully qualified name of the Mapper interface is identical to the namespace value of the mapper mapping file.
Usermapper.xml
[Java]View PlainCopy
- <?xmlversion= "1.0" encoding="UTF-8"?>
- <! Doctypemapper
- Public"-//mybatis.org//dtdmapper 3.0//en"
- "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <!--namespace namespace, in order to isolate SQL statements, easy to manage, mapper develop DAO way, use namespace have special function
- Mapper agent is developed with namespace specified as the fully qualified name of the Mapper interface
- -
- <mapper namespace="Cn.itcast.mybatis.mapper.UserMapper" >
This step is intended to be associated with Mapper.xml and Mapper.java.
2. the ID of statement in Usermapper.xml is the method name in Mapper.java
3. The method parameter type of the Mapper interface is consistent with the ParameterType value of the statement of the mapper mapping file.
4. The method return value type of the Mapper interface is consistent with the Resulttype value of the statement of the mapper mapping file.
4. Development
"1" Mapping interface
[HTML]View PlainCopy
- public interface usermapper{
- 1. Query user information based on user ID
- Public User Finduserbyid (int id) throws Exception;
- 2. Add Users
- public void Insertuser (user user) throws Exception;
- 3. Query user information according to user name
- Public List<User> Finduserbyname (String username) throws Exception;
- }
"2" Mapper mapping file
Create the Mapper directory under config and create the Usermapper.xml
The fully qualified name of the namespace and mapper interfaces is consistent
[Java]View PlainCopy
- <mapper namespace="Cn.itcast.mybatis.mapper.UserMapper" >
- <!--query user information by ID
- ID: uniquely identifies a statement
- #{}: Represents a placeholder, if a parameter in a simple type is passed in #{}, the name in #{} is arbitrarily
- ParameterType: Type of input parameter, receive ParameterType input parameters via #{}
- Resulttype: Output type, regardless of whether the return is multiple or single, specifies the Pojo type of a single record map
- -
- <select id="Finduserbyid" parametertype="int" resulttype="Com.itheima.mybatis.po.user" >
- SELECT * from USER WHERE id= #{id}
- </select>
- <!--Add Users
- ParameterType: Type of input parameter, user object includes Username,birthday,sex,address
- #{} receives Pojo data, you can use OGNL to parse out the value of the Pojo property
- #{username} indicates that the property value of Pojo is obtained from the ParameterType
- Selectkey: Used for primary key return, defines the SQL that gets the primary key value
- Order: Sets the sequence of SQL execution in Selectkey, relative to the INSERT statement
- Keyproperty: Setting the primary key value to which property
- Result type of Resulttype:select last_insert_id ()
- -
- <insert id="Insertuser" parametertype="Cn.itcast.mybatis.po.User" >
- <selectkey keyproperty="id" order="after" resulttype="int" >
- Select LAST_INSERT_ID ()
- </selectKey>
- INSERT into USER (username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address})
- </insert>
- <!--query user information based on user name, may return more than one
- ${}: Represents the concatenation of the SQL, through the ${} to receive parameters, the contents of the parameters without any decoration splicing in SQL.
- -
- <select id="finduserbyname" parametertype="java.lang.String" resulttype= "Cn.itcast.mybatis.po.User ">
- SELECT * from user where usernamelike '%${value}% '
- </select>
- </mapper>
"3" Load Map file
Load the Usermapper.xml in Sqlmapconfing.xml, and if you integrate with spring, you can use the Mapper scanner provided in the consolidation package, where the mappers is not configured. The code looks like this:
"4" Test code
Summary:
Proxy object Internal call SelectOne or SelectList
If the Mapper method returns a single Pojo object (not a collection object), the proxy object queries the database internally through SelectOne.
If the Mapper method returns a collection object, the proxy object internally queries the database through SelectList.
How to develop Mybatis mapper agent